2019-01-17 01:44:24 +05:30
|
|
|
/* Copyright 2013-2019 MultiMC Contributors
|
2015-02-08 22:26:14 +05:30
|
|
|
*
|
|
|
|
* 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 "DownloadTask.h"
|
|
|
|
|
2015-02-09 06:21:14 +05:30
|
|
|
#include "updater/UpdateChecker.h"
|
2015-02-08 22:26:14 +05:30
|
|
|
#include "GoUpdate.h"
|
2015-02-09 06:21:14 +05:30
|
|
|
#include "net/NetJob.h"
|
2015-02-08 22:26:14 +05:30
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QTemporaryDir>
|
|
|
|
#include <QCryptographicHash>
|
|
|
|
|
|
|
|
namespace GoUpdate
|
|
|
|
{
|
|
|
|
|
2017-05-04 02:41:52 +05:30
|
|
|
DownloadTask::DownloadTask(Status status, QString target, QObject *parent)
|
2018-07-15 18:21:05 +05:30
|
|
|
: Task(parent), m_updateFilesDir(target)
|
2015-02-08 22:26:14 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
m_status = status;
|
2015-02-08 22:26:14 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
m_updateFilesDir.setAutoRemove(false);
|
2015-02-08 22:26:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadTask::executeTask()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
loadVersionInfo();
|
2015-02-08 22:26:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadTask::loadVersionInfo()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
setStatus(tr("Loading version information..."));
|
2015-02-08 22:26:14 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
NetJob *netJob = new NetJob("Version Info");
|
2015-02-08 22:26:14 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// Find the index URL.
|
|
|
|
QUrl newIndexUrl = QUrl(m_status.newRepoUrl).resolved(QString::number(m_status.newVersionId) + ".json");
|
|
|
|
qDebug() << m_status.newRepoUrl << " turns into " << newIndexUrl;
|
2015-02-08 22:26:14 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
netJob->addNetAction(m_newVersionFileListDownload = Net::Download::makeByteArray(newIndexUrl, &newVersionFileListData));
|
2015-02-08 22:26:14 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// If we have a current version URL, get that one too.
|
|
|
|
if (!m_status.currentRepoUrl.isEmpty())
|
|
|
|
{
|
|
|
|
QUrl cIndexUrl = QUrl(m_status.currentRepoUrl).resolved(QString::number(m_status.currentVersionId) + ".json");
|
|
|
|
netJob->addNetAction(m_currentVersionFileListDownload = Net::Download::makeByteArray(cIndexUrl, ¤tVersionFileListData));
|
|
|
|
qDebug() << m_status.currentRepoUrl << " turns into " << cIndexUrl;
|
|
|
|
}
|
2015-02-08 22:26:14 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// connect signals and start the job
|
|
|
|
connect(netJob, &NetJob::succeeded, this, &DownloadTask::processDownloadedVersionInfo);
|
|
|
|
connect(netJob, &NetJob::failed, this, &DownloadTask::vinfoDownloadFailed);
|
|
|
|
m_vinfoNetJob.reset(netJob);
|
|
|
|
netJob->start();
|
2015-02-08 22:26:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadTask::vinfoDownloadFailed()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
// Something failed. We really need the second download (current version info), so parse
|
|
|
|
// downloads anyways as long as the first one succeeded.
|
|
|
|
if (m_newVersionFileListDownload->wasSuccessful())
|
|
|
|
{
|
|
|
|
processDownloadedVersionInfo();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Give a more detailed error message.
|
|
|
|
qCritical() << "Failed to download version info files.";
|
|
|
|
emitFailed(tr("Failed to download version info files."));
|
2015-02-08 22:26:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadTask::processDownloadedVersionInfo()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
VersionFileList m_currentVersionFileList;
|
|
|
|
VersionFileList m_newVersionFileList;
|
|
|
|
|
|
|
|
setStatus(tr("Reading file list for new version..."));
|
|
|
|
qDebug() << "Reading file list for new version...";
|
|
|
|
QString error;
|
|
|
|
if (!parseVersionInfo(newVersionFileListData, m_newVersionFileList, error))
|
|
|
|
{
|
|
|
|
qCritical() << error;
|
|
|
|
emitFailed(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have the current version info, use it.
|
|
|
|
if (m_currentVersionFileListDownload && m_currentVersionFileListDownload->wasSuccessful())
|
|
|
|
{
|
|
|
|
setStatus(tr("Reading file list for current version..."));
|
|
|
|
qDebug() << "Reading file list for current version...";
|
|
|
|
// if this fails, it's not a complete loss.
|
|
|
|
QString error;
|
|
|
|
if(!parseVersionInfo( currentVersionFileListData, m_currentVersionFileList, error))
|
|
|
|
{
|
|
|
|
qDebug() << error << "This is not a fatal error.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need this any more.
|
|
|
|
m_currentVersionFileListDownload.reset();
|
|
|
|
m_newVersionFileListDownload.reset();
|
|
|
|
m_vinfoNetJob.reset();
|
|
|
|
|
|
|
|
setStatus(tr("Processing file lists - figuring out how to install the update..."));
|
|
|
|
|
|
|
|
// make a new netjob for the actual update files
|
|
|
|
NetJobPtr netJob (new NetJob("Update Files"));
|
|
|
|
|
|
|
|
// fill netJob and operationList
|
|
|
|
if (!processFileLists(m_currentVersionFileList, m_newVersionFileList, m_status.rootPath, m_updateFilesDir.path(), netJob, m_operations))
|
|
|
|
{
|
|
|
|
emitFailed(tr("Failed to process update lists..."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now start the download.
|
|
|
|
QObject::connect(netJob.get(), &NetJob::succeeded, this, &DownloadTask::fileDownloadFinished);
|
|
|
|
QObject::connect(netJob.get(), &NetJob::progress, this, &DownloadTask::fileDownloadProgressChanged);
|
|
|
|
QObject::connect(netJob.get(), &NetJob::failed, this, &DownloadTask::fileDownloadFailed);
|
|
|
|
|
|
|
|
setStatus(tr("Downloading %1 update files.").arg(QString::number(netJob->size())));
|
|
|
|
qDebug() << "Begin downloading update files to" << m_updateFilesDir.path();
|
|
|
|
m_filesNetJob = netJob;
|
|
|
|
m_filesNetJob->start();
|
2015-02-08 22:26:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadTask::fileDownloadFinished()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
emitSucceeded();
|
2015-02-08 22:26:14 +05:30
|
|
|
}
|
|
|
|
|
2015-04-26 17:17:14 +05:30
|
|
|
void DownloadTask::fileDownloadFailed(QString reason)
|
2015-02-08 22:26:14 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
qCritical() << "Failed to download update files:" << reason;
|
|
|
|
emitFailed(tr("Failed to download update files: %1").arg(reason));
|
2015-02-08 22:26:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadTask::fileDownloadProgressChanged(qint64 current, qint64 total)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
setProgress(current, total);
|
2015-02-08 22:26:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QString DownloadTask::updateFilesDir()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return m_updateFilesDir.path();
|
2015-02-08 22:26:14 +05:30
|
|
|
}
|
|
|
|
|
2015-06-08 06:13:16 +05:30
|
|
|
OperationList DownloadTask::operations()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return m_operations;
|
2015-06-08 06:13:16 +05:30
|
|
|
}
|
|
|
|
|
2015-02-08 22:26:14 +05:30
|
|
|
}
|