mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-26 04:58:49 +03:00
Add cache pruning for old image files
This commit is contained in:
parent
952827d629
commit
95d898e09d
2 changed files with 63 additions and 0 deletions
|
@ -16,6 +16,8 @@
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QPainterPath>
|
#include <QPainterPath>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
|
#include <QThreadPool>
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
#include "Logging.h"
|
#include "Logging.h"
|
||||||
#include "MatrixClient.h"
|
#include "MatrixClient.h"
|
||||||
|
@ -23,6 +25,39 @@
|
||||||
|
|
||||||
QHash<QString, mtx::crypto::EncryptedFile> infos;
|
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(30));
|
||||||
|
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);
|
||||||
|
|
||||||
|
for (const auto &fileInfo : dir.entryInfoList()) {
|
||||||
|
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 *
|
QQuickImageResponse *
|
||||||
MxcImageProvider::requestImageResponse(const QString &id, const QSize &requestedSize)
|
MxcImageProvider::requestImageResponse(const QString &id, const QSize &requestedSize)
|
||||||
{
|
{
|
||||||
|
@ -97,6 +132,24 @@ clipRadius(QImage img, double radius)
|
||||||
return out;
|
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
|
void
|
||||||
MxcImageProvider::download(const QString &id,
|
MxcImageProvider::download(const QString &id,
|
||||||
const QSize &requestedSize,
|
const QSize &requestedSize,
|
||||||
|
@ -141,6 +194,8 @@ MxcImageProvider::download(const QString &id,
|
||||||
if (fileInfo.exists()) {
|
if (fileInfo.exists()) {
|
||||||
QImage image = utils::readImageFromFile(fileInfo.absoluteFilePath());
|
QImage image = utils::readImageFromFile(fileInfo.absoluteFilePath());
|
||||||
if (!image.isNull()) {
|
if (!image.isNull()) {
|
||||||
|
possiblyUpdateAccessTime(fileInfo);
|
||||||
|
|
||||||
if (requestedSize.width() <= 0) {
|
if (requestedSize.width() <= 0) {
|
||||||
image = image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation);
|
image = image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation);
|
||||||
} else {
|
} else {
|
||||||
|
@ -185,6 +240,8 @@ MxcImageProvider::download(const QString &id,
|
||||||
auto data = QByteArray(res.data(), (int)res.size());
|
auto data = QByteArray(res.data(), (int)res.size());
|
||||||
QImage image = utils::readImage(data);
|
QImage image = utils::readImage(data);
|
||||||
if (!image.isNull()) {
|
if (!image.isNull()) {
|
||||||
|
possiblyUpdateAccessTime(fileInfo);
|
||||||
|
|
||||||
if (requestedSize.width() <= 0) {
|
if (requestedSize.width() <= 0) {
|
||||||
image =
|
image =
|
||||||
image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation);
|
image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation);
|
||||||
|
@ -238,6 +295,7 @@ MxcImageProvider::download(const QString &id,
|
||||||
QImage image = utils::readImage(data);
|
QImage image = utils::readImage(data);
|
||||||
image.setText(QStringLiteral("mxc url"), "mxc://" + id);
|
image.setText(QStringLiteral("mxc url"), "mxc://" + id);
|
||||||
if (!image.isNull()) {
|
if (!image.isNull()) {
|
||||||
|
possiblyUpdateAccessTime(fileInfo);
|
||||||
if (radius != 0) {
|
if (radius != 0) {
|
||||||
image = clipRadius(std::move(image), radius);
|
image = clipRadius(std::move(image), radius);
|
||||||
}
|
}
|
||||||
|
@ -248,6 +306,7 @@ MxcImageProvider::download(const QString &id,
|
||||||
} else {
|
} else {
|
||||||
QImage image = utils::readImageFromFile(fileInfo.absoluteFilePath());
|
QImage image = utils::readImageFromFile(fileInfo.absoluteFilePath());
|
||||||
if (!image.isNull()) {
|
if (!image.isNull()) {
|
||||||
|
possiblyUpdateAccessTime(fileInfo);
|
||||||
if (radius != 0) {
|
if (radius != 0) {
|
||||||
image = clipRadius(std::move(image), radius);
|
image = clipRadius(std::move(image), radius);
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,6 +80,10 @@ class MxcImageProvider
|
||||||
public QQuickAsyncImageProvider
|
public QQuickAsyncImageProvider
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
MxcImageProvider(QObject *parent = nullptr);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
QQuickImageResponse *
|
QQuickImageResponse *
|
||||||
requestImageResponse(const QString &id, const QSize &requestedSize) override;
|
requestImageResponse(const QString &id, const QSize &requestedSize) override;
|
||||||
|
|
Loading…
Reference in a new issue