From ab868df50eb6f9f3958bdc0a7ab9199dcdf46b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 11 Mar 2017 01:39:45 +0100 Subject: NOISSUE Wonko is the new Meta And then Wonko was the Meta. --- api/logic/meta/BaseEntity.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 api/logic/meta/BaseEntity.cpp (limited to 'api/logic/meta/BaseEntity.cpp') diff --git a/api/logic/meta/BaseEntity.cpp b/api/logic/meta/BaseEntity.cpp new file mode 100644 index 00000000..fd44e29c --- /dev/null +++ b/api/logic/meta/BaseEntity.cpp @@ -0,0 +1,42 @@ +/* Copyright 2015-2017 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 "BaseEntity.h" + +#include "Json.h" +#include "Util.h" + +namespace Meta +{ +BaseEntity::~BaseEntity() +{ +} + +void BaseEntity::store() const +{ + Json::write(serialized(), Meta::localDir().absoluteFilePath(localFilename())); +} + +void BaseEntity::notifyLocalLoadComplete() +{ + m_localLoaded = true; + store(); +} +void BaseEntity::notifyRemoteLoadComplete() +{ + m_remoteLoaded = true; + store(); +} +} -- cgit v1.2.3 From 0060b506257b906d40ef53d1e23404dba76afcee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Fri, 17 Mar 2017 01:48:54 +0100 Subject: NOISSUE simplify. --- api/logic/meta/BaseEntity.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'api/logic/meta/BaseEntity.cpp') diff --git a/api/logic/meta/BaseEntity.cpp b/api/logic/meta/BaseEntity.cpp index fd44e29c..87cd55c8 100644 --- a/api/logic/meta/BaseEntity.cpp +++ b/api/logic/meta/BaseEntity.cpp @@ -24,19 +24,18 @@ BaseEntity::~BaseEntity() { } -void BaseEntity::store() const +QUrl BaseEntity::url() const { - Json::write(serialized(), Meta::localDir().absoluteFilePath(localFilename())); + return rootUrl().resolved(localFilename()); } void BaseEntity::notifyLocalLoadComplete() { m_localLoaded = true; - store(); } + void BaseEntity::notifyRemoteLoadComplete() { m_remoteLoaded = true; - store(); } } -- cgit v1.2.3 From e46aba9da584338db8d8a1a8a487bdcc6cf84343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 18 Mar 2017 02:22:36 +0100 Subject: NOISSUE sanitize loading and downloading of metadata files --- api/logic/meta/BaseEntity.cpp | 119 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 109 insertions(+), 10 deletions(-) (limited to 'api/logic/meta/BaseEntity.cpp') diff --git a/api/logic/meta/BaseEntity.cpp b/api/logic/meta/BaseEntity.cpp index 87cd55c8..18b7f92f 100644 --- a/api/logic/meta/BaseEntity.cpp +++ b/api/logic/meta/BaseEntity.cpp @@ -16,26 +16,125 @@ #include "BaseEntity.h" #include "Json.h" -#include "Util.h" -namespace Meta +#include "net/Download.h" +#include "net/HttpMetaCache.h" +#include "net/NetJob.h" + +#include "Env.h" +#include "Json.h" + +class ParsingValidator : public Net::Validator { -BaseEntity::~BaseEntity() +public: /* con/des */ + ParsingValidator(Meta::BaseEntity *entity) : m_entity(entity) + { + }; + virtual ~ParsingValidator() + { + }; + +public: /* methods */ + bool init(QNetworkRequest &) override + { + return true; + } + bool write(QByteArray & data) override + { + this->data.append(data); + return true; + } + bool abort() override + { + return true; + } + bool validate(QNetworkReply &) override + { + auto fname = m_entity->localFilename(); + try + { + m_entity->parse(Json::requireObject(Json::requireDocument(data, fname), fname)); + return true; + } + catch (Exception &e) + { + qWarning() << "Unable to parse response:" << e.cause(); + return false; + } + } + +private: /* data */ + QByteArray data; + Meta::BaseEntity *m_entity; +}; + +Meta::BaseEntity::~BaseEntity() { } -QUrl BaseEntity::url() const +QUrl Meta::BaseEntity::url() const { - return rootUrl().resolved(localFilename()); + return QUrl("https://meta.multimc.org").resolved(localFilename()); } -void BaseEntity::notifyLocalLoadComplete() +bool Meta::BaseEntity::loadLocalFile() { - m_localLoaded = true; + const QString fname = QDir("meta").absoluteFilePath(localFilename()); + if (!QFile::exists(fname)) + { + return false; + } + // TODO: check if the file has the expected checksum + try + { + parse(Json::requireObject(Json::requireDocument(fname, fname), fname)); + return true; + } + catch (Exception &e) + { + qDebug() << QString("Unable to parse file %1: %2").arg(fname, e.cause()); + // just make sure it's gone and we never consider it again. + QFile::remove(fname); + return false; + } } -void BaseEntity::notifyRemoteLoadComplete() +void Meta::BaseEntity::load() { - m_remoteLoaded = true; -} + if(!isLoaded()) + { + loadLocalFile(); + } + if(!shouldStartRemoteUpdate()) + { + return; + } + NetJob *job = new NetJob(QObject::tr("Download of meta file %1").arg(localFilename())); + + auto url = this->url(); + auto entry = ENV.metacache()->resolveEntry("meta", localFilename()); + entry->setStale(true); + auto dl = Net::Download::makeCached(url, entry); + /* + * The validator parses the file and loads it into the object. + * If that fails, the file is not written to storage. + */ + dl->addValidator(new ParsingValidator(this)); + job->addNetAction(dl); + m_updateStatus = UpdateStatus::InProgress; + m_updateTask.reset(job); + QObject::connect(job, &NetJob::succeeded, [&]() + { + m_loadStatus = LoadStatus::Remote; + m_updateStatus = UpdateStatus::Succeeded; + m_updateTask.reset(); + }); + QObject::connect(job, &NetJob::failed, [&]() + { + m_updateStatus = UpdateStatus::Failed; + m_updateTask.reset(); + }); + m_updateTask->start(); } + +#include "BaseEntity.moc" -- cgit v1.2.3 From 2660418d58efb33cd3a0ab8ed9d48c359c076905 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 19 Mar 2017 02:13:49 +0100 Subject: NOISSUE hack it together enough to get launching back Meta index will now always return valid objects. They just might never load if they don't exist on the server. --- api/logic/meta/BaseEntity.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'api/logic/meta/BaseEntity.cpp') diff --git a/api/logic/meta/BaseEntity.cpp b/api/logic/meta/BaseEntity.cpp index 18b7f92f..809f88cb 100644 --- a/api/logic/meta/BaseEntity.cpp +++ b/api/logic/meta/BaseEntity.cpp @@ -137,4 +137,13 @@ void Meta::BaseEntity::load() m_updateTask->start(); } +shared_qobject_ptr Meta::BaseEntity::getCurrentTask() +{ + if(m_updateStatus == UpdateStatus::InProgress) + { + return m_updateTask; + } + return nullptr; +} + #include "BaseEntity.moc" -- cgit v1.2.3 From f557c1367994a61935fa0e8d0b1b67688d2692d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 19 Mar 2017 23:58:54 +0100 Subject: NOISSUE stuff and things happened. Maybe. --- api/logic/meta/BaseEntity.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'api/logic/meta/BaseEntity.cpp') diff --git a/api/logic/meta/BaseEntity.cpp b/api/logic/meta/BaseEntity.cpp index 809f88cb..633afab2 100644 --- a/api/logic/meta/BaseEntity.cpp +++ b/api/logic/meta/BaseEntity.cpp @@ -101,16 +101,20 @@ bool Meta::BaseEntity::loadLocalFile() void Meta::BaseEntity::load() { + // load local file if nothing is loaded yet if(!isLoaded()) { - loadLocalFile(); + if(loadLocalFile()) + { + m_loadStatus = LoadStatus::Local; + } } + // if we need remote update, run the update task if(!shouldStartRemoteUpdate()) { return; } NetJob *job = new NetJob(QObject::tr("Download of meta file %1").arg(localFilename())); - auto url = this->url(); auto entry = ENV.metacache()->resolveEntry("meta", localFilename()); entry->setStale(true); -- cgit v1.2.3