diff options
| author | Petr Mrázek <peterix@gmail.com> | 2018-07-15 14:51:05 +0200 |
|---|---|---|
| committer | Petr Mrázek <peterix@gmail.com> | 2018-07-15 14:51:05 +0200 |
| commit | bbb3b3e6f6e3c0f95873f22e6d0a4aaf350f49d9 (patch) | |
| tree | e6497e304b7b9368367565fbc7c06efab1124b1c /api/logic/net | |
| parent | 03280cc62e75f8073f8d3d9e9e3952acf21fa77d (diff) | |
| download | PrismLauncher-bbb3b3e6f6e3c0f95873f22e6d0a4aaf350f49d9.tar.gz PrismLauncher-bbb3b3e6f6e3c0f95873f22e6d0a4aaf350f49d9.tar.bz2 PrismLauncher-bbb3b3e6f6e3c0f95873f22e6d0a4aaf350f49d9.zip | |
NOISSUE tabs -> spaces
Diffstat (limited to 'api/logic/net')
| -rw-r--r-- | api/logic/net/ByteArraySink.h | 88 | ||||
| -rw-r--r-- | api/logic/net/ChecksumValidator.h | 76 | ||||
| -rw-r--r-- | api/logic/net/Download.cpp | 406 | ||||
| -rw-r--r-- | api/logic/net/Download.h | 60 | ||||
| -rw-r--r-- | api/logic/net/FileSink.cpp | 138 | ||||
| -rw-r--r-- | api/logic/net/FileSink.h | 24 | ||||
| -rw-r--r-- | api/logic/net/HttpMetaCache.cpp | 366 | ||||
| -rw-r--r-- | api/logic/net/HttpMetaCache.h | 152 | ||||
| -rw-r--r-- | api/logic/net/MetaCacheSink.cpp | 74 | ||||
| -rw-r--r-- | api/logic/net/MetaCacheSink.h | 14 | ||||
| -rw-r--r-- | api/logic/net/Mode.h | 4 | ||||
| -rw-r--r-- | api/logic/net/NetAction.h | 132 | ||||
| -rw-r--r-- | api/logic/net/NetJob.cpp | 312 | ||||
| -rw-r--r-- | api/logic/net/NetJob.h | 96 | ||||
| -rw-r--r-- | api/logic/net/PasteUpload.cpp | 128 | ||||
| -rw-r--r-- | api/logic/net/PasteUpload.h | 62 | ||||
| -rw-r--r-- | api/logic/net/Sink.h | 102 | ||||
| -rw-r--r-- | api/logic/net/URLConstants.cpp | 4 | ||||
| -rw-r--r-- | api/logic/net/Validator.h | 12 |
19 files changed, 1125 insertions, 1125 deletions
diff --git a/api/logic/net/ByteArraySink.h b/api/logic/net/ByteArraySink.h index 03b77fcc..20e6764c 100644 --- a/api/logic/net/ByteArraySink.h +++ b/api/logic/net/ByteArraySink.h @@ -9,54 +9,54 @@ namespace Net { class ByteArraySink : public Sink { public: - ByteArraySink(QByteArray *output) - :m_output(output) - { - // nil - }; + ByteArraySink(QByteArray *output) + :m_output(output) + { + // nil + }; - virtual ~ByteArraySink() - { - // 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; - } + 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; + QByteArray * m_output; }; } diff --git a/api/logic/net/ChecksumValidator.h b/api/logic/net/ChecksumValidator.h index 187e4b30..0d6b19c2 100644 --- a/api/logic/net/ChecksumValidator.h +++ b/api/logic/net/ChecksumValidator.h @@ -9,47 +9,47 @@ namespace Net { class ChecksumValidator: public Validator { public: /* con/des */ - ChecksumValidator(QCryptographicHash::Algorithm algorithm, QByteArray expected = QByteArray()) - :m_checksum(algorithm), m_expected(expected) - { - }; - virtual ~ChecksumValidator() {}; + 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; - } + 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; + 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 index 631d3076..fc634e3d 100644 --- a/api/logic/net/Download.cpp +++ b/api/logic/net/Download.cpp @@ -28,271 +28,271 @@ namespace Net { Download::Download():NetAction() { - m_status = Job_NotStarted; + 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 * 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 * 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); + 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); + 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; - } + 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, "MultiMC/5.0"); + request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0"); - QNetworkReply *rep = ENV.qnam().get(request); + 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); + 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); + 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; - } + 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++; - } + 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); + 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 the URL begins with //, we need to insert the URL scheme. - * See: https://bugreports.qt-project.org/browse/QTBUG-41061 - */ - if(redirectStr.startsWith("//")) - { - redirectStr = m_reply->url().scheme() + ":" + redirectStr; - } + /* + * IF the URL begins with //, we need to insert the URL scheme. + * See: https://bugreports.qt-project.org/browse/QTBUG-41061 + */ + if(redirectStr.startsWith("//")) + { + redirectStr = m_reply->url().scheme() + ":" + redirectStr; + } - /* - * 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; - } + /* + * 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; + 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; - } + // 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; - } + // 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); - } + // 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); + // 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; - } + 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; + if(m_reply) + { + m_reply->abort(); + } + else + { + m_status = Job_Aborted; + } + return true; } bool Net::Download::canAbort() { - return true; + return true; } diff --git a/api/logic/net/Download.h b/api/logic/net/Download.h index 00bf108c..9d9d7743 100644 --- a/api/logic/net/Download.h +++ b/api/logic/net/Download.h @@ -24,52 +24,52 @@ namespace Net { class MULTIMC_LOGIC_EXPORT Download : public NetAction { - Q_OBJECT + Q_OBJECT public: /* types */ - typedef std::shared_ptr<class Download> Ptr; - enum class Option - { - NoOptions = 0, - AcceptLocalFiles = 1 - }; - Q_DECLARE_FLAGS(Options, Option) + typedef std::shared_ptr<class Download> Ptr; + enum class Option + { + NoOptions = 0, + AcceptLocalFiles = 1 + }; + Q_DECLARE_FLAGS(Options, Option) protected: /* con/des */ - explicit Download(); + 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); + 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; + QString getTargetFilepath() + { + return m_target_path; + } + void addValidator(Validator * v); + bool abort() override; + bool canAbort() override; private: /* methods */ - bool handleRedirect(); + bool handleRedirect(); protected slots: - void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) override; - void downloadError(QNetworkReply::NetworkError error) override; + 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; + void downloadFinished() override; + void downloadReadyRead() override; public slots: - void start() override; + 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; + // FIXME: remove this, it has no business being here. + QString m_target_path; + std::unique_ptr<Sink> m_sink; + Options m_options; }; } diff --git a/api/logic/net/FileSink.cpp b/api/logic/net/FileSink.cpp index e3ff0498..8b3e917d 100644 --- a/api/logic/net/FileSink.cpp +++ b/api/logic/net/FileSink.cpp @@ -7,109 +7,109 @@ namespace Net { FileSink::FileSink(QString filename) - :m_filename(filename) + :m_filename(filename) { - // nil + // nil } FileSink::~FileSink() { - // nil + // 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; - } + 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; + if(initAllValidators(request)) + return Job_InProgress; + return Job_Failed; } JobStatus FileSink::initCache(QNetworkRequest &) { - return Job_InProgress; + 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; + 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; + 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(); + 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); + return finalizeCache(reply); } JobStatus FileSink::finalizeCache(QNetworkReply &) { - return Job_Finished; + return Job_Finished; } bool FileSink::hasLocalData() { - QFileInfo info(m_filename); - return info.exists() && info.size() != 0; + QFileInfo info(m_filename); + return info.exists() && info.size() != 0; } } diff --git a/api/logic/net/FileSink.h b/api/logic/net/FileSink.h index 035d5bfd..875fe511 100644 --- a/api/logic/net/FileSink.h +++ b/api/logic/net/FileSink.h @@ -6,23 +6,23 @@ namespace Net { class FileSink : public Sink { public: /* con/des */ - FileSink(QString filename); - virtual ~FileSink(); + 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; + JobStatus init(QNetworkRequest & request) ove |
