thumbnailer: use single process + load original

This commit is contained in:
2 * r + 2 * t 2025-05-05 22:15:31 +10:00
parent 137fd56bb2
commit dff2bde9a3
3 changed files with 28 additions and 43 deletions

View file

@ -41,6 +41,7 @@ Item {
anchors.fill: parent anchors.fill: parent
loadOriginal: true
asynchronous: true asynchronous: true
cache: false cache: false
fillMode: Image.PreserveAspectCrop fillMode: Image.PreserveAspectCrop

View file

@ -11,11 +11,12 @@ Singleton {
readonly property string thumbDir: `${StandardPaths.standardLocations(StandardPaths.GenericCacheLocation)[0]}/caelestia/thumbnails`.slice(7) readonly property string thumbDir: `${StandardPaths.standardLocations(StandardPaths.GenericCacheLocation)[0]}/caelestia/thumbnails`.slice(7)
function go(path: string, width: int, height: int): var { function go(obj: var): var {
return thumbComp.createObject(root, { return thumbComp.createObject(obj, {
originalPath: path, originalPath: obj.path,
width: width, width: obj.width,
height: height height: obj.height,
loadOriginal: obj.loadOriginal
}); });
} }
@ -25,46 +26,41 @@ Singleton {
required property string originalPath required property string originalPath
required property int width required property int width
required property int height required property int height
required property bool loadOriginal
property string path property string path
readonly property Process shaProc: Process { readonly property Process proc: Process {
running: true running: true
command: ["sha1sum", obj.originalPath]
stdout: SplitParser {
onRead: data => {
const sha = data.split(" ")[0];
obj.path = `${root.thumbDir}/${sha}@${obj.width}x${obj.height}-exact.png`;
obj.thumbProc.signal(9);
obj.thumbProc.running = true;
}
}
}
readonly property Process thumbProc: Process {
command: ["fish", "-c", ` command: ["fish", "-c", `
if test -f ${obj.path} set -l path "${root.thumbDir}/$(sha1sum ${obj.originalPath} | cut -d ' ' -f 1)@${obj.width}x${obj.height}-exact.png"
exit 1 if test -f $path
echo $path
else else
echo 'start'
set -l size (identify -ping -format '%w\n%h' ${obj.originalPath}) set -l size (identify -ping -format '%w\n%h' ${obj.originalPath})
if test $size[1] -gt ${obj.width} -o $size[2] -gt ${obj.height} if test $size[1] -gt ${obj.width} -o $size[2] -gt ${obj.height}
magick ${obj.originalPath} -${obj.width > 1024 || obj.height > 1024 ? "resize" : "thumbnail"} ${obj.width}x${obj.height}^ -background none -gravity center -extent ${obj.width}x${obj.height} -unsharp 0x.5 ${obj.path} magick ${obj.originalPath} -${obj.width > 1024 || obj.height > 1024 ? "resize" : "thumbnail"} ${obj.width}x${obj.height}^ -background none -gravity center -extent ${obj.width}x${obj.height} -unsharp 0x.5 $path
else else
cp ${obj.originalPath} ${obj.path} cp ${obj.originalPath} $path
end end
echo $path
end`] end`]
onExited: code => { stdout: SplitParser {
if (code === 0) { onRead: data => {
const path = obj.path; if (data === "start") {
obj.path = ""; if (obj.loadOriginal)
obj.path = path; obj.path = obj.originalPath;
} else {
obj.path = data;
}
} }
} }
} }
function reload(): void { function reload(): void {
shaProc.signal(9); proc.signal(9);
shaProc.running = true; proc.running = true;
} }
} }

View file

@ -6,22 +6,10 @@ Image {
id: root id: root
property string path property string path
readonly property Thumbnailer.Thumbnail thumbnail: Thumbnailer.go(path, width, height) property bool loadOriginal
readonly property Thumbnailer.Thumbnail thumbnail: Thumbnailer.go(this)
source: thumbnail.path ? `file://${thumbnail.path}` : "" source: thumbnail.path ? `file://${thumbnail.path}` : ""
asynchronous: true asynchronous: true
fillMode: Image.PreserveAspectCrop fillMode: Image.PreserveAspectCrop
onPathChanged: {
thumbnail.originalPath = path;
thumbnail.reload();
}
onWidthChanged: {
thumbnail.width = width;
thumbnail.reload();
}
onHeightChanged: {
thumbnail.height = height;
thumbnail.reload();
}
} }