matrixion/src/MxcImageProvider.cpp

260 lines
9.4 KiB
C++
Raw Normal View History

2021-03-05 02:35:15 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
2019-09-07 23:22:07 +03:00
#include "MxcImageProvider.h"
#include <optional>
2020-10-27 19:45:28 +03:00
#include <mtxclient/crypto/client.hpp>
#include <QByteArray>
2021-03-17 21:08:17 +03:00
#include <QDir>
#include <QFileInfo>
2021-08-14 18:17:50 +03:00
#include <QPainter>
#include <QPainterPath>
#include <QStandardPaths>
2019-12-15 04:56:04 +03:00
#include "Logging.h"
#include "MatrixClient.h"
#include "Utils.h"
2019-09-07 23:22:07 +03:00
QHash<QString, mtx::crypto::EncryptedFile> infos;
QQuickImageResponse *
MxcImageProvider::requestImageResponse(const QString &id, const QSize &requestedSize)
{
2021-09-18 01:22:33 +03:00
auto id_ = id;
bool crop = true;
double radius = 0;
auto queryStart = id.lastIndexOf('?');
if (queryStart != -1) {
id_ = id.left(queryStart);
auto query = id.midRef(queryStart + 1);
auto queryBits = query.split('&');
for (auto b : queryBits) {
if (b == "scale") {
crop = false;
} else if (b.startsWith("radius=")) {
radius = b.mid(7).toDouble();
}
2021-08-06 02:45:47 +03:00
}
2021-09-18 01:22:33 +03:00
}
2021-08-06 02:45:47 +03:00
2021-12-08 01:58:17 +03:00
return new MxcImageResponse(id_, crop, radius, requestedSize);
}
void
MxcImageProvider::addEncryptionInfo(mtx::crypto::EncryptedFile info)
{
2021-09-18 01:22:33 +03:00
infos.insert(QString::fromStdString(info.url), info);
}
2019-09-07 23:22:07 +03:00
void
2021-12-08 01:58:17 +03:00
MxcImageRunnable::run()
2019-09-07 23:22:07 +03:00
{
2021-09-18 01:22:33 +03:00
MxcImageProvider::download(
m_id,
m_requestedSize,
[this](QString, QSize, QImage image, QString) {
if (image.isNull()) {
2021-12-08 01:58:17 +03:00
emit error("Failed to download image.");
2021-09-18 01:22:33 +03:00
} else {
2021-12-08 01:58:17 +03:00
emit done(image);
2021-09-18 01:22:33 +03:00
}
2021-12-08 01:58:17 +03:00
this->deleteLater();
2021-09-18 01:22:33 +03:00
},
m_crop,
m_radius);
2021-08-14 18:17:50 +03:00
}
static QImage
clipRadius(QImage img, double radius)
{
2021-09-18 01:22:33 +03:00
QImage out(img.size(), QImage::Format_ARGB32_Premultiplied);
out.fill(Qt::transparent);
2021-08-14 18:17:50 +03:00
2021-09-18 01:22:33 +03:00
QPainter painter(&out);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
2021-08-14 18:17:50 +03:00
2021-09-18 01:22:33 +03:00
QPainterPath ppath;
ppath.addRoundedRect(img.rect(), radius, radius, Qt::SizeMode::RelativeSize);
2021-08-14 18:17:50 +03:00
2021-09-18 01:22:33 +03:00
painter.setClipPath(ppath);
painter.drawImage(img.rect(), img);
2021-08-14 18:17:50 +03:00
2021-09-18 01:22:33 +03:00
return out;
}
void
MxcImageProvider::download(const QString &id,
const QSize &requestedSize,
2021-08-06 02:45:47 +03:00
std::function<void(QString, QSize, QImage, QString)> then,
2021-08-14 18:17:50 +03:00
bool crop,
double radius)
{
2021-09-18 01:22:33 +03:00
std::optional<mtx::crypto::EncryptedFile> encryptionInfo;
auto temp = infos.find("mxc://" + id);
if (temp != infos.end())
encryptionInfo = *temp;
if (requestedSize.isValid() && !encryptionInfo) {
QString fileName = QString("%1_%2x%3_%4_radius%5")
.arg(QString::fromUtf8(id.toUtf8().toBase64(
QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals)))
.arg(requestedSize.width())
.arg(requestedSize.height())
.arg(crop ? "crop" : "scale")
.arg(radius);
QFileInfo fileInfo(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) +
"/media_cache",
fileName);
QDir().mkpath(fileInfo.absolutePath());
if (fileInfo.exists()) {
QImage image = utils::readImageFromFile(fileInfo.absoluteFilePath());
if (!image.isNull()) {
image = image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
if (radius != 0) {
image = clipRadius(std::move(image), radius);
2019-09-07 23:22:07 +03:00
}
2021-09-18 01:22:33 +03:00
if (!image.isNull()) {
then(id, requestedSize, image, fileInfo.absoluteFilePath());
return;
}
}
}
mtx::http::ThumbOpts opts;
opts.mxc_url = "mxc://" + id.toStdString();
opts.width = requestedSize.width() > 0 ? requestedSize.width() : -1;
opts.height = requestedSize.height() > 0 ? requestedSize.height() : -1;
opts.method = crop ? "crop" : "scale";
http::client()->get_thumbnail(
opts,
[fileInfo, requestedSize, radius, then, id, crop](const std::string &res,
mtx::http::RequestErr err) {
2021-09-18 01:22:33 +03:00
if (err || res.empty()) {
download(id, QSize(), then, crop, radius);
2021-09-18 01:22:33 +03:00
return;
}
auto data = QByteArray(res.data(), (int)res.size());
QImage image = utils::readImage(data);
if (!image.isNull()) {
image =
image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
if (radius != 0) {
image = clipRadius(std::move(image), radius);
}
}
image.setText("mxc url", "mxc://" + id);
if (image.save(fileInfo.absoluteFilePath(), "png"))
nhlog::ui()->debug("Wrote: {}", fileInfo.absoluteFilePath().toStdString());
else
nhlog::ui()->debug("Failed to write: {}",
fileInfo.absoluteFilePath().toStdString());
then(id, requestedSize, image, fileInfo.absoluteFilePath());
});
} else {
try {
QString fileName = QString("%1_radius%2")
.arg(QString::fromUtf8(id.toUtf8().toBase64(
QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals)))
.arg(radius);
QFileInfo fileInfo(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) +
"/media_cache",
fileName);
QDir().mkpath(fileInfo.absolutePath());
if (fileInfo.exists()) {
if (encryptionInfo) {
QFile f(fileInfo.absoluteFilePath());
f.open(QIODevice::ReadOnly);
QByteArray fileData = f.readAll();
auto tempData = mtx::crypto::to_string(
mtx::crypto::decrypt_file(fileData.toStdString(), encryptionInfo.value()));
auto data = QByteArray(tempData.data(), (int)tempData.size());
QImage image = utils::readImage(data);
image.setText("mxc url", "mxc://" + id);
if (!image.isNull()) {
if (radius != 0) {
image = clipRadius(std::move(image), radius);
}
2021-09-18 01:22:33 +03:00
then(id, requestedSize, image, fileInfo.absoluteFilePath());
return;
}
} else {
QImage image = utils::readImageFromFile(fileInfo.absoluteFilePath());
if (!image.isNull()) {
if (radius != 0) {
image = clipRadius(std::move(image), radius);
}
then(id, requestedSize, image, fileInfo.absoluteFilePath());
return;
}
2019-09-07 23:22:07 +03:00
}
2021-09-18 01:22:33 +03:00
}
http::client()->download(
"mxc://" + id.toStdString(),
[fileInfo, requestedSize, then, id, radius, encryptionInfo](
const std::string &res,
const std::string &,
const std::string &originalFilename,
mtx::http::RequestErr err) {
if (err) {
then(id, QSize(), {}, "");
return;
}
auto tempData = res;
QFile f(fileInfo.absoluteFilePath());
if (!f.open(QIODevice::Truncate | QIODevice::WriteOnly)) {
then(id, QSize(), {}, "");
return;
}
f.write(tempData.data(), tempData.size());
f.close();
if (encryptionInfo) {
tempData = mtx::crypto::to_string(
mtx::crypto::decrypt_file(tempData, encryptionInfo.value()));
auto data = QByteArray(tempData.data(), (int)tempData.size());
QImage image = utils::readImage(data);
if (radius != 0) {
image = clipRadius(std::move(image), radius);
}
image.setText("original filename", QString::fromStdString(originalFilename));
image.setText("mxc url", "mxc://" + id);
then(id, requestedSize, image, fileInfo.absoluteFilePath());
return;
}
QImage image = utils::readImageFromFile(fileInfo.absoluteFilePath());
if (radius != 0) {
image = clipRadius(std::move(image), radius);
}
image.setText("original filename", QString::fromStdString(originalFilename));
image.setText("mxc url", "mxc://" + id);
then(id, requestedSize, image, fileInfo.absoluteFilePath());
});
} catch (std::exception &e) {
nhlog::net()->error("Exception while downloading media: {}", e.what());
2019-09-07 23:22:07 +03:00
}
2021-09-18 01:22:33 +03:00
}
2019-09-07 23:22:07 +03:00
}