2023-04-18 21:10:06 -04:00
|
|
|
#include "download.h"
|
2023-05-02 20:31:17 -04:00
|
|
|
#include "network.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-25 10:57:40 -04:00
|
|
|
connect(this, &Download::downloadLocalModelsPathChanged, this, &Download::updateModelList);
|
2023-04-18 21:10:06 -04:00
|
|
|
updateModelList();
|
2023-04-28 10:54:05 -04:00
|
|
|
updateReleaseNotes();
|
2023-04-27 16:27:53 -04:00
|
|
|
QSettings settings;
|
|
|
|
settings.sync();
|
|
|
|
m_downloadLocalModelsPath = settings.value("modelPath",
|
|
|
|
defaultLocalModelsPath()).toString();
|
2023-05-02 18:24:16 -04:00
|
|
|
m_startTime = QDateTime::currentDateTime();
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
2023-04-27 14:52:40 -04:00
|
|
|
bool operator==(const ModelInfo& lhs, const ModelInfo& rhs) {
|
|
|
|
return lhs.filename == rhs.filename && lhs.md5sum == rhs.md5sum;
|
|
|
|
}
|
|
|
|
|
2023-04-28 10:54:05 -04:00
|
|
|
bool operator==(const ReleaseInfo& lhs, const ReleaseInfo& rhs) {
|
|
|
|
return lhs.version == rhs.version;
|
|
|
|
}
|
|
|
|
|
2023-04-28 20:30:52 -04:00
|
|
|
bool compareVersions(const QString &a, const QString &b) {
|
|
|
|
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-18 21:10:06 -04:00
|
|
|
QList<ModelInfo> Download::modelList() const
|
|
|
|
{
|
|
|
|
// We make sure the default model is listed first
|
|
|
|
QList<ModelInfo> values = m_modelMap.values();
|
2023-04-28 20:30:52 -04:00
|
|
|
const QString currentVersion = QCoreApplication::applicationVersion();
|
2023-04-27 14:52:40 -04:00
|
|
|
|
2023-04-18 21:10:06 -04:00
|
|
|
ModelInfo defaultInfo;
|
2023-04-27 14:52:40 -04:00
|
|
|
ModelInfo bestGPTJInfo;
|
|
|
|
ModelInfo bestLlamaInfo;
|
2023-04-28 20:30:52 -04:00
|
|
|
QList<ModelInfo> filtered;
|
2023-04-18 21:10:06 -04:00
|
|
|
for (ModelInfo v : values) {
|
2023-04-28 20:30:52 -04:00
|
|
|
if (!v.requires.isEmpty()
|
|
|
|
&& v.requires != currentVersion
|
|
|
|
&& compareVersions(v.requires, currentVersion)) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-04-27 14:52:40 -04:00
|
|
|
if (v.isDefault)
|
2023-04-18 21:10:06 -04:00
|
|
|
defaultInfo = v;
|
2023-04-27 14:52:40 -04:00
|
|
|
if (v.bestGPTJ)
|
|
|
|
bestGPTJInfo = v;
|
|
|
|
if (v.bestLlama)
|
|
|
|
bestLlamaInfo = v;
|
2023-04-28 20:30:52 -04:00
|
|
|
filtered.append(v);
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
2023-04-27 14:52:40 -04:00
|
|
|
|
|
|
|
Q_ASSERT(defaultInfo == bestGPTJInfo || defaultInfo == bestLlamaInfo);
|
|
|
|
|
2023-04-28 20:30:52 -04:00
|
|
|
filtered.removeAll(bestLlamaInfo);
|
|
|
|
filtered.prepend(bestLlamaInfo);
|
2023-04-27 14:52:40 -04:00
|
|
|
|
2023-04-28 20:30:52 -04:00
|
|
|
filtered.removeAll(bestGPTJInfo);
|
|
|
|
filtered.prepend(bestGPTJInfo);
|
2023-04-27 14:52:40 -04:00
|
|
|
|
2023-04-28 20:30:52 -04:00
|
|
|
return filtered;
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-04-25 10:57:40 -04:00
|
|
|
QString Download::downloadLocalModelsPath() const {
|
|
|
|
return m_downloadLocalModelsPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Download::setDownloadLocalModelsPath(const QString &modelPath) {
|
|
|
|
QString filePath = (modelPath.startsWith("file://") ?
|
|
|
|
QUrl(modelPath).toLocalFile() : modelPath);
|
2023-04-29 21:02:54 -04:00
|
|
|
QString canonical = QFileInfo(filePath).canonicalFilePath() + "/";
|
2023-04-25 10:57:40 -04:00
|
|
|
if (m_downloadLocalModelsPath != canonical) {
|
|
|
|
m_downloadLocalModelsPath = canonical;
|
|
|
|
emit downloadLocalModelsPathChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 10:54:05 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-05-02 18:24:16 -04:00
|
|
|
QString Download::incompleteDownloadPath(const QString &modelFile) {
|
|
|
|
QString downloadPath = downloadLocalModelsPath() + "incomplete-" +
|
|
|
|
modelFile;
|
|
|
|
return downloadPath;
|
|
|
|
}
|
|
|
|
|
2023-04-25 10:57:40 -04:00
|
|
|
QString Download::defaultLocalModelsPath() const
|
2023-04-23 11:28:17 -04:00
|
|
|
{
|
2023-04-24 11:14:23 -04:00
|
|
|
QString localPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)
|
2023-04-29 21:02:54 -04:00
|
|
|
+ "/";
|
2023-04-24 11:14:23 -04:00
|
|
|
QString testWritePath = localPath + QString("test_write.txt");
|
2023-04-29 21:02:54 -04:00
|
|
|
QString canonicalLocalPath = QFileInfo(localPath).canonicalFilePath() + "/";
|
2023-04-24 11:14:23 -04:00
|
|
|
QDir localDir(localPath);
|
|
|
|
if (!localDir.exists()) {
|
|
|
|
if (!localDir.mkpath(localPath)) {
|
|
|
|
qWarning() << "ERROR: Local download directory can't be created:" << canonicalLocalPath;
|
|
|
|
return canonicalLocalPath;
|
2023-04-24 09:07:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 11:14:23 -04:00
|
|
|
if (QFileInfo::exists(testWritePath))
|
|
|
|
return canonicalLocalPath;
|
2023-04-24 09:07:28 -04:00
|
|
|
|
2023-04-24 11:14:23 -04:00
|
|
|
QFile testWriteFile(testWritePath);
|
|
|
|
if (testWriteFile.open(QIODeviceBase::ReadWrite)) {
|
|
|
|
testWriteFile.close();
|
2023-04-24 09:07:28 -04:00
|
|
|
return canonicalLocalPath;
|
|
|
|
}
|
2023-04-24 11:14:23 -04:00
|
|
|
|
|
|
|
qWarning() << "ERROR: Local download path appears not writeable:" << canonicalLocalPath;
|
|
|
|
return canonicalLocalPath;
|
2023-04-23 11:28:17 -04:00
|
|
|
}
|
|
|
|
|
2023-04-18 21:10:06 -04:00
|
|
|
void Download::updateModelList()
|
|
|
|
{
|
|
|
|
QUrl jsonUrl("http://gpt4all.io/models/models.json");
|
|
|
|
QNetworkRequest request(jsonUrl);
|
2023-04-24 00:03:01 -04:00
|
|
|
QSslConfiguration conf = request.sslConfiguration();
|
|
|
|
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
request.setSslConfiguration(conf);
|
2023-04-18 21:10:06 -04:00
|
|
|
QNetworkReply *jsonReply = m_networkManager.get(request);
|
2023-04-28 10:54:05 -04:00
|
|
|
connect(jsonReply, &QNetworkReply::finished, this, &Download::handleModelsJsonDownloadFinished);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
connect(jsonReply, &QNetworkReply::finished, this, &Download::handleReleaseJsonDownloadFinished);
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Download::downloadModel(const QString &modelFile)
|
|
|
|
{
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile = new QFile(incompleteDownloadPath(modelFile));
|
|
|
|
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) {
|
|
|
|
qWarning() << "ERROR: Could not open temp file:"
|
|
|
|
<< tempFile->fileName() << modelFile;
|
|
|
|
return;
|
|
|
|
}
|
2023-05-02 18:24:16 -04:00
|
|
|
size_t incomplete_size = tempFile->size();
|
|
|
|
if (incomplete_size > 0) {
|
|
|
|
if (modTime < m_startTime) {
|
|
|
|
qWarning() << "File last modified before app started, rewinding by 1MB";
|
|
|
|
if (incomplete_size >= 1024 * 1024) {
|
|
|
|
incomplete_size -= 1024 * 1024;
|
|
|
|
} else {
|
|
|
|
incomplete_size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tempFile->seek(incomplete_size);
|
|
|
|
}
|
2023-04-23 19:43:20 -04:00
|
|
|
|
2023-05-02 20:31:17 -04:00
|
|
|
Network::globalInstance()->sendDownloadStarted(modelFile);
|
2023-04-18 21:10:06 -04:00
|
|
|
QNetworkRequest request("http://gpt4all.io/models/" + modelFile);
|
2023-05-02 18:24:16 -04:00
|
|
|
request.setRawHeader("range", QString("bytes=%1-").arg(incomplete_size).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);
|
|
|
|
connect(modelReply, &QNetworkReply::downloadProgress, this, &Download::handleDownloadProgress);
|
|
|
|
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
|
|
|
|
|
|
|
// Emit downloadFinished signal for cleanup
|
|
|
|
emit downloadFinished(modelFile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 17:52:19 -04:00
|
|
|
void Download::handleSslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
|
|
|
|
{
|
|
|
|
QUrl url = reply->request().url();
|
|
|
|
for (auto e : errors)
|
|
|
|
qWarning() << "ERROR: Received ssl error:" << e.errorString() << "for" << url;
|
|
|
|
}
|
|
|
|
|
2023-04-28 10:54:05 -04:00
|
|
|
void Download::handleModelsJsonDownloadFinished()
|
2023-04-18 21:10:06 -04:00
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
QByteArray jsonData = QString(""
|
|
|
|
"["
|
|
|
|
" {"
|
|
|
|
" \"md5sum\": \"61d48a82cb188cceb14ebb8082bfec37\","
|
2023-04-23 23:54:49 -04:00
|
|
|
" \"filename\": \"ggml-gpt4all-j-v1.1-breezy.bin\","
|
2023-04-20 09:32:51 -04:00
|
|
|
" \"filesize\": \"3785248281\""
|
2023-04-18 21:10:06 -04:00
|
|
|
" },"
|
|
|
|
" {"
|
|
|
|
" \"md5sum\": \"879344aaa9d62fdccbda0be7a09e7976\","
|
|
|
|
" \"filename\": \"ggml-gpt4all-j-v1.2-jazzy.bin\","
|
2023-04-23 23:54:49 -04:00
|
|
|
" \"filesize\": \"3785248281\","
|
2023-04-18 21:10:06 -04:00
|
|
|
" \"isDefault\": \"true\""
|
|
|
|
" },"
|
|
|
|
" {"
|
|
|
|
" \"md5sum\": \"5b5a3f9b858d33b29b52b89692415595\","
|
2023-04-23 23:54:49 -04:00
|
|
|
" \"filesize\": \"3785248281\","
|
2023-04-18 21:10:06 -04:00
|
|
|
" \"filename\": \"ggml-gpt4all-j.bin\""
|
|
|
|
" }"
|
|
|
|
"]"
|
|
|
|
).toUtf8();
|
|
|
|
printf("%s\n", jsonData.toStdString().c_str());
|
|
|
|
fflush(stdout);
|
|
|
|
#else
|
|
|
|
QNetworkReply *jsonReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!jsonReply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QByteArray jsonData = jsonReply->readAll();
|
|
|
|
jsonReply->deleteLater();
|
|
|
|
#endif
|
2023-04-28 10:54:05 -04:00
|
|
|
parseModelsJsonFile(jsonData);
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
2023-04-28 10:54:05 -04:00
|
|
|
void Download::parseModelsJsonFile(const QByteArray &jsonData)
|
2023-04-18 21:10:06 -04:00
|
|
|
{
|
|
|
|
QJsonParseError err;
|
|
|
|
QJsonDocument document = QJsonDocument::fromJson(jsonData, &err);
|
|
|
|
if (err.error != QJsonParseError::NoError) {
|
|
|
|
qDebug() << "ERROR: Couldn't parse: " << jsonData << err.errorString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-27 13:52:24 -04:00
|
|
|
QString defaultModel;
|
2023-04-18 21:10:06 -04:00
|
|
|
QJsonArray jsonArray = document.array();
|
|
|
|
|
|
|
|
m_modelMap.clear();
|
|
|
|
for (const QJsonValue &value : jsonArray) {
|
|
|
|
QJsonObject obj = value.toObject();
|
|
|
|
|
|
|
|
QString modelFilename = obj["filename"].toString();
|
2023-04-20 09:32:51 -04:00
|
|
|
QString modelFilesize = obj["filesize"].toString();
|
2023-04-28 20:30:52 -04:00
|
|
|
QString requires = obj["requires"].toString();
|
2023-04-18 21:10:06 -04:00
|
|
|
QByteArray modelMd5sum = obj["md5sum"].toString().toLatin1().constData();
|
|
|
|
bool isDefault = obj.contains("isDefault") && obj["isDefault"] == QString("true");
|
2023-04-27 14:52:40 -04:00
|
|
|
bool bestGPTJ = obj.contains("bestGPTJ") && obj["bestGPTJ"] == QString("true");
|
|
|
|
bool bestLlama = obj.contains("bestLlama") && obj["bestLlama"] == QString("true");
|
|
|
|
QString description = obj["description"].toString();
|
|
|
|
|
2023-04-27 13:52:24 -04:00
|
|
|
if (isDefault)
|
|
|
|
defaultModel = modelFilename;
|
2023-04-20 09:32:51 -04:00
|
|
|
quint64 sz = modelFilesize.toULongLong();
|
|
|
|
if (sz < 1024) {
|
|
|
|
modelFilesize = QString("%1 bytes").arg(sz);
|
|
|
|
} else if (sz < 1024 * 1024) {
|
|
|
|
modelFilesize = QString("%1 KB").arg(qreal(sz) / 1024, 0, 'g', 3);
|
|
|
|
} else if (sz < 1024 * 1024 * 1024) {
|
|
|
|
modelFilesize = QString("%1 MB").arg(qreal(sz) / (1024 * 1024), 0, 'g', 3);
|
|
|
|
} else {
|
|
|
|
modelFilesize = QString("%1 GB").arg(qreal(sz) / (1024 * 1024 * 1024), 0, 'g', 3);
|
|
|
|
}
|
|
|
|
|
2023-04-23 11:28:17 -04:00
|
|
|
QString filePath = downloadLocalModelsPath() + modelFilename;
|
2023-04-18 21:10:06 -04:00
|
|
|
QFileInfo info(filePath);
|
|
|
|
ModelInfo modelInfo;
|
|
|
|
modelInfo.filename = modelFilename;
|
2023-04-20 09:32:51 -04:00
|
|
|
modelInfo.filesize = modelFilesize;
|
2023-04-18 21:10:06 -04:00
|
|
|
modelInfo.md5sum = modelMd5sum;
|
|
|
|
modelInfo.installed = info.exists();
|
|
|
|
modelInfo.isDefault = isDefault;
|
2023-04-27 14:52:40 -04:00
|
|
|
modelInfo.bestGPTJ = bestGPTJ;
|
|
|
|
modelInfo.bestLlama = bestLlama;
|
|
|
|
modelInfo.description = description;
|
2023-04-28 20:30:52 -04:00
|
|
|
modelInfo.requires = requires;
|
2023-04-18 21:10:06 -04:00
|
|
|
m_modelMap.insert(modelInfo.filename, modelInfo);
|
|
|
|
}
|
|
|
|
|
2023-04-27 13:52:24 -04:00
|
|
|
// remove ggml- prefix and .bin suffix
|
|
|
|
Q_ASSERT(defaultModel.startsWith("ggml-"));
|
|
|
|
defaultModel = defaultModel.remove(0, 5);
|
|
|
|
Q_ASSERT(defaultModel.endsWith(".bin"));
|
|
|
|
defaultModel.chop(4);
|
|
|
|
|
|
|
|
QSettings settings;
|
|
|
|
settings.sync();
|
|
|
|
settings.setValue("defaultModel", defaultModel);
|
|
|
|
settings.sync();
|
2023-04-18 21:10:06 -04:00
|
|
|
emit modelListChanged();
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
qDebug() << "ERROR: Couldn't parse: " << jsonData << err.errorString();
|
|
|
|
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-05-02 20:31:17 -04:00
|
|
|
void Download::handleErrorOccurred(QNetworkReply::NetworkError code)
|
|
|
|
{
|
|
|
|
QNetworkReply *modelReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!modelReply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QString modelFilename = modelReply->url().fileName();
|
|
|
|
qWarning() << "ERROR: Network error occurred attemptint to download"
|
|
|
|
<< modelFilename
|
|
|
|
<< "code:" << code
|
|
|
|
<< "errorString" << modelReply->errorString();
|
|
|
|
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
|
|
|
|
|
|
|
QString modelFilename = modelReply->url().fileName();
|
2023-05-02 18:24:16 -04:00
|
|
|
emit downloadProgress(tempFile->pos(), bytesTotal, modelFilename);
|
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
|
|
|
|
2023-04-23 19:43:20 -04:00
|
|
|
void HashAndSaveFile::hashAndSave(const QString &expectedHash, const QString &saveFilePath,
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile, QNetworkReply *modelReply)
|
2023-04-23 19:43:20 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(!tempFile->isOpen());
|
2023-04-18 21:10:06 -04:00
|
|
|
QString modelFilename = modelReply->url().fileName();
|
|
|
|
|
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-04-23 19:43:20 -04:00
|
|
|
qWarning() << "ERROR: Could not open temp file for hashing:"
|
|
|
|
<< tempFile->fileName() << modelFilename;
|
|
|
|
emit hashAndSaveFinished(false, tempFile, modelReply);
|
2023-04-18 21:10:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QCryptographicHash hash(QCryptographicHash::Md5);
|
2023-04-23 19:43:20 -04:00
|
|
|
while(!tempFile->atEnd())
|
|
|
|
hash.addData(tempFile->read(16384));
|
|
|
|
if (hash.result().toHex() != expectedHash) {
|
|
|
|
tempFile->close();
|
2023-04-18 21:10:06 -04:00
|
|
|
qWarning() << "ERROR: Download error MD5SUM did not match:"
|
|
|
|
<< hash.result().toHex()
|
2023-04-23 19:43:20 -04:00
|
|
|
<< "!=" << expectedHash << "for" << modelFilename;
|
2023-05-02 18:24:16 -04:00
|
|
|
tempFile->remove();
|
2023-04-23 19:43:20 -04:00
|
|
|
emit hashAndSaveFinished(false, tempFile, modelReply);
|
|
|
|
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)) {
|
|
|
|
emit hashAndSaveFinished(true, tempFile, modelReply);
|
|
|
|
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-04-23 19:43:20 -04:00
|
|
|
qWarning() << "ERROR: Could not open temp file at finish:"
|
|
|
|
<< tempFile->fileName() << modelFilename;
|
|
|
|
emit hashAndSaveFinished(false, 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();
|
|
|
|
emit hashAndSaveFinished(true, tempFile, modelReply);
|
2023-04-23 11:28:17 -04:00
|
|
|
} else {
|
|
|
|
QFile::FileError error = file.error();
|
|
|
|
qWarning() << "ERROR: Could not save model to location:"
|
2023-04-23 19:43:20 -04:00
|
|
|
<< saveFilePath
|
2023-04-23 11:28:17 -04:00
|
|
|
<< "failed with code" << error;
|
2023-04-23 19:43:20 -04:00
|
|
|
tempFile->close();
|
|
|
|
emit hashAndSaveFinished(false, tempFile, modelReply);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Download::handleModelDownloadFinished()
|
|
|
|
{
|
|
|
|
QNetworkReply *modelReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!modelReply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QString modelFilename = modelReply->url().fileName();
|
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()) {
|
|
|
|
qWarning() << "ERROR: downloading:" << modelReply->errorString();
|
2023-04-23 11:28:17 -04:00
|
|
|
modelReply->deleteLater();
|
2023-04-23 19:43:20 -04:00
|
|
|
tempFile->deleteLater();
|
2023-04-23 11:28:17 -04:00
|
|
|
emit downloadFinished(modelFilename);
|
|
|
|
return;
|
2023-04-18 21:10:06 -04:00
|
|
|
}
|
|
|
|
|
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-04-23 19:43:20 -04:00
|
|
|
// Notify that we are calculating hash
|
|
|
|
ModelInfo info = m_modelMap.value(modelFilename);
|
|
|
|
info.calcHash = true;
|
2023-04-18 21:10:06 -04:00
|
|
|
m_modelMap.insert(modelFilename, info);
|
|
|
|
emit modelListChanged();
|
2023-04-23 19:43:20 -04:00
|
|
|
|
|
|
|
const QString saveFilePath = downloadLocalModelsPath() + modelFilename;
|
|
|
|
emit requestHashAndSave(info.md5sum, saveFilePath, tempFile, modelReply);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Download::handleHashAndSaveFinished(bool success,
|
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());
|
|
|
|
QString modelFilename = modelReply->url().fileName();
|
2023-05-02 20:31:17 -04:00
|
|
|
Network::globalInstance()->sendDownloadFinished(modelFilename, success);
|
2023-04-23 19:43:20 -04:00
|
|
|
|
|
|
|
ModelInfo info = m_modelMap.value(modelFilename);
|
|
|
|
info.calcHash = false;
|
|
|
|
info.installed = success;
|
|
|
|
m_modelMap.insert(modelFilename, info);
|
|
|
|
emit modelListChanged();
|
|
|
|
|
|
|
|
modelReply->deleteLater();
|
|
|
|
tempFile->deleteLater();
|
|
|
|
emit downloadFinished(modelFilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Download::handleReadyRead()
|
|
|
|
{
|
|
|
|
QNetworkReply *modelReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!modelReply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QString modelFilename = modelReply->url().fileName();
|
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
|
|
|
}
|