aboutsummaryrefslogtreecommitdiff
path: root/api/logic/net
diff options
context:
space:
mode:
Diffstat (limited to 'api/logic/net')
-rw-r--r--api/logic/net/ByteArraySink.h62
-rw-r--r--api/logic/net/ChecksumValidator.h55
-rw-r--r--api/logic/net/Download.cpp309
-rw-r--r--api/logic/net/Download.h76
-rw-r--r--api/logic/net/FileSink.cpp115
-rw-r--r--api/logic/net/FileSink.h28
-rw-r--r--api/logic/net/HttpMetaCache.cpp273
-rw-r--r--api/logic/net/HttpMetaCache.h125
-rw-r--r--api/logic/net/MetaCacheSink.cpp65
-rw-r--r--api/logic/net/MetaCacheSink.h22
-rw-r--r--api/logic/net/Mode.h10
-rw-r--r--api/logic/net/NetAction.h115
-rw-r--r--api/logic/net/NetJob.cpp218
-rw-r--r--api/logic/net/NetJob.h91
-rw-r--r--api/logic/net/PasteUpload.cpp104
-rw-r--r--api/logic/net/PasteUpload.h49
-rw-r--r--api/logic/net/Sink.h71
-rw-r--r--api/logic/net/Validator.h20
18 files changed, 0 insertions, 1808 deletions
diff --git a/api/logic/net/ByteArraySink.h b/api/logic/net/ByteArraySink.h
deleted file mode 100644
index 20e6764c..00000000
--- a/api/logic/net/ByteArraySink.h
+++ /dev/null
@@ -1,62 +0,0 @@
-#pragma once
-
-#include "Sink.h"
-
-namespace Net {
-/*
- * Sink object for downloads that uses an external QByteArray it doesn't own as a target.
- */
-class ByteArraySink : public Sink
-{
-public:
- ByteArraySink(QByteArray *output)
- :m_output(output)
- {
- // nil
- };
-
- virtual ~ByteArraySink()
- {
- // nil
- }
-
-public:
- JobStatus init(QNetworkRequest & request) override
- {
- m_output->clear();
- if(initAllValidators(request))
- return Job_InProgress;
- return Job_Failed;
- };
-
- JobStatus write(QByteArray & data) override
- {
- m_output->append(data);
- if(writeAllValidators(data))
- return Job_InProgress;
- return Job_Failed;
- }
-
- JobStatus abort() override
- {
- m_output->clear();
- failAllValidators();
- return Job_Failed;
- }
-
- JobStatus finalize(QNetworkReply &reply) override
- {
- if(finalizeAllValidators(reply))
- return Job_Finished;
- return Job_Failed;
- }
-
- bool hasLocalData() override
- {
- return false;
- }
-
-private:
- QByteArray * m_output;
-};
-}
diff --git a/api/logic/net/ChecksumValidator.h b/api/logic/net/ChecksumValidator.h
deleted file mode 100644
index 0d6b19c2..00000000
--- a/api/logic/net/ChecksumValidator.h
+++ /dev/null
@@ -1,55 +0,0 @@
-#pragma once
-
-#include "Validator.h"
-#include <QCryptographicHash>
-#include <memory>
-#include <QFile>
-
-namespace Net {
-class ChecksumValidator: public Validator
-{
-public: /* con/des */
- ChecksumValidator(QCryptographicHash::Algorithm algorithm, QByteArray expected = QByteArray())
- :m_checksum(algorithm), m_expected(expected)
- {
- };
- virtual ~ChecksumValidator() {};
-
-public: /* methods */
- bool init(QNetworkRequest &) override
- {
- m_checksum.reset();
- return true;
- }
- bool write(QByteArray & data) override
- {
- m_checksum.addData(data);
- return true;
- }
- bool abort() override
- {
- return true;
- }
- bool validate(QNetworkReply &) override
- {
- if(m_expected.size() && m_expected != hash())
- {
- qWarning() << "Checksum mismatch, download is bad.";
- return false;
- }
- return true;
- }
- QByteArray hash()
- {
- return m_checksum.result();
- }
- void setExpected(QByteArray expected)
- {
- m_expected = expected;
- }
-
-private: /* data */
- QCryptographicHash m_checksum;
- QByteArray m_expected;
-};
-} \ No newline at end of file
diff --git a/api/logic/net/Download.cpp b/api/logic/net/Download.cpp
deleted file mode 100644
index 3f183b7d..00000000
--- a/api/logic/net/Download.cpp
+++ /dev/null
@@ -1,309 +0,0 @@
-/* Copyright 2013-2021 MultiMC Contributors
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "Download.h"
-
-#include "BuildConfig.h"
-#include <QFileInfo>
-#include <QDateTime>
-#include <QDebug>
-#include "Env.h"
-#include <FileSystem.h>
-#include "ChecksumValidator.h"
-#include "MetaCacheSink.h"
-#include "ByteArraySink.h"
-
-namespace Net {
-
-Download::Download():NetAction()
-{
- m_status = Job_NotStarted;
-}
-
-Download::Ptr Download::makeCached(QUrl url, MetaEntryPtr entry, Options options)
-{
- Download * dl = new Download();
- dl->m_url = url;
- dl->m_options = options;
- auto md5Node = new ChecksumValidator(QCryptographicHash::Md5);
- auto cachedNode = new MetaCacheSink(entry, md5Node);
- dl->m_sink.reset(cachedNode);
- dl->m_target_path = entry->getFullPath();
- return std::shared_ptr<Download>(dl);
-}
-
-Download::Ptr Download::makeByteArray(QUrl url, QByteArray *output, Options options)
-{
- Download * dl = new Download();
- dl->m_url = url;
- dl->m_options = options;
- dl->m_sink.reset(new ByteArraySink(output));
- return std::shared_ptr<Download>(dl);
-}
-
-Download::Ptr Download::makeFile(QUrl url, QString path, Options options)
-{
- Download * dl = new Download();
- dl->m_url = url;
- dl->m_options = options;
- dl->m_sink.reset(new FileSink(path));
- return std::shared_ptr<Download>(dl);
-}
-
-void Download::addValidator(Validator * v)
-{
- m_sink->addValidator(v);
-}
-
-void Download::start()
-{
- if(m_status == Job_Aborted)
- {
- qWarning() << "Attempt to start an aborted Download:" << m_url.toString();
- emit aborted(m_index_within_job);
- return;
- }
- QNetworkRequest request(m_url);
- m_status = m_sink->init(request);
- switch(m_status)
- {
- case Job_Finished:
- emit succeeded(m_index_within_job);
- qDebug() << "Download cache hit " << m_url.toString();
- return;
- case Job_InProgress:
- qDebug() << "Downloading " << m_url.toString();
- break;
- case Job_Failed_Proceed: // this is meaningless in this context. We do need a sink.
- case Job_NotStarted:
- case Job_Failed:
- emit failed(m_index_within_job);
- return;
- case Job_Aborted:
- return;
- }
-
- request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT);
-
- QNetworkReply *rep = ENV.qnam().get(request);
-
- m_reply.reset(rep);
- connect(rep, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64)));
- connect(rep, SIGNAL(finished()), SLOT(downloadFinished()));
- connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(downloadError(QNetworkReply::NetworkError)));
- connect(rep, &QNetworkReply::sslErrors, this, &Download::sslErrors);
- connect(rep, &QNetworkReply::readyRead, this, &Download::downloadReadyRead);
-}
-
-void Download::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
-{
- m_total_progress = bytesTotal;
- m_progress = bytesReceived;
- emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal);
-}
-
-void Download::downloadError(QNetworkReply::NetworkError error)
-{
- if(error == QNetworkReply::OperationCanceledError)
- {
- qCritical() << "Aborted " << m_url.toString();
- m_status = Job_Aborted;
- }
- else
- {
- if(m_options & Option::AcceptLocalFiles)
- {
- if(m_sink->hasLocalData())
- {
- m_status = Job_Failed_Proceed;
- return;
- }
- }
- // error happened during download.
- qCritical() << "Failed " << m_url.toString() << " with reason " << error;
- m_status = Job_Failed;
- }
-}
-
-void Download::sslErrors(const QList<QSslError> & errors)
-{
- int i = 1;
- for (auto error : errors)
- {
- qCritical() << "Download" << m_url.toString() << "SSL Error #" << i << " : " << error.errorString();
- auto cert = error.certificate();
- qCritical() << "Certificate in question:\n" << cert.toText();
- i++;
- }
-}
-
-bool Download::handleRedirect()
-{
- QUrl redirect = m_reply->header(QNetworkRequest::LocationHeader).toUrl();
- if(!redirect.isValid())
- {
- if(!m_reply->hasRawHeader("Location"))
- {
- // no redirect -> it's fine to continue
- return false;
- }
- // there is a Location header, but it's not correct. we need to apply some workarounds...
- QByteArray redirectBA = m_reply->rawHeader("Location");
- if(redirectBA.size() == 0)
- {
- // empty, yet present redirect header? WTF?
- return false;
- }
- QString redirectStr = QString::fromUtf8(redirectBA);
-
- if(redirectStr.startsWith("//"))
- {
- /*
- * IF the URL begins with //, we need to insert the URL scheme.
- * See: https://bugreports.qt.io/browse/QTBUG-41061
- * See: http://tools.ietf.org/html/rfc3986#section-4.2
- */
- redirectStr = m_reply->url().scheme() + ":" + redirectStr;
- }
- else if(redirectStr.startsWith("/"))
- {
- /*
- * IF the URL begins with /, we need to process it as a relative URL
- */
- auto url = m_reply->url();
- url.setPath(redirectStr, QUrl::TolerantMode);
- redirectStr = url.toString();
- }
-
- /*
- * Next, make sure the URL is parsed in tolerant mode. Qt doesn't parse the location header in tolerant mode, which causes issues.
- * FIXME: report Qt bug for this
- */
- redirect = QUrl(redirectStr, QUrl::TolerantMode);
- if(!redirect.isValid())
- {
- qWarning() << "Failed to parse redirect URL:" << redirectStr;
- downloadError(QNetworkReply::ProtocolFailure);
- return false;
- }
- qDebug() << "Fixed location header:" << redirect;
- }
- else
- {
- qDebug() << "Location header:" << redirect;
- }
-
- m_url = QUrl(redirect.toString());
- qDebug() << "Following redirect to " << m_url.toString();
- start();
- return true;
-}
-
-
-void Download::downloadFinished()
-{
- // handle HTTP redirection first
- if(handleRedirect())
- {
- qDebug() << "Download redirected:" << m_url.toString();
- return;
- }
-
- // if the download failed before this point ...
- if (m_status == Job_Failed_Proceed)
- {
- qDebug() << "Download failed but we are allowed to proceed:" << m_url.toString();
- m_sink->abort();
- m_reply.reset();
- emit succeeded(m_index_within_job);
- return;
- }
- else if (m_status == Job_Failed)
- {
- qDebug() << "Download failed in previous step:" << m_url.toString();
- m_sink->abort();
- m_reply.reset();
- emit failed(m_index_within_job);
- return;
- }
- else if(m_status == Job_Aborted)
- {
- qDebug() << "Download aborted in previous step:" << m_url.toString();
- m_sink->abort();
- m_reply.reset();
- emit aborted(m_index_within_job);
- return;
- }
-
- // make sure we got all the remaining data, if any
- auto data = m_reply->readAll();
- if(data.size())
- {
- qDebug() << "Writing extra" << data.size() << "bytes to" << m_target_path;
- m_status = m_sink->write(data);
- }
-
- // otherwise, finalize the whole graph
- m_status = m_sink->finalize(*m_reply.get());
- if (m_status != Job_Finished)
- {
- qDebug() << "Download failed to finalize:" << m_url.toString();
- m_sink->abort();
- m_reply.reset();
- emit failed(m_index_within_job);
- return;
- }
- m_reply.reset();
- qDebug() << "Download succeeded:" << m_url.toString();
- emit succeeded(m_index_within_job);
-}
-
-void Download::downloadReadyRead()
-{
- if(m_status == Job_InProgress)
- {
- auto data = m_reply->readAll();
- m_status = m_sink->write(data);
- if(m_status == Job_Failed)
- {
- qCritical() << "Failed to process response chunk for " << m_target_path;
- }
- // qDebug() << "Download" << m_url.toString() << "gained" << data.size() << "bytes";
- }
- else
- {
- qCritical() << "Cannot write to " << m_target_path << ", illegal status" << m_status;
- }
-}
-
-}
-
-bool Net::Download::abort()
-{
- if(m_reply)
- {
- m_reply->abort();
- }
- else
- {
- m_status = Job_Aborted;
- }
- return true;
-}
-
-bool Net::Download::canAbort()
-{
- return true;
-}
diff --git a/api/logic/net/Download.h b/api/logic/net/Download.h
deleted file mode 100644
index 2c436032..00000000
--- a/api/logic/net/Download.h
+++ /dev/null
@@ -1,76 +0,0 @@
-/* Copyright 2013-2021 MultiMC Contributors
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#pragma once
-
-#include "NetAction.h"
-#include "HttpMetaCache.h"
-#include "Validator.h"
-#include "Sink.h"
-
-#include "multimc_logic_export.h"
-namespace Net {
-class MULTIMC_LOGIC_EXPORT Download : public NetAction
-{
- Q_OBJECT
-
-public: /* types */
- typedef std::shared_ptr<class Download> Ptr;
- enum class Option
- {
- NoOptions = 0,
- AcceptLocalFiles = 1
- };
- Q_DECLARE_FLAGS(Options, Option)
-
-protected: /* con/des */
- explicit Download();
-public:
- virtual ~Download(){};
- static Download::Ptr makeCached(QUrl url, MetaEntryPtr entry, Options options = Option::NoOptions);
- static Download::Ptr makeByteArray(QUrl url, QByteArray *output, Options options = Option::NoOptions);
- static Download::Ptr makeFile(QUrl url, QString path, Options options = Option::NoOptions);
-
-public: /* methods */
- QString getTargetFilepath()
- {
- return m_target_path;
- }
- void addValidator(Validator * v);
- bool abort() override;
- bool canAbort() override;
-
-private: /* methods */
- bool handleRedirect();
-
-protected slots:
- void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) override;
- void downloadError(QNetworkReply::NetworkError error) override;
- void sslErrors(const QList<QSslError> & errors);
- void downloadFinished() override;
- void downloadReadyRead() override;
-
-public slots:
- void start() override;
-
-private: /* data */
- // FIXME: remove this, it has no business being here.
- QString m_target_path;
- std::unique_ptr<Sink> m_sink;
- Options m_options;
-};
-}
-
-Q_DECLARE_OPERATORS_FOR_FLAGS(Net::Download::Options)
diff --git a/api/logic/net/FileSink.cpp b/api/logic/net/FileSink.cpp
deleted file mode 100644
index 8b3e917d..00000000
--- a/api/logic/net/FileSink.cpp
+++ /dev/null
@@ -1,115 +0,0 @@
-#include "FileSink.h"
-#include <QFile>
-#include <QFileInfo>
-#include "Env.h"
-#include "FileSystem.h"
-
-namespace Net {
-
-FileSink::FileSink(QString filename)
- :m_filename(filename)
-{
- // nil
-}
-
-FileSink::~FileSink()
-{
- // nil
-}
-
-JobStatus FileSink::init(QNetworkRequest& request)
-{
- auto result = initCache(request);
- if(result != Job_InProgress)
- {
- return result;
- }
- // create a new save file and open it for writing
- if (!FS::ensureFilePathExists(m_filename))
- {
- qCritical() << "Could not create folder for " + m_filename;
- return Job_Failed;
- }
- wroteAnyData = false;
- m_output_file.reset(new QSaveFile(m_filename));
- if (!m_output_file->open(QIODevice::WriteOnly))
- {
- qCritical() << "Could not open " + m_filename + " for writing";
- return Job_Failed;
- }
-
- if(initAllValidators(request))
- return Job_InProgress;
- return Job_Failed;
-}
-
-JobStatus FileSink::initCache(QNetworkRequest &)
-{
- return Job_InProgress;
-}
-
-JobStatus FileSink::write(QByteArray& data)
-{
- if (!writeAllValidators(data) || m_output_file->write(data) != data.size())
- {
- qCritical() << "Failed writing into " + m_filename;
- m_output_file->cancelWriting();
- m_output_file.reset();
- wroteAnyData = false;
- return Job_Failed;
- }
- wroteAnyData = true;
- return Job_InProgress;
-}
-
-JobStatus FileSink::abort()
-{
- m_output_file->cancelWriting();
- failAllValidators();
- return Job_Failed;
-}
-
-JobStatus FileSink::finalize(QNetworkReply& reply)
-{
- bool gotFile = false;
- QVariant statusCodeV = reply.attribute(QNetworkRequest::HttpStatusCodeAttribute);
- bool validStatus = false;
- int statusCode = statusCodeV.toInt(&validStatus);
- if(validStatus)
- {
- // this leaves out 304 Not Modified
- gotFile = statusCode == 200 || statusCode == 203;
- }
- // if we wrote any data to the save file, we try to commit the data to the real file.
- // if it actually got a proper file, we write it even if it was empty
- if (gotFile || wroteAnyData)
- {
- // ask validators for data consistency
- // we only do this for actual downloads, not 'your data is still the same' cache hits
- if(!finalizeAllValidators(reply))
- return Job_Failed;
- // nothing went wrong...
- if (!m_output_file->commit())
- {
- qCritical() << "Failed to commit changes to " << m_filename;
- m_output_file->cancelWriting();
- return Job_Failed;
- }
- }
- // then get rid of the save file
- m_output_file.reset();
-
- return finalizeCache(reply);
-}
-
-JobStatus FileSink::finalizeCache(QNetworkReply &)
-{
- return Job_Finished;
-}
-
-bool FileSink::hasLocalData()
-{
- QFileInfo info(m_filename);
- return info.exists() && info.size() != 0;
-}
-}
diff --git a/api/logic/net/FileSink.h b/api/logic/net/FileSink.h
deleted file mode 100644
index 875fe511..00000000
--- a/api/logic/net/FileSink.h
+++ /dev/null
@@ -1,28 +0,0 @@
-#pragma once
-#include "Sink.h"
-#include <QSaveFile>
-
-namespace Net {
-class FileSink : public Sink
-{
-public: /* con/des */
- FileSink(QString filename);
- virtual ~FileSink();
-
-public: /* methods */
- JobStatus init(QNetworkRequest & request) override;
- JobStatus write(QByteArray & data) override;
- JobStatus abort() override;
- JobStatus finalize(QNetworkReply & reply) override;
- bool hasLocalData() override;
-
-protected: /* methods */
- virtual JobStatus initCache(QNetworkRequest &);
- virtual JobStatus finalizeCache(QNetworkReply &reply);
-
-protected: /* data */
- QString m_filename;
- bool wroteAnyData = false;
- std::unique_ptr<QSaveFile> m_output_file;
-};
-}
diff --git a/api/logic/net/HttpMetaCache.cpp b/api/logic/net/HttpMetaCache.cpp
deleted file mode 100644
index 4bc8fbc8..00000000
--- a/api/logic/net/HttpMetaCache.cpp
+++ /dev/null
@@ -1,273 +0,0 @@
-/* Copyright 2013-2021 MultiMC Contributors
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "Env.h"
-#include "HttpMetaCache.h"
-#include "FileSystem.h"
-
-#include <QFileInfo>
-#include <QFile>
-#include <QDateTime>
-#include <QCryptographicHash>
-
-#include <QDebug>
-
-#include <QJsonDocument>
-#include <QJsonArray>
-#include <QJsonObject>
-
-QString MetaEntry::getFullPath()
-{
- // FIXME: make local?
- return FS::PathCombine(basePath, relativePath);
-}
-
-HttpMetaCache::HttpMetaCache(QString path) : QObject()
-{
- m_index_file = path;
- saveBatchingTimer.setSingleShot(true);
- saveBatchingTimer.setTimerType(Qt::VeryCoarseTimer);
- connect(&saveBatchingTimer, SIGNAL(timeout()), SLOT(SaveNow()));
-}
-
-HttpMetaCache::~HttpMetaCache()
-{
- saveBatchingTimer.stop();
- SaveNow();
-}
-
-MetaEntryPtr HttpMetaCache::getEntry(QString base, QString resource_path)
-{
- // no base. no base path. can't store
- if (!m_entries.contains(base))
- {
- // TODO: log problem
- return MetaEntryPtr();
- }
- EntryMap &map = m_entries[base];
- if (map.entry_list.contains(resource_path))
- {
- return map.entry_list[resource_path];
- }
- return MetaEntryPtr();
-}
-
-MetaEntryPtr HttpMetaCache::resolveEntry(QString base, QString resource_path, QString expected_etag)
-{
- auto entry = getEntry(base, resource_path);
- // it's not present? generate a default stale entry
- if (!entry)
- {
- return staleEntry(base, resource_path);
- }
-
- auto &selected_base = m_entries[base];
- QString real_path = FS::PathCombine(selected_base.base_path, resource_path);
- QFileInfo finfo(real_path);
-
- // is the file really there? if not -> stale
- if (!finfo.isFile() || !finfo.isReadable())
- {
- // if the file doesn't exist, we disown the entry
- selected_base.entry_list.remove(resource_path);
- return staleEntry(base, resource_path);
- }
-
- if (!expected_etag.isEmpty() && expected_etag != entry->etag)
- {
- // if the etag doesn't match expected, we disown the entry
- selected_base.entry_list.remove(resource_path);
- return staleEntry(base, resource_path);
- }
-
- // if the file changed, check md5sum
- qint64 file_last_changed = finfo.lastModified().toUTC().toMSecsSinceEpoch();
- if (file_last_changed != entry->local_changed_timestamp)
- {
- QFile input(real_path);
- input.open(QIODevice::ReadOnly);
- QString md5sum = QCryptographicHash::hash(input.readAll(), QCryptographicHash::Md5)
- .toHex()
- .constData();
- if (entry->md5sum != md5sum)
- {
- selected_base.entry_list.remove(resource_path);
- return staleEntry(base, resource_path);
- }
- // md5sums matched... keep entry and save the new state to file
- entry->local_changed_timestamp = file_last_changed;
- SaveEventually();
- }
-
- // entry passed all the checks we cared about.
- entry->basePath = getBasePath(base);
- return entry;
-}
-
-bool HttpMetaCache::updateEntry(MetaEntryPtr stale_entry)
-{
- if (!m_entries.contains(stale_entry->baseId))
- {
- qCritical() << "Cannot add entry with unknown base: "
- << stale_entry->baseId.toLocal8Bit();
- return false;
- }
- if (stale_entry->stale)
- {
- qCritical() << "Cannot add stale entry: " << stale_entry->getFullPath().toLocal8Bit();
- return false;
- }
- m_entries[stale_entry->baseId].entry_list[stale_entry->relativePath] = stale_entry;
- SaveEventually();
- return true;
-}
-
-bool HttpMetaCache::evictEntry(MetaEntryPtr entry)
-{
- if(entry)
- {
- entry->stale = true;
- SaveEventually();
- return true;
- }
- return false;
-}
-
-MetaEntryPtr HttpMetaCache::staleEntry(QString base, QString resource_path)
-{
- auto foo = new MetaEntry();
- foo->baseId = base;
- foo->basePath = getBasePath(base);
- foo->relativePath = resource_path;
- foo->stale = true;
- return MetaEntryPtr(foo);
-}
-
-void HttpMetaCache::addBase(QString base, QString base_root)
-{
- // TODO: report error
- if (m_entries.contains(base))
- return;
- // TODO: check if the base path is valid
- EntryMap foo;
- foo.base_path = base_root;
- m_entries[base] = foo;
-}
-
-QString HttpMetaCache::getBasePath(QString base)
-{
- if (m_entries.contains(base))
- {
- return m_entries[base].base_path;
- }
- return QString();
-}
-
-void HttpMetaCache::Load()
-{
- if(m_index_file.isNull())
- return;
-
- QFile index(m_index_file);
- if (!index.open(QIODevice::ReadOnly))
- return;
-
- QJsonDocument json = QJsonDocument::fromJson(index.readAll());
- if (!json.isObject())
- return;
- auto root = json.object();
- // check file version first
- auto version_val = root.value("version");
- if (!version_val.isString())
- return;
- if (version_val.toString() != "1")
- return;
-
- // read the entry array
- auto entries_val = root.value("entries");
- if (!entries_val.isArray())
- return;
- QJsonArray array = entries_val.toArray();
- for (auto element : array)
- {
- if (!element.isObject())
- return;
- auto element_obj = element.toObject();
- QString base = element_obj.value("base").toString();
- if (!m_entries.contains(base))
- continue;
- auto &entrymap = m_entries[base];
- auto foo = new MetaEntry();
- foo->baseId = base;
- QString path = foo->relativePath = element_obj.value("path").toString();
- foo->md5sum = element_obj.value("md5sum").toString();
- foo->etag = element_obj.value("etag").toString();
- foo->local_changed_timestamp = element_obj.value("last_changed_timestamp").toDouble();
- foo->remote_changed_timestamp =
- element_obj.value("remote_changed_timestamp").toString();
- // presumed innocent until closer examination
- foo->stale = false;
- entrymap.entry_list[path] = MetaEntryPtr(foo);
- }
-}
-
-void HttpMetaCache::SaveEventually()
-{
- // reset the save timer
- saveBatchingTimer.stop();
- saveBatchingTimer.start(30000);
-}
-
-void HttpMetaCache::SaveNow()
-{
- if(m_index_file.isNull())
- return;
- QJsonObject toplevel;
- toplevel.insert("version", QJsonValue(QString("1")));
- QJsonArray entriesArr;
- for (auto group : m_entries)
- {
- for (auto entry : group.entry_list)
- {
- // do not save stale entries. they are dead.
- if(entry->stale)
- {
- continue;
- }
- QJsonObject entryObj;
- entryObj.insert("base", QJsonValue(entry->baseId));
- entryObj.insert("path", QJsonValue(entry->relativePath));
- entryObj.insert("md5sum", QJsonValue(entry->md5sum));
- entryObj.insert("etag", QJsonValue(entry->etag));
- entryObj.insert("last_changed_timestamp",
- QJsonValue(double(entry->local_changed_timestamp)));
- if (!entry->remote_changed_timestamp.isEmpty())
- entryObj.insert("remote_changed_timestamp",
- QJsonValue(entry->remote_changed_timestamp));
- entriesArr.append(entryObj);
- }
- }
- toplevel.insert("entries", entriesArr);
-
- QJsonDocument doc(toplevel);
- try
- {
- FS::write(m_index_file, doc.toJson());
- }
- catch (const Exception &e)
- {
- qWarning() << e.what();
- }
-}
diff --git a/api/logic/net/HttpMetaCache.h b/api/logic/net/HttpMetaCache.h
deleted file mode 100644
index c3248793..00000000
--- a/api/logic/net/HttpMetaCache.h
+++ /dev/null
@@ -1,125 +0,0 @@
-/* Copyright 2013-2021 MultiMC Contributors
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#pragma once
-#include <QString>
-#include <QMap>
-#include <qtimer.h>
-#include <memory>
-
-#include "multimc_logic_export.h"
-
-class HttpMetaCache;
-
-class MULTIMC_LOGIC_EXPORT MetaEntry
-{
-friend class HttpMetaCache;
-protected:
- MetaEntry() {}
-public:
- bool isStale()
- {
- return stale;
- }
- void setStale(bool stale)
- {
- this->stale = stale;
- }
- QString getFullPath();
- QString getRemoteChangedTimestamp()
- {
- return remote_changed_timestamp;
- }
- void setRemoteChangedTimestamp(QString remote_changed_timestamp)
- {
- this->remote_changed_timestamp = remote_changed_timestamp;
- }
- void setLocalChangedTimestamp(qint64 timestamp)
- {
- local_changed_timestamp = timestamp;
- }
- QString getETag()
- {
- return etag;
- }
- void setETag(QString etag)
- {
- this->etag = etag;
- }
- QString getMD5