downloadImageByUrl moved to SystemUtils

This commit is contained in:
Michał Cieślak 2024-10-08 15:28:45 +02:00 committed by Michał
parent 3f9f175e07
commit 206bc79ab4
6 changed files with 64 additions and 15 deletions

View File

@ -76,12 +76,6 @@ QtObject:
except:
return false
proc downloadImage*(self: Utils, content: string, path: string) {.slot.} =
downloadImage(content, path)
proc downloadImageByUrl*(self: Utils, url: string, path: string) {.slot.} =
downloadImageByUrl(url, path)
proc generateQRCodeSVG*(self: Utils, text: string, border: int = 0): string =
var qr0: array[0..qrcodegen_BUFFER_LEN_MAX, uint8]
var tempBuffer: array[0..qrcodegen_BUFFER_LEN_MAX, uint8]

View File

@ -45,10 +45,6 @@ SplitView {
return url
}
function downloadImageByUrl(url, path) {
logs.logEvent("Utils::downloadImageByUrl", ["url", "path"], arguments)
}
function isAlias(name) {
return false
}

View File

@ -10,4 +10,5 @@ public:
Q_INVOKABLE QString qtRuntimeVersion() const;
Q_INVOKABLE void restartApplication() const;
Q_INVOKABLE void downloadImageByUrl(const QUrl& url, const QString& path) const;
};

View File

@ -1,7 +1,12 @@
#include "StatusQ/systemutilsinternal.h"
#include <QCoreApplication>
#include <QDir>
#include <QMimeDatabase>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QProcess>
#include <QSaveFile>
SystemUtilsInternal::SystemUtilsInternal(QObject *parent)
: QObject{parent}
@ -16,3 +21,60 @@ void SystemUtilsInternal::restartApplication() const
QProcess::startDetached(QCoreApplication::applicationFilePath(), {});
QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
}
void SystemUtilsInternal::downloadImageByUrl(
const QUrl& url, const QString& path) const
{
static thread_local QNetworkAccessManager manager;
manager.setAutoDeleteReplies(true);
QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(url)));
// accept both "file:/foo/bar" and "/foo/bar"
auto targetDir = QUrl::fromUserInput(path).toLocalFile();
if (targetDir.isEmpty())
targetDir = QDir::homePath();
QObject::connect(reply, &QNetworkReply::finished, [reply, targetDir] {
if(reply->error() != QNetworkReply::NoError) {
qWarning() << "SystemUtilsInternal::downloadImageByUrl: Downloading image"
<< reply->request().url() << "failed!";
return;
}
// Extract the image data to be able to load and save it
const auto btArray = reply->readAll();
Q_ASSERT(!btArray.isEmpty());
// Get current Date/Time information to use in naming of the image file
const auto dateTimeString = QDateTime::currentDateTime().toString(
QStringLiteral("dd-MM-yyyy_hh-mm-ss"));
// Get the preferred extension
QMimeDatabase mimeDb;
auto ext = mimeDb.mimeTypeForData(btArray).preferredSuffix();
if (ext.isEmpty())
ext = QStringLiteral("jpg");
// Construct the target path
const auto targetFile = QStringLiteral("%1/image_%2.%3").arg(
targetDir, dateTimeString, ext);
// Save the image in a safe way
QSaveFile image(targetFile);
if (!image.open(QIODevice::WriteOnly)) {
qWarning() << "SystemUtilsInternal::downloadImageByUrl: "
"Downloading image failed while opening the save file:"
<< targetFile;
return;
}
if (image.write(btArray) != -1)
image.commit();
else
qWarning() << "SystemUtilsInternal::downloadImageByUrl: "
"Downloading image failed while saving to file:"
<< targetFile;
});
}

View File

@ -862,7 +862,7 @@ QtObject {
selectMultiple: false
modality: Qt.NonModal
onAccepted: {
Utils.downloadImageByUrl(imageSource, fileUrl)
SystemUtils.downloadImageByUrl(imageSource, fileUrl)
destroy()
}
onRejected: {

View File

@ -956,10 +956,6 @@ QtObject {
}
}
function downloadImageByUrl(url, path) {
globalUtilsInst.downloadImageByUrl(url, path)
}
function getKeypairLocation(keypair, fromAccountDetailsView) {
if (!keypair || keypair.pairType === Constants.keypair.type.watchOnly) {
return ""