diff --git a/src/app/global/utils.nim b/src/app/global/utils.nim index 6e9d6ec5de..f52c24fe0a 100644 --- a/src/app/global/utils.nim +++ b/src/app/global/utils.nim @@ -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] diff --git a/storybook/pages/ProfileDialogViewPage.qml b/storybook/pages/ProfileDialogViewPage.qml index 1bfe54c983..2ed66ef5cf 100644 --- a/storybook/pages/ProfileDialogViewPage.qml +++ b/storybook/pages/ProfileDialogViewPage.qml @@ -45,10 +45,6 @@ SplitView { return url } - function downloadImageByUrl(url, path) { - logs.logEvent("Utils::downloadImageByUrl", ["url", "path"], arguments) - } - function isAlias(name) { return false } diff --git a/ui/StatusQ/include/StatusQ/systemutilsinternal.h b/ui/StatusQ/include/StatusQ/systemutilsinternal.h index fdd185092f..a0f4467142 100644 --- a/ui/StatusQ/include/StatusQ/systemutilsinternal.h +++ b/ui/StatusQ/include/StatusQ/systemutilsinternal.h @@ -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; }; diff --git a/ui/StatusQ/src/systemutilsinternal.cpp b/ui/StatusQ/src/systemutilsinternal.cpp index 33ed7be2c8..9761b3c46f 100644 --- a/ui/StatusQ/src/systemutilsinternal.cpp +++ b/ui/StatusQ/src/systemutilsinternal.cpp @@ -1,7 +1,12 @@ #include "StatusQ/systemutilsinternal.h" #include +#include +#include +#include +#include #include +#include 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; + }); +} diff --git a/ui/app/mainui/Popups.qml b/ui/app/mainui/Popups.qml index 5cd07f7bdb..4f0f79c0d9 100644 --- a/ui/app/mainui/Popups.qml +++ b/ui/app/mainui/Popups.qml @@ -862,7 +862,7 @@ QtObject { selectMultiple: false modality: Qt.NonModal onAccepted: { - Utils.downloadImageByUrl(imageSource, fileUrl) + SystemUtils.downloadImageByUrl(imageSource, fileUrl) destroy() } onRejected: { diff --git a/ui/imports/utils/Utils.qml b/ui/imports/utils/Utils.qml index d5a9e8d748..e7c52effe5 100644 --- a/ui/imports/utils/Utils.qml +++ b/ui/imports/utils/Utils.qml @@ -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 ""