Files
2026-09-11 16:01:11 -04:00

69 lines
1.8 KiB
QML

import QtQuick
import Quickshell
import Quickshell.Io
Item {
id: root
required property string url
required property bool cacheEnabled
property string source: url
property string dest: ""
readonly property string cacheDir: Quickshell.env("HOME") + "/.config/skygqts/carousel-image-cache"
function cachePathFor(imageUrl) {
let h = 5381
for (let i = 0; i < imageUrl.length; i++)
h = ((h << 5) + h) + imageUrl.charCodeAt(i)
h = h >>> 0
const m = imageUrl.match(/\.([A-Za-z0-9]+)(?:[?#]|$)/)
let ext = m ? m[1].toLowerCase() : "img"
if (ext === "jpeg")
ext = "jpg"
const allowed = ["png", "jpg", "gif", "webp", "svg", "ico", "bmp"]
if (allowed.indexOf(ext) < 0)
ext = "img"
return root.cacheDir + "/" + h.toString(16) + "." + ext
}
function isHttp(s) {
return s.startsWith("http://") || s.startsWith("https://")
}
function start() {
root.source = root.url
check.running = false
downloader.running = false
if (!root.cacheEnabled || !root.url || !isHttp(root.url))
return
root.dest = cachePathFor(root.url)
check.running = true
}
Component.onCompleted: start()
onUrlChanged: start()
onCacheEnabledChanged: start()
Process {
id: check
command: ["test", "-f", root.dest]
onExited: code => {
if (code === 0)
root.source = "file://" + root.dest
else
downloader.running = true
}
}
Process {
id: downloader
command: ["curl", "-fsSL", "--create-dirs", "-o", root.dest, root.url]
onExited: code => {
if (code === 0)
root.source = "file://" + root.dest
}
}
}