2019-09-07 23:22:07 +03:00
|
|
|
#include "MxcImageProvider.h"
|
|
|
|
|
|
|
|
#include "Cache.h"
|
2019-12-15 04:56:04 +03:00
|
|
|
#include "Logging.h"
|
2019-12-15 05:34:17 +03:00
|
|
|
#include "MatrixClient.h"
|
2020-04-26 12:26:51 +03:00
|
|
|
#include "Utils.h"
|
2019-09-07 23:22:07 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
MxcImageResponse::run()
|
|
|
|
{
|
2019-12-04 01:34:16 +03:00
|
|
|
if (m_requestedSize.isValid() && !m_encryptionInfo) {
|
2019-11-03 03:17:40 +03:00
|
|
|
QString fileName = QString("%1_%2x%3_crop")
|
2019-09-07 23:22:07 +03:00
|
|
|
.arg(m_id)
|
|
|
|
.arg(m_requestedSize.width())
|
|
|
|
.arg(m_requestedSize.height());
|
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
auto data = cache::image(fileName);
|
2020-04-26 12:26:51 +03:00
|
|
|
if (!data.isNull()) {
|
|
|
|
m_image = utils::readImage(&data);
|
2020-06-07 15:53:35 +03:00
|
|
|
m_image = m_image.scaled(
|
|
|
|
m_requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
2019-09-07 23:22:07 +03:00
|
|
|
m_image.setText("mxc url", "mxc://" + m_id);
|
2020-04-26 12:26:51 +03:00
|
|
|
|
|
|
|
if (!m_image.isNull()) {
|
|
|
|
emit finished();
|
|
|
|
return;
|
|
|
|
}
|
2019-09-07 23:22:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mtx::http::ThumbOpts opts;
|
|
|
|
opts.mxc_url = "mxc://" + m_id.toStdString();
|
|
|
|
opts.width = m_requestedSize.width() > 0 ? m_requestedSize.width() : -1;
|
|
|
|
opts.height = m_requestedSize.height() > 0 ? m_requestedSize.height() : -1;
|
2019-11-03 03:17:40 +03:00
|
|
|
opts.method = "crop";
|
2019-09-07 23:22:07 +03:00
|
|
|
http::client()->get_thumbnail(
|
|
|
|
opts, [this, fileName](const std::string &res, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
nhlog::net()->error("Failed to download image {}",
|
|
|
|
m_id.toStdString());
|
|
|
|
m_error = "Failed download";
|
|
|
|
emit finished();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto data = QByteArray(res.data(), res.size());
|
2019-12-15 04:56:04 +03:00
|
|
|
cache::saveImage(fileName, data);
|
2020-04-26 12:26:51 +03:00
|
|
|
m_image = utils::readImage(&data);
|
2019-09-07 23:22:07 +03:00
|
|
|
m_image.setText("mxc url", "mxc://" + m_id);
|
|
|
|
|
|
|
|
emit finished();
|
|
|
|
});
|
|
|
|
} else {
|
2019-12-15 04:56:04 +03:00
|
|
|
auto data = cache::image(m_id);
|
2020-04-26 12:26:51 +03:00
|
|
|
|
|
|
|
if (!data.isNull()) {
|
|
|
|
m_image = utils::readImage(&data);
|
2019-09-07 23:22:07 +03:00
|
|
|
m_image.setText("mxc url", "mxc://" + m_id);
|
2020-04-26 12:26:51 +03:00
|
|
|
|
|
|
|
if (!m_image.isNull()) {
|
|
|
|
emit finished();
|
|
|
|
return;
|
|
|
|
}
|
2019-09-07 23:22:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
http::client()->download(
|
|
|
|
"mxc://" + m_id.toStdString(),
|
|
|
|
[this](const std::string &res,
|
|
|
|
const std::string &,
|
|
|
|
const std::string &originalFilename,
|
|
|
|
mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
nhlog::net()->error("Failed to download image {}",
|
|
|
|
m_id.toStdString());
|
|
|
|
m_error = "Failed download";
|
|
|
|
emit finished();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-04 01:34:16 +03:00
|
|
|
auto temp = res;
|
|
|
|
if (m_encryptionInfo)
|
|
|
|
temp = mtx::crypto::to_string(
|
|
|
|
mtx::crypto::decrypt_file(temp, m_encryptionInfo.value()));
|
|
|
|
|
|
|
|
auto data = QByteArray(temp.data(), temp.size());
|
2020-04-26 12:26:51 +03:00
|
|
|
cache::saveImage(m_id, data);
|
|
|
|
m_image = utils::readImage(&data);
|
2019-09-07 23:22:07 +03:00
|
|
|
m_image.setText("original filename",
|
|
|
|
QString::fromStdString(originalFilename));
|
|
|
|
m_image.setText("mxc url", "mxc://" + m_id);
|
|
|
|
|
|
|
|
emit finished();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|