2021-02-16 00:36:10 +01:00
|
|
|
#include "notifications/Manager.h"
|
|
|
|
|
|
|
|
#include "Cache.h"
|
|
|
|
#include "EventAccessors.h"
|
2021-03-17 19:17:57 +01:00
|
|
|
#include "Logging.h"
|
|
|
|
#include "MatrixClient.h"
|
2021-02-16 00:36:10 +01:00
|
|
|
#include "Utils.h"
|
|
|
|
|
2021-03-17 19:17:57 +01:00
|
|
|
#include <QFile>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
|
|
|
|
#include <mtxclient/crypto/client.hpp>
|
|
|
|
|
|
|
|
QString
|
|
|
|
NotificationsManager::cacheImage(const mtx::events::collections::TimelineEvents &event)
|
2021-02-16 00:36:10 +01:00
|
|
|
{
|
2021-03-17 19:17:57 +01:00
|
|
|
const auto url = mtx::accessors::url(event);
|
|
|
|
auto encryptionInfo = mtx::accessors::file(event);
|
|
|
|
|
|
|
|
auto filename = QString::fromStdString(mtx::accessors::body(event));
|
|
|
|
QString path{QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" +
|
|
|
|
filename};
|
|
|
|
|
2021-03-03 01:42:34 +01:00
|
|
|
bool downloadComplete = false;
|
|
|
|
|
2021-03-17 19:17:57 +01:00
|
|
|
http::client()->download(
|
|
|
|
url,
|
2021-03-03 01:42:34 +01:00
|
|
|
[&downloadComplete, &path, url, encryptionInfo](const std::string &data,
|
|
|
|
const std::string &,
|
|
|
|
const std::string &,
|
|
|
|
mtx::http::RequestErr err) {
|
2021-03-17 19:17:57 +01:00
|
|
|
if (err) {
|
|
|
|
nhlog::net()->warn("failed to retrieve image {}: {} {}",
|
|
|
|
url,
|
|
|
|
err->matrix_error.error,
|
|
|
|
static_cast<int>(err->status_code));
|
2021-03-03 01:42:34 +01:00
|
|
|
// the image doesn't exist, so delete the path
|
|
|
|
path.clear();
|
|
|
|
downloadComplete = true;
|
2021-03-17 19:17:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto temp = data;
|
|
|
|
if (encryptionInfo)
|
|
|
|
temp = mtx::crypto::to_string(
|
|
|
|
mtx::crypto::decrypt_file(temp, encryptionInfo.value()));
|
|
|
|
|
|
|
|
QFile file{path};
|
|
|
|
|
2021-03-03 01:42:34 +01:00
|
|
|
if (!file.open(QIODevice::WriteOnly)) {
|
|
|
|
path.clear();
|
|
|
|
downloadComplete = true;
|
2021-03-17 19:17:57 +01:00
|
|
|
return;
|
2021-03-03 01:42:34 +01:00
|
|
|
}
|
2021-03-17 19:17:57 +01:00
|
|
|
|
|
|
|
// delete any existing file content
|
|
|
|
file.resize(0);
|
|
|
|
|
2021-03-02 01:56:11 +01:00
|
|
|
// resize the image
|
|
|
|
QImage img{utils::readImage(QByteArray{temp.data()})};
|
2021-03-17 19:17:57 +01:00
|
|
|
|
2021-03-03 01:42:34 +01:00
|
|
|
if (img.isNull()) {
|
|
|
|
path.clear();
|
|
|
|
downloadComplete = true;
|
|
|
|
return;
|
2021-03-02 02:07:53 +01:00
|
|
|
}
|
2021-03-17 19:17:57 +01:00
|
|
|
|
2021-03-02 01:56:11 +01:00
|
|
|
#ifdef NHEKO_DBUS_SYS // the images in D-Bus notifications are to be 200x100 max
|
2021-03-17 19:17:57 +01:00
|
|
|
img.scaled(200, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation)
|
|
|
|
.save(&file);
|
2021-03-02 01:56:11 +01:00
|
|
|
#else
|
|
|
|
img.save(&file);
|
|
|
|
#endif // NHEKO_DBUS_SYS
|
|
|
|
|
2021-03-17 19:17:57 +01:00
|
|
|
file.close();
|
|
|
|
|
2021-03-03 01:42:34 +01:00
|
|
|
downloadComplete = true;
|
2021-03-17 19:17:57 +01:00
|
|
|
return;
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
nhlog::ui()->warn("Error while caching file to: {}", e.what());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-03 01:42:34 +01:00
|
|
|
while (!downloadComplete)
|
|
|
|
continue;
|
|
|
|
|
2021-03-17 19:17:57 +01:00
|
|
|
return path.toHtmlEscaped();
|
2021-02-16 00:36:10 +01:00
|
|
|
}
|