2023-04-18 21:10:06 -04:00
|
|
|
#include "download.h"
|
2023-05-02 20:31:17 -04:00
|
|
|
#include "network.h"
|
2023-06-22 15:44:49 -04:00
|
|
|
#include "modellist.h"
|
2023-06-28 16:05:35 -04:00
|
|
|
#include "mysettings.h"
|
2023-04-18 21:10:06 -04:00
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QDir>
|
2023-04-23 11:28:17 -04:00
|
|
|
#include <QStandardPaths>
|
2023-04-27 13:52:24 -04:00
|
|
|
#include <QSettings>
|
2023-04-18 21:10:06 -04:00
|
|
|
|
|
|
|
class MyDownload: public Download { };
|
|
|
|
Q_GLOBAL_STATIC(MyDownload, downloadInstance)
|
|
|
|
Download *Download::globalInstance()
|
|
|
|
{
|
|
|
|
return downloadInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
Download::Download()
|
|
|
|
: QObject(nullptr)
|
2023-04-23 19:43:20 -04:00
|
|
|
, m_hashAndSave(new HashAndSaveFile)
|
2023-04-18 21:10:06 -04:00
|
|
|
{
|
2023-04-23 19:43:20 -04:00
|
|
|
connect(this, &Download::requestHashAndSave, m_hashAndSave,
|
|
|
|
&HashAndSaveFile::hashAndSave, Qt::QueuedConnection);
|
|
|
|
connect(m_hashAndSave, &HashAndSaveFile::hashAndSaveFinished, this,
|
|
|
|
&Download::handleHashAndSaveFinished, Qt::QueuedConnection);
|
2023-04-24 17:52:19 -04:00
|
|
|
connect(&m_networkManager, &QNetworkAccessManager::sslErrors, this,
|
|
|
|
&Download::handleSslErrors);
|
2023-04-28 10:54:05 -04:00
|
|
|
updateReleaseNotes();
|
2023-05-02 18:24:16 -04:00
|
|
|
m_startTime = QDateTime::currentDateTime();
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
2023-06-28 11:13:33 -04:00
|
|
|
static bool operator==(const ReleaseInfo& lhs, const ReleaseInfo& rhs) {
|
2023-04-28 10:54:05 -04:00
|
|
|
return lhs.version == rhs.version;
|
|
|
|
}
|
|
|
|
|
2023-06-28 11:13:33 -04:00
|
|
|
static bool compareVersions(const QString &a, const QString &b) {
|
2023-04-28 20:30:52 -04:00
|
|
|
QStringList aParts = a.split('.');
|
|
|
|
QStringList bParts = b.split('.');
|
|
|
|
|
|
|
|
for (int i = 0; i < std::min(aParts.size(), bParts.size()); ++i) {
|
|
|
|
int aInt = aParts[i].toInt();
|
|
|
|
int bInt = bParts[i].toInt();
|
|
|
|
|
|
|
|
if (aInt > bInt) {
|
|
|
|
return true;
|
|
|
|
} else if (aInt < bInt) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return aParts.size() > bParts.size();
|
|
|
|
}
|
|
|
|
|
2023-04-28 10:54:05 -04:00
|
|
|
ReleaseInfo Download::releaseInfo() const
|
|
|
|
{
|
|
|
|
const QString currentVersion = QCoreApplication::applicationVersion();
|
|
|
|
if (m_releaseMap.contains(currentVersion))
|
|
|
|
return m_releaseMap.value(currentVersion);
|
|
|
|
return ReleaseInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Download::hasNewerRelease() const
|
|
|
|
{
|
|
|
|
const QString currentVersion = QCoreApplication::applicationVersion();
|
|
|
|
QList<QString> versions = m_releaseMap.keys();
|
|
|
|
std::sort(versions.begin(), versions.end(), compareVersions);
|
|
|
|
if (versions.isEmpty())
|
|
|
|
return false;
|
|
|
|
return compareVersions(versions.first(), currentVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Download::isFirstStart() const
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
settings.sync();
|
|
|
|
QString lastVersionStarted = settings.value("download/lastVersionStarted").toString();
|
|
|
|
bool first = lastVersionStarted != QCoreApplication::applicationVersion();
|
|
|
|
settings.setValue("download/lastVersionStarted", QCoreApplication::applicationVersion());
|
|
|
|
settings.sync();
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Download::updateReleaseNotes()
|
|
|
|
{
|
|
|
|
QUrl jsonUrl("http://gpt4all.io/meta/release.json");
|
|
|
|
QNetworkRequest request(jsonUrl);
|
|
|
|
QSslConfiguration conf = request.sslConfiguration();
|
|
|
|
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
request.setSslConfiguration(conf);
|
|
|
|
QNetworkReply *jsonReply = m_networkManager.get(request);
|
2023-07-11 12:37:21 -04:00
|
|
|
connect(qApp, &QCoreApplication::aboutToQuit, jsonReply, &QNetworkReply::abort);
|
2023-04-28 10:54:05 -04:00
|
|
|
connect(jsonReply, &QNetworkReply::finished, this, &Download::handleReleaseJsonDownloadFinished);
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Download::downloadModel(const QString &modelFile)
|
|
|
|
{
|
2023-06-22 15:44:49 -04:00
|
|
|
QFile *tempFile = new QFile(ModelList::globalInstance()->incompleteDownloadPath(modelFile));
|
2023-05-02 18:24:16 -04:00
|
|
|
QDateTime modTime = tempFile->fileTime(QFile::FileModificationTime);
|
|
|
|
bool success = tempFile->open(QIODevice::WriteOnly | QIODevice::Append);
|
2023-04-23 19:43:20 -04:00
|
|
|
qWarning() << "Opening temp file for writing:" << tempFile->fileName();
|
|
|
|
if (!success) {
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString error
|
|
|
|
= QString("ERROR: Could not open temp file: %1 %2").arg(tempFile->fileName()).arg(modelFile);
|
|
|
|
qWarning() << error;
|
2023-11-21 15:24:42 -05:00
|
|
|
clearRetry(modelFile);
|
2024-03-07 14:27:23 -05:00
|
|
|
QVector<QPair<int, QVariant>> data;
|
|
|
|
data.append(qMakePair(ModelList::DownloadErrorRole, error));
|
|
|
|
ModelList::globalInstance()->updateDataByFilename(modelFile, data);
|
2023-04-23 19:43:20 -04:00
|
|
|
return;
|
|
|
|
}
|
2023-06-22 15:44:49 -04:00
|
|
|
tempFile->flush();
|
2023-05-02 18:24:16 -04:00
|
|
|
size_t incomplete_size = tempFile->size();
|
|
|
|
if (incomplete_size > 0) {
|
2023-06-22 15:44:49 -04:00
|
|
|
bool success = tempFile->seek(incomplete_size);
|
|
|
|
if (!success) {
|
|
|
|
incomplete_size = 0;
|
|
|
|
success = tempFile->seek(incomplete_size);
|
|
|
|
Q_ASSERT(success);
|
2023-05-02 18:24:16 -04:00
|
|
|
}
|
|
|
|
}
|
2023-04-23 19:43:20 -04:00
|
|
|
|
2023-07-05 20:12:37 -04:00
|
|
|
if (!ModelList::globalInstance()->containsByFilename(modelFile)) {
|
2023-06-22 15:44:49 -04:00
|
|
|
qWarning() << "ERROR: Could not find file:" << modelFile;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-07 14:27:23 -05:00
|
|
|
QVector<QPair<int, QVariant>> data;
|
|
|
|
data.append(qMakePair(ModelList::DownloadingRole, true));
|
|
|
|
ModelList::globalInstance()->updateDataByFilename(modelFile, data);
|
2023-07-05 20:12:37 -04:00
|
|
|
ModelInfo info = ModelList::globalInstance()->modelInfoByFilename(modelFile);
|
2024-03-05 11:31:31 -05:00
|
|
|
QString url = !info.url().isEmpty() ? info.url() : "http://gpt4all.io/models/gguf/" + modelFile;
|
2023-05-02 20:31:17 -04:00
|
|
|
Network::globalInstance()->sendDownloadStarted(modelFile);
|
2023-06-04 19:02:43 -04:00
|
|
|
QNetworkRequest request(url);
|
|
|
|
request.setAttribute(QNetworkRequest::User, modelFile);
|
2023-06-22 15:44:49 -04:00
|
|
|
request.setRawHeader("range", QString("bytes=%1-").arg(tempFile->pos()).toUtf8());
|
2023-04-24 00:05:06 -04:00
|
|
|
QSslConfiguration conf = request.sslConfiguration();
|
|
|
|
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
request.setSslConfiguration(conf);
|
2023-04-18 21:10:06 -04:00
|
|
|
QNetworkReply *modelReply = m_networkManager.get(request);
|
2023-07-11 12:37:21 -04:00
|
|
|
connect(qApp, &QCoreApplication::aboutToQuit, modelReply, &QNetworkReply::abort);
|
2023-04-18 21:10:06 -04:00
|
|
|
connect(modelReply, &QNetworkReply::downloadProgress, this, &Download::handleDownloadProgress);
|
2023-11-21 15:24:42 -05:00
|
|
|
connect(modelReply, &QNetworkReply::errorOccurred, this, &Download::handleErrorOccurred);
|
2023-04-18 21:10:06 -04:00
|
|
|
connect(modelReply, &QNetworkReply::finished, this, &Download::handleModelDownloadFinished);
|
2023-04-23 19:43:20 -04:00
|
|
|
connect(modelReply, &QNetworkReply::readyRead, this, &Download::handleReadyRead);
|
|
|
|
m_activeDownloads.insert(modelReply, tempFile);
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Download::cancelDownload(const QString &modelFile)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_activeDownloads.size(); ++i) {
|
2023-04-23 19:43:20 -04:00
|
|
|
QNetworkReply *modelReply = m_activeDownloads.keys().at(i);
|
2023-04-18 21:10:06 -04:00
|
|
|
QUrl url = modelReply->request().url();
|
|
|
|
if (url.toString().endsWith(modelFile)) {
|
2023-05-02 20:31:17 -04:00
|
|
|
Network::globalInstance()->sendDownloadCanceled(modelFile);
|
|
|
|
|
2023-04-18 21:10:06 -04:00
|
|
|
// Disconnect the signals
|
|
|
|
disconnect(modelReply, &QNetworkReply::downloadProgress, this, &Download::handleDownloadProgress);
|
|
|
|
disconnect(modelReply, &QNetworkReply::finished, this, &Download::handleModelDownloadFinished);
|
|
|
|
|
|
|
|
modelReply->abort(); // Abort the download
|
|
|
|
modelReply->deleteLater(); // Schedule the reply for deletion
|
2023-04-23 19:43:20 -04:00
|
|
|
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile = m_activeDownloads.value(modelReply);
|
2023-04-23 19:43:20 -04:00
|
|
|
tempFile->deleteLater();
|
|
|
|
m_activeDownloads.remove(modelReply);
|
2023-04-18 21:10:06 -04:00
|
|
|
|
2024-03-07 14:27:23 -05:00
|
|
|
QVector<QPair<int, QVariant>> data;
|
|
|
|
data.append(qMakePair(ModelList::DownloadingRole, false));
|
|
|
|
ModelList::globalInstance()->updateDataByFilename(modelFile, data);
|
2023-04-18 21:10:06 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-14 20:12:15 -04:00
|
|
|
void Download::installModel(const QString &modelFile, const QString &apiKey)
|
|
|
|
{
|
|
|
|
Q_ASSERT(!apiKey.isEmpty());
|
|
|
|
if (apiKey.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Network::globalInstance()->sendInstallModel(modelFile);
|
2023-07-01 11:34:21 -04:00
|
|
|
QString filePath = MySettings::globalInstance()->modelPath() + modelFile;
|
2023-05-14 20:12:15 -04:00
|
|
|
QFile file(filePath);
|
|
|
|
if (file.open(QIODeviceBase::WriteOnly | QIODeviceBase::Text)) {
|
|
|
|
QTextStream stream(&file);
|
|
|
|
stream << apiKey;
|
|
|
|
file.close();
|
2024-01-22 14:15:13 -05:00
|
|
|
ModelList::globalInstance()->updateModelsFromDirectory();
|
2023-05-14 20:12:15 -04:00
|
|
|
}
|
2024-01-22 14:15:13 -05:00
|
|
|
|
2024-03-07 14:27:23 -05:00
|
|
|
QVector<QPair<int, QVariant>> data;
|
|
|
|
data.append(qMakePair(ModelList::InstalledRole, true));
|
|
|
|
ModelList::globalInstance()->updateDataByFilename(modelFile, data);
|
2023-05-14 20:12:15 -04:00
|
|
|
}
|
|
|
|
|
2023-05-16 09:32:01 -04:00
|
|
|
void Download::removeModel(const QString &modelFile)
|
|
|
|
{
|
2023-06-28 16:05:35 -04:00
|
|
|
const QString filePath = MySettings::globalInstance()->modelPath() + modelFile;
|
2023-06-22 15:44:49 -04:00
|
|
|
QFile incompleteFile(ModelList::globalInstance()->incompleteDownloadPath(modelFile));
|
|
|
|
if (incompleteFile.exists()) {
|
|
|
|
incompleteFile.remove();
|
|
|
|
}
|
|
|
|
|
2024-03-07 14:27:23 -05:00
|
|
|
bool shouldRemoveInstalled = false;
|
2023-05-16 09:32:01 -04:00
|
|
|
QFile file(filePath);
|
2023-06-22 15:44:49 -04:00
|
|
|
if (file.exists()) {
|
2024-03-05 11:31:31 -05:00
|
|
|
const ModelInfo info = ModelList::globalInstance()->modelInfoByFilename(modelFile);
|
2024-03-07 14:27:23 -05:00
|
|
|
shouldRemoveInstalled = info.installed && !info.isClone() && (info.isDiscovered() || info.description() == "");
|
|
|
|
if (shouldRemoveInstalled)
|
|
|
|
ModelList::globalInstance()->removeInstalled(info);
|
2023-06-22 15:44:49 -04:00
|
|
|
Network::globalInstance()->sendRemoveModel(modelFile);
|
|
|
|
file.remove();
|
2023-05-16 09:32:01 -04:00
|
|
|
}
|
|
|
|
|
2024-03-07 14:27:23 -05:00
|
|
|
if (!shouldRemoveInstalled) {
|
|
|
|
QVector<QPair<int, QVariant>> data;
|
|
|
|
data.append(qMakePair(ModelList::InstalledRole, false));
|
|
|
|
data.append(qMakePair(ModelList::BytesReceivedRole, 0));
|
|
|
|
data.append(qMakePair(ModelList::BytesTotalRole, 0));
|
|
|
|
data.append(qMakePair(ModelList::TimestampRole, 0));
|
|
|
|
data.append(qMakePair(ModelList::SpeedRole, QString()));
|
|
|
|
data.append(qMakePair(ModelList::DownloadErrorRole, QString()));
|
|
|
|
ModelList::globalInstance()->updateDataByFilename(modelFile, data);
|
|
|
|
}
|
2023-05-16 09:32:01 -04:00
|
|
|
}
|
|
|
|
|
2023-04-24 17:52:19 -04:00
|
|
|
void Download::handleSslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
|
|
|
|
{
|
|
|
|
QUrl url = reply->request().url();
|
2023-06-02 15:46:41 -04:00
|
|
|
for (const auto &e : errors)
|
2023-04-24 17:52:19 -04:00
|
|
|
qWarning() << "ERROR: Received ssl error:" << e.errorString() << "for" << url;
|
|
|
|
}
|
|
|
|
|
2023-04-28 10:54:05 -04:00
|
|
|
void Download::handleReleaseJsonDownloadFinished()
|
|
|
|
{
|
|
|
|
QNetworkReply *jsonReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!jsonReply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QByteArray jsonData = jsonReply->readAll();
|
|
|
|
jsonReply->deleteLater();
|
|
|
|
parseReleaseJsonFile(jsonData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Download::parseReleaseJsonFile(const QByteArray &jsonData)
|
|
|
|
{
|
|
|
|
QJsonParseError err;
|
|
|
|
QJsonDocument document = QJsonDocument::fromJson(jsonData, &err);
|
|
|
|
if (err.error != QJsonParseError::NoError) {
|
2023-06-22 15:44:49 -04:00
|
|
|
qWarning() << "ERROR: Couldn't parse: " << jsonData << err.errorString();
|
2023-04-28 10:54:05 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonArray jsonArray = document.array();
|
|
|
|
|
|
|
|
m_releaseMap.clear();
|
|
|
|
for (const QJsonValue &value : jsonArray) {
|
|
|
|
QJsonObject obj = value.toObject();
|
|
|
|
|
|
|
|
QString version = obj["version"].toString();
|
|
|
|
QString notes = obj["notes"].toString();
|
|
|
|
QString contributors = obj["contributors"].toString();
|
|
|
|
ReleaseInfo releaseInfo;
|
|
|
|
releaseInfo.version = version;
|
|
|
|
releaseInfo.notes = notes;
|
|
|
|
releaseInfo.contributors = contributors;
|
|
|
|
m_releaseMap.insert(version, releaseInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit hasNewerReleaseChanged();
|
|
|
|
emit releaseInfoChanged();
|
|
|
|
}
|
|
|
|
|
2023-11-21 15:24:42 -05:00
|
|
|
bool Download::hasRetry(const QString &filename) const
|
|
|
|
{
|
|
|
|
return m_activeRetries.contains(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Download::shouldRetry(const QString &filename)
|
|
|
|
{
|
|
|
|
int retries = 0;
|
|
|
|
if (m_activeRetries.contains(filename))
|
|
|
|
retries = m_activeRetries.value(filename);
|
|
|
|
|
|
|
|
++retries;
|
|
|
|
|
|
|
|
// Allow up to ten retries for now
|
|
|
|
if (retries < 10) {
|
|
|
|
m_activeRetries.insert(filename, retries);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Download::clearRetry(const QString &filename)
|
|
|
|
{
|
|
|
|
m_activeRetries.remove(filename);
|
|
|
|
}
|
|
|
|
|
2023-05-02 20:31:17 -04:00
|
|
|
void Download::handleErrorOccurred(QNetworkReply::NetworkError code)
|
|
|
|
{
|
|
|
|
QNetworkReply *modelReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!modelReply)
|
|
|
|
return;
|
|
|
|
|
2023-11-21 15:24:42 -05:00
|
|
|
// This occurs when the user explicitly cancels the download
|
|
|
|
if (code == QNetworkReply::OperationCanceledError)
|
|
|
|
return;
|
|
|
|
|
2023-06-04 19:02:43 -04:00
|
|
|
QString modelFilename = modelReply->request().attribute(QNetworkRequest::User).toString();
|
2023-11-21 15:24:42 -05:00
|
|
|
if (shouldRetry(modelFilename)) {
|
|
|
|
downloadModel(modelFilename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearRetry(modelFilename);
|
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString error
|
|
|
|
= QString("ERROR: Network error occurred attempting to download %1 code: %2 errorString %3")
|
|
|
|
.arg(modelFilename)
|
|
|
|
.arg(code)
|
|
|
|
.arg(modelReply->errorString());
|
|
|
|
qWarning() << error;
|
2024-03-07 14:27:23 -05:00
|
|
|
QVector<QPair<int, QVariant>> data;
|
|
|
|
data.append(qMakePair(ModelList::DownloadErrorRole, error));
|
|
|
|
ModelList::globalInstance()->updateDataByFilename(modelFilename, data);
|
2023-05-02 20:31:17 -04:00
|
|
|
Network::globalInstance()->sendDownloadError(modelFilename, (int)code, modelReply->errorString());
|
|
|
|
cancelDownload(modelFilename);
|
|
|
|
}
|
2023-04-28 10:54:05 -04:00
|
|
|
|
2023-04-18 21:10:06 -04:00
|
|
|
void Download::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
|
|
|
{
|
|
|
|
QNetworkReply *modelReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!modelReply)
|
|
|
|
return;
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile = m_activeDownloads.value(modelReply);
|
|
|
|
if (!tempFile)
|
|
|
|
return;
|
|
|
|
QString contentRange = modelReply->rawHeader("content-range");
|
|
|
|
if (contentRange.contains("/")) {
|
|
|
|
QString contentTotalSize = contentRange.split("/").last();
|
|
|
|
bytesTotal = contentTotalSize.toLongLong();
|
|
|
|
}
|
2023-04-18 21:10:06 -04:00
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString modelFilename = modelReply->request().attribute(QNetworkRequest::User).toString();
|
2023-07-05 20:12:37 -04:00
|
|
|
const qint64 lastUpdate = ModelList::globalInstance()->dataByFilename(modelFilename, ModelList::TimestampRole).toLongLong();
|
2023-06-22 15:44:49 -04:00
|
|
|
const qint64 currentUpdate = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
if (currentUpdate - lastUpdate < 1000)
|
|
|
|
return;
|
|
|
|
|
2023-07-05 20:12:37 -04:00
|
|
|
const qint64 lastBytesReceived = ModelList::globalInstance()->dataByFilename(modelFilename, ModelList::BytesReceivedRole).toLongLong();
|
2023-06-22 15:44:49 -04:00
|
|
|
const qint64 currentBytesReceived = tempFile->pos();
|
|
|
|
|
|
|
|
qint64 timeDifference = currentUpdate - lastUpdate;
|
|
|
|
qint64 bytesDifference = currentBytesReceived - lastBytesReceived;
|
|
|
|
qint64 speed = (bytesDifference / timeDifference) * 1000; // bytes per second
|
|
|
|
QString speedText;
|
|
|
|
if (speed < 1024)
|
|
|
|
speedText = QString::number(static_cast<double>(speed), 'f', 2) + " B/s";
|
|
|
|
else if (speed < 1024 * 1024)
|
|
|
|
speedText = QString::number(static_cast<double>(speed / 1024.0), 'f', 2) + " KB/s";
|
|
|
|
else
|
|
|
|
speedText = QString::number(static_cast<double>(speed / (1024.0 * 1024.0)), 'f', 2) + " MB/s";
|
|
|
|
|
2024-03-07 14:27:23 -05:00
|
|
|
QVector<QPair<int, QVariant>> data;
|
|
|
|
data.append(qMakePair(ModelList::BytesReceivedRole, currentBytesReceived));
|
|
|
|
data.append(qMakePair(ModelList::BytesTotalRole, bytesTotal));
|
|
|
|
data.append(qMakePair(ModelList::SpeedRole, speedText));
|
|
|
|
data.append(qMakePair(ModelList::TimestampRole, currentUpdate));
|
|
|
|
ModelList::globalInstance()->updateDataByFilename(modelFilename, data);
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
2023-04-23 19:43:20 -04:00
|
|
|
HashAndSaveFile::HashAndSaveFile()
|
|
|
|
: QObject(nullptr)
|
2023-04-18 21:10:06 -04:00
|
|
|
{
|
2023-04-23 19:43:20 -04:00
|
|
|
moveToThread(&m_hashAndSaveThread);
|
|
|
|
m_hashAndSaveThread.setObjectName("hashandsave thread");
|
|
|
|
m_hashAndSaveThread.start();
|
|
|
|
}
|
2023-04-18 21:10:06 -04:00
|
|
|
|
2024-03-05 11:31:31 -05:00
|
|
|
void HashAndSaveFile::hashAndSave(const QString &expectedHash, QCryptographicHash::Algorithm a,
|
|
|
|
const QString &saveFilePath, QFile *tempFile, QNetworkReply *modelReply)
|
2023-04-23 19:43:20 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(!tempFile->isOpen());
|
2023-06-04 19:02:43 -04:00
|
|
|
QString modelFilename = modelReply->request().attribute(QNetworkRequest::User).toString();
|
2023-04-18 21:10:06 -04:00
|
|
|
|
2023-04-23 19:43:20 -04:00
|
|
|
// Reopen the tempFile for hashing
|
2023-05-02 18:24:16 -04:00
|
|
|
if (!tempFile->open(QIODevice::ReadOnly)) {
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString error
|
|
|
|
= QString("ERROR: Could not open temp file for hashing: %1 %2").arg(tempFile->fileName()).arg(modelFilename);
|
|
|
|
qWarning() << error;
|
|
|
|
emit hashAndSaveFinished(false, error, tempFile, modelReply);
|
2023-04-18 21:10:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-05 11:31:31 -05:00
|
|
|
QCryptographicHash hash(a);
|
2023-04-23 19:43:20 -04:00
|
|
|
while(!tempFile->atEnd())
|
|
|
|
hash.addData(tempFile->read(16384));
|
2024-03-05 11:31:31 -05:00
|
|
|
if (hash.result().toHex() != expectedHash.toLatin1()) {
|
2023-04-23 19:43:20 -04:00
|
|
|
tempFile->close();
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString error
|
2024-03-05 11:31:31 -05:00
|
|
|
= QString("ERROR: Download error hash did not match: %1 != %2 for %3")
|
|
|
|
.arg(hash.result().toHex())
|
|
|
|
.arg(expectedHash.toLatin1())
|
|
|
|
.arg(modelFilename);
|
2023-06-22 15:44:49 -04:00
|
|
|
qWarning() << error;
|
2023-05-02 18:24:16 -04:00
|
|
|
tempFile->remove();
|
2023-06-22 15:44:49 -04:00
|
|
|
emit hashAndSaveFinished(false, error, tempFile, modelReply);
|
2023-04-23 19:43:20 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The file save needs the tempFile closed
|
|
|
|
tempFile->close();
|
|
|
|
|
2023-04-26 20:57:25 -04:00
|
|
|
// Attempt to *move* the verified tempfile into place - this should be atomic
|
|
|
|
// but will only work if the destination is on the same filesystem
|
|
|
|
if (tempFile->rename(saveFilePath)) {
|
2023-06-22 15:44:49 -04:00
|
|
|
emit hashAndSaveFinished(true, QString(), tempFile, modelReply);
|
2023-11-21 15:24:42 -05:00
|
|
|
ModelList::globalInstance()->updateModelsFromDirectory();
|
2023-04-26 20:57:25 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-23 19:43:20 -04:00
|
|
|
// Reopen the tempFile for copying
|
2023-05-02 18:24:16 -04:00
|
|
|
if (!tempFile->open(QIODevice::ReadOnly)) {
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString error
|
|
|
|
= QString("ERROR: Could not open temp file at finish: %1 %2").arg(tempFile->fileName()).arg(modelFilename);
|
|
|
|
qWarning() << error;
|
|
|
|
emit hashAndSaveFinished(false, error, tempFile, modelReply);
|
2023-04-18 21:10:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the model file to disk
|
2023-04-23 19:43:20 -04:00
|
|
|
QFile file(saveFilePath);
|
2023-04-18 21:10:06 -04:00
|
|
|
if (file.open(QIODevice::WriteOnly)) {
|
2023-04-23 19:43:20 -04:00
|
|
|
QByteArray buffer;
|
|
|
|
while (!tempFile->atEnd()) {
|
|
|
|
buffer = tempFile->read(16384);
|
|
|
|
file.write(buffer);
|
|
|
|
}
|
2023-04-18 21:10:06 -04:00
|
|
|
file.close();
|
2023-04-23 19:43:20 -04:00
|
|
|
tempFile->close();
|
2023-06-22 15:44:49 -04:00
|
|
|
emit hashAndSaveFinished(true, QString(), tempFile, modelReply);
|
2023-04-23 11:28:17 -04:00
|
|
|
} else {
|
|
|
|
QFile::FileError error = file.error();
|
2023-06-22 15:44:49 -04:00
|
|
|
const QString errorString
|
2023-06-26 09:35:29 -04:00
|
|
|
= QString("ERROR: Could not save model to location: %1 failed with code %1").arg(saveFilePath).arg(error);
|
2023-06-22 15:44:49 -04:00
|
|
|
qWarning() << errorString;
|
2023-04-23 19:43:20 -04:00
|
|
|
tempFile->close();
|
2023-06-22 15:44:49 -04:00
|
|
|
emit hashAndSaveFinished(false, errorString, tempFile, modelReply);
|
2023-04-23 19:43:20 -04:00
|
|
|
}
|
2023-11-17 13:27:17 -05:00
|
|
|
|
|
|
|
ModelList::globalInstance()->updateModelsFromDirectory();
|
2023-04-23 19:43:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Download::handleModelDownloadFinished()
|
|
|
|
{
|
|
|
|
QNetworkReply *modelReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!modelReply)
|
|
|
|
return;
|
|
|
|
|
2023-06-04 19:02:43 -04:00
|
|
|
QString modelFilename = modelReply->request().attribute(QNetworkRequest::User).toString();
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile = m_activeDownloads.value(modelReply);
|
2023-04-23 19:43:20 -04:00
|
|
|
m_activeDownloads.remove(modelReply);
|
|
|
|
|
|
|
|
if (modelReply->error()) {
|
2023-06-26 09:35:29 -04:00
|
|
|
const QString errorString
|
|
|
|
= QString("ERROR: Downloading failed with code %1 \"%2\"").arg(modelReply->error()).arg(modelReply->errorString());
|
|
|
|
qWarning() << errorString;
|
2023-04-23 11:28:17 -04:00
|
|
|
modelReply->deleteLater();
|
2023-04-23 19:43:20 -04:00
|
|
|
tempFile->deleteLater();
|
2023-11-21 15:24:42 -05:00
|
|
|
if (!hasRetry(modelFilename)) {
|
2024-03-07 14:27:23 -05:00
|
|
|
QVector<QPair<int, QVariant>> data;
|
|
|
|
data.append(qMakePair(ModelList::DownloadingRole, false));
|
|
|
|
data.append(qMakePair(ModelList::DownloadErrorRole, errorString));
|
|
|
|
ModelList::globalInstance()->updateDataByFilename(modelFilename, data);
|
2023-11-21 15:24:42 -05:00
|
|
|
}
|
2023-04-23 11:28:17 -04:00
|
|
|
return;
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
2023-11-21 15:24:42 -05:00
|
|
|
clearRetry(modelFilename);
|
|
|
|
|
2023-04-23 19:43:20 -04:00
|
|
|
// The hash and save needs the tempFile closed
|
|
|
|
tempFile->close();
|
2023-04-18 21:10:06 -04:00
|
|
|
|
2023-07-05 20:12:37 -04:00
|
|
|
if (!ModelList::globalInstance()->containsByFilename(modelFilename)) {
|
2023-06-22 15:44:49 -04:00
|
|
|
qWarning() << "ERROR: downloading no such file:" << modelFilename;
|
|
|
|
modelReply->deleteLater();
|
|
|
|
tempFile->deleteLater();
|
|
|
|
return;
|
|
|
|
}
|
2023-04-23 19:43:20 -04:00
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
// Notify that we are calculating hash
|
2024-03-07 14:27:23 -05:00
|
|
|
QVector<QPair<int, QVariant>> data;
|
|
|
|
data.append(qMakePair(ModelList::CalcHashRole, true));
|
|
|
|
ModelList::globalInstance()->updateDataByFilename(modelFilename, data);
|
2024-03-05 11:31:31 -05:00
|
|
|
QByteArray hash = ModelList::globalInstance()->modelInfoByFilename(modelFilename).hash;
|
|
|
|
ModelInfo::HashAlgorithm hashAlgorithm = ModelList::globalInstance()->modelInfoByFilename(modelFilename).hashAlgorithm;
|
2023-06-28 16:05:35 -04:00
|
|
|
const QString saveFilePath = MySettings::globalInstance()->modelPath() + modelFilename;
|
2024-03-05 11:31:31 -05:00
|
|
|
emit requestHashAndSave(hash,
|
|
|
|
(hashAlgorithm == ModelInfo::Md5 ? QCryptographicHash::Md5 : QCryptographicHash::Sha256),
|
|
|
|
saveFilePath, tempFile, modelReply);
|
2023-04-23 19:43:20 -04:00
|
|
|
}
|
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
void Download::handleHashAndSaveFinished(bool success, const QString &error,
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile, QNetworkReply *modelReply)
|
2023-04-23 19:43:20 -04:00
|
|
|
{
|
|
|
|
// The hash and save should send back with tempfile closed
|
|
|
|
Q_ASSERT(!tempFile->isOpen());
|
2023-06-04 19:02:43 -04:00
|
|
|
QString modelFilename = modelReply->request().attribute(QNetworkRequest::User).toString();
|
2023-05-02 20:31:17 -04:00
|
|
|
Network::globalInstance()->sendDownloadFinished(modelFilename, success);
|
2024-03-07 14:27:23 -05:00
|
|
|
|
|
|
|
QVector<QPair<int, QVariant>> data;
|
|
|
|
data.append(qMakePair(ModelList::CalcHashRole, false));
|
|
|
|
data.append(qMakePair(ModelList::DownloadingRole, false));
|
|
|
|
|
2023-04-23 19:43:20 -04:00
|
|
|
modelReply->deleteLater();
|
|
|
|
tempFile->deleteLater();
|
2023-06-22 15:44:49 -04:00
|
|
|
|
2024-03-05 11:31:31 -05:00
|
|
|
if (!success) {
|
2024-03-07 14:27:23 -05:00
|
|
|
data.append(qMakePair(ModelList::DownloadErrorRole, error));
|
2024-03-05 11:31:31 -05:00
|
|
|
} else {
|
2024-03-07 14:27:23 -05:00
|
|
|
data.append(qMakePair(ModelList::DownloadErrorRole, QString()));
|
2024-03-05 11:31:31 -05:00
|
|
|
ModelInfo info = ModelList::globalInstance()->modelInfoByFilename(modelFilename);
|
|
|
|
if (info.isDiscovered())
|
|
|
|
ModelList::globalInstance()->updateDiscoveredInstalled(info);
|
|
|
|
}
|
2024-03-07 14:27:23 -05:00
|
|
|
|
|
|
|
ModelList::globalInstance()->updateDataByFilename(modelFilename, data);
|
2023-04-23 19:43:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Download::handleReadyRead()
|
|
|
|
{
|
|
|
|
QNetworkReply *modelReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!modelReply)
|
|
|
|
return;
|
|
|
|
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile = m_activeDownloads.value(modelReply);
|
2023-04-23 19:43:20 -04:00
|
|
|
QByteArray buffer;
|
|
|
|
while (!modelReply->atEnd()) {
|
|
|
|
buffer = modelReply->read(16384);
|
|
|
|
tempFile->write(buffer);
|
|
|
|
}
|
2023-05-02 18:24:16 -04:00
|
|
|
tempFile->flush();
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|