Skip to content
Snippets Groups Projects
Commit 0a9f221a authored by Nicolas Werner's avatar Nicolas Werner
Browse files

Image provider image caching

parent 9fdeee1f
No related branches found
No related tags found
No related merge requests found
#include "mxcimageprovider.h"
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QStandardPaths>
#include "client.h"
#include "debug_out.h"
void MXCImageResponse::run() {
qDebug() << "Downloading image: " << m_id;
QFileInfo cacheFile(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" + m_id);
if (m_requestedSize.isValid()) {
cacheFile = QFileInfo(cacheFile.filePath() +
QString("_%1x%2").arg(m_requestedSize.width()).arg(m_requestedSize.height()));
if (m_image.load(cacheFile.filePath())) {
emit finished();
qDebug() << "Loaded cached thumbnail: " << m_id;
return;
}
qDebug() << "Downloading thumbnail: " << m_id;
mtx::http::ThumbOpts opts;
opts.mxc_url = "mxc://" + m_id.toStdString();
opts.width = m_requestedSize.width();
opts.height = m_requestedSize.height();
opts.method = "scale";
http::client().get_thumbnail(opts, [this](const std::string &res, mtx::http::RequestErr err) {
http::client().get_thumbnail(opts, [this, cacheFile](const std::string &res, mtx::http::RequestErr err) {
if (err) {
qDebug() << "Failed to retrieve image: " << m_id << " error: " << *err;
m_error = "Failed download";
......@@ -26,13 +41,25 @@ void MXCImageResponse::run() {
auto data = QByteArray(res.data(), res.size());
m_image.loadFromData(data);
m_image = m_image.scaled(m_requestedSize, Qt::KeepAspectRatio);
m_image.setText("mxc url", "mxc://" + m_id);
QDir().mkpath(cacheFile.path());
m_image.save(cacheFile.filePath(), "png");
emit finished();
});
} else {
if (m_image.load(cacheFile.filePath())) {
emit finished();
qDebug() << "Loaded cached image: " << m_id;
return;
}
qDebug() << "Downloading image: " << m_id;
http::client().download("mxc://" + m_id.toStdString(),
[this](const std::string &res, const std::string &content_type,
const std::string &originalFilename, mtx::http::RequestErr err) {
[this, cacheFile](const std::string &res, const std::string &content_type,
const std::string &originalFilename, mtx::http::RequestErr err) {
if (err) {
qDebug() << "Failed to retrieve image: " << m_id << " error: " << *err;
m_error = "Failed download";
......@@ -43,6 +70,11 @@ void MXCImageResponse::run() {
auto data = QByteArray(res.data(), res.size());
m_image.loadFromData(data);
m_image.setText("original filename", QString::fromStdString(originalFilename));
m_image.setText("mxc url", "mxc://" + m_id);
QDir().mkpath(cacheFile.path());
m_image.save(cacheFile.filePath(), "png");
emit finished();
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment