summary refs log tree commit diff
path: root/src/MxcImageProvider.cpp
diff options
context:
space:
mode:
authorZhymabek Roman <61125068+ZhymabekRoman@users.noreply.github.com>2023-01-02 21:59:29 +0600
committerGitHub <noreply@github.com>2023-01-02 21:59:29 +0600
commit59410a99ac9b5a90356adc2192b8c6107456c78f (patch)
treef42e081641cee3d62057208f3af1de3afddd02e6 /src/MxcImageProvider.cpp
parentAppImage: reduce package size (diff)
parentA whole new year full of excitement and possibilities! (diff)
downloadnheko-59410a99ac9b5a90356adc2192b8c6107456c78f.tar.xz
Merge branch 'master' into master
Diffstat (limited to 'src/MxcImageProvider.cpp')
-rw-r--r--src/MxcImageProvider.cpp68
1 files changed, 65 insertions, 3 deletions
diff --git a/src/MxcImageProvider.cpp b/src/MxcImageProvider.cpp

index 3022ca3d..33f691da 100644 --- a/src/MxcImageProvider.cpp +++ b/src/MxcImageProvider.cpp
@@ -1,5 +1,6 @@ // SPDX-FileCopyrightText: 2021 Nheko Contributors // SPDX-FileCopyrightText: 2022 Nheko Contributors +// SPDX-FileCopyrightText: 2023 Nheko Contributors // // SPDX-License-Identifier: GPL-3.0-or-later @@ -16,6 +17,8 @@ #include <QPainter> #include <QPainterPath> #include <QStandardPaths> +#include <QThreadPool> +#include <QTimer> #include "Logging.h" #include "MatrixClient.h" @@ -23,6 +26,40 @@ QHash<QString, mtx::crypto::EncryptedFile> infos; +MxcImageProvider::MxcImageProvider(QObject *parent) +#if QT_VERSION < 0x60000 + : QObject(parent) +#else + : QQuickAsyncImageProvider(parent) +#endif +{ + auto timer = new QTimer(this); + timer->setInterval(std::chrono::hours(1)); + connect(timer, &QTimer::timeout, this, [] { + QThreadPool::globalInstance()->start([] { + QDir dir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + + "/media_cache", + "", + QDir::SortFlags(QDir::Name | QDir::IgnoreCase), + QDir::Filter::Writable | QDir::Filter::NoDotAndDotDot | QDir::Filter::Files); + + auto files = dir.entryInfoList(); + for (const auto &fileInfo : qAsConst(files)) { + if (fileInfo.fileTime(QFile::FileTime::FileAccessTime) + .daysTo(QDateTime::currentDateTime()) > 30) { + if (QFile::remove(fileInfo.absoluteFilePath())) + nhlog::net()->debug("Deleted stale media '{}'", + fileInfo.absoluteFilePath().toStdString()); + else + nhlog::net()->warn("Failed to delete stale media '{}'", + fileInfo.absoluteFilePath().toStdString()); + } + } + }); + }); + timer->start(); +} + QQuickImageResponse * MxcImageProvider::requestImageResponse(const QString &id, const QSize &requestedSize) { @@ -97,6 +134,24 @@ clipRadius(QImage img, double radius) return out; } +static void +possiblyUpdateAccessTime(const QFileInfo &fileInfo) +{ + if (fileInfo.fileTime(QFile::FileTime::FileAccessTime).daysTo(QDateTime::currentDateTime()) > + 7) { + nhlog::net()->debug("Updating file time for '{}'", + fileInfo.absoluteFilePath().toStdString()); + + QFile f(fileInfo.absoluteFilePath()); + + if (!f.open(QIODevice::ReadWrite) || + !f.setFileTime(QDateTime::currentDateTime(), QFile::FileTime::FileAccessTime)) { + nhlog::net()->warn("Failed to update filetime for '{}'", + fileInfo.absoluteFilePath().toStdString()); + } + } +} + void MxcImageProvider::download(const QString &id, const QSize &requestedSize, @@ -141,6 +196,8 @@ MxcImageProvider::download(const QString &id, if (fileInfo.exists()) { QImage image = utils::readImageFromFile(fileInfo.absoluteFilePath()); if (!image.isNull()) { + possiblyUpdateAccessTime(fileInfo); + if (requestedSize.width() <= 0) { image = image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation); } else { @@ -169,9 +226,10 @@ MxcImageProvider::download(const QString &id, 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"; + opts.width = static_cast<uint16_t>(requestedSize.width() > 0 ? requestedSize.width() : -1); + opts.height = + static_cast<uint16_t>(requestedSize.height() > 0 ? requestedSize.height() : -1); + opts.method = crop ? "crop" : "scale"; http::client()->get_thumbnail( opts, [fileInfo, requestedSize, radius, then, id, crop, cropLocally]( @@ -184,6 +242,8 @@ MxcImageProvider::download(const QString &id, auto data = QByteArray(res.data(), (int)res.size()); QImage image = utils::readImage(data); if (!image.isNull()) { + possiblyUpdateAccessTime(fileInfo); + if (requestedSize.width() <= 0) { image = image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation); @@ -237,6 +297,7 @@ MxcImageProvider::download(const QString &id, QImage image = utils::readImage(data); image.setText(QStringLiteral("mxc url"), "mxc://" + id); if (!image.isNull()) { + possiblyUpdateAccessTime(fileInfo); if (radius != 0) { image = clipRadius(std::move(image), radius); } @@ -247,6 +308,7 @@ MxcImageProvider::download(const QString &id, } else { QImage image = utils::readImageFromFile(fileInfo.absoluteFilePath()); if (!image.isNull()) { + possiblyUpdateAccessTime(fileInfo); if (radius != 0) { image = clipRadius(std::move(image), radius); }