Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
nheko
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nheko Reborn
nheko
Commits
95d898e0
Verified
Commit
95d898e0
authored
2 years ago
by
Nicolas Werner
Browse files
Options
Downloads
Patches
Plain Diff
Add cache pruning for old image files
parent
952827d6
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Pipeline
#3997
failed
2 years ago
Stage: build
Stage: deploy
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/MxcImageProvider.cpp
+59
-0
59 additions, 0 deletions
src/MxcImageProvider.cpp
src/MxcImageProvider.h
+4
-0
4 additions, 0 deletions
src/MxcImageProvider.h
with
63 additions
and
0 deletions
src/MxcImageProvider.cpp
+
59
−
0
View file @
95d898e0
...
...
@@ -16,6 +16,8 @@
#include
<QPainter>
#include
<QPainterPath>
#include
<QStandardPaths>
#include
<QThreadPool>
#include
<QTimer>
#include
"Logging.h"
#include
"MatrixClient.h"
...
...
@@ -23,6 +25,39 @@
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
*
MxcImageProvider
::
requestImageResponse
(
const
QString
&
id
,
const
QSize
&
requestedSize
)
{
...
...
@@ -97,6 +132,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 +194,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
{
...
...
@@ -185,6 +240,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
);
...
...
@@ -238,6 +295,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
);
}
...
...
@@ -248,6 +306,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
);
}
...
...
This diff is collapsed.
Click to expand it.
src/MxcImageProvider.h
+
4
−
0
View file @
95d898e0
...
...
@@ -80,6 +80,10 @@ class MxcImageProvider
public
QQuickAsyncImageProvider
{
Q_OBJECT
public:
MxcImageProvider
(
QObject
*
parent
=
nullptr
);
public
slots
:
QQuickImageResponse
*
requestImageResponse
(
const
QString
&
id
,
const
QSize
&
requestedSize
)
override
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment