summaryrefslogtreecommitdiff
path: root/src/utils/thumbnailer.ts
diff options
context:
space:
mode:
author2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2025-04-26 22:36:23 +1000
committer2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2025-04-26 22:36:23 +1000
commit3c579d0e275cdaf6f2c9589abade94bde7905c82 (patch)
tree4b825dc642cb6eb9a060e54bf8d69288fbee4904 /src/utils/thumbnailer.ts
parentschemes: fix (diff)
downloadcaelestia-shell-3c579d0e275cdaf6f2c9589abade94bde7905c82.tar.gz
caelestia-shell-3c579d0e275cdaf6f2c9589abade94bde7905c82.tar.bz2
caelestia-shell-3c579d0e275cdaf6f2c9589abade94bde7905c82.zip
clean
Remove everything
Diffstat (limited to 'src/utils/thumbnailer.ts')
-rw-r--r--src/utils/thumbnailer.ts80
1 files changed, 0 insertions, 80 deletions
diff --git a/src/utils/thumbnailer.ts b/src/utils/thumbnailer.ts
deleted file mode 100644
index d23dab1..0000000
--- a/src/utils/thumbnailer.ts
+++ /dev/null
@@ -1,80 +0,0 @@
-import { execAsync, GLib, type Variable } from "astal";
-import { thumbnailer as config } from "config";
-
-export interface ThumbOpts {
- width?: number;
- height?: number;
- exact?: boolean;
-}
-
-export default class Thumbnailer {
- static readonly thumbnailDir = `${CACHE}/thumbnails`;
-
- static readonly #running = new Set<string>();
-
- static getOpt<T extends keyof ThumbOpts>(opt: T, opts: ThumbOpts) {
- return opts[opt] ?? (config.defaults[opt] as Variable<NonNullable<ThumbOpts[T]>>).get();
- }
-
- static async getThumbPath(path: string, opts: ThumbOpts) {
- const hash = (await execAsync(`sha1sum ${path}`)).split(" ")[0];
- const size = `${this.getOpt("width", opts)}x${this.getOpt("height", opts)}`;
- const exact = this.getOpt("exact", opts) ? "-exact" : "";
- return `${this.thumbnailDir}/${hash}@${size}${exact}.png`;
- }
-
- static async shouldThumbnail(path: string, opts: ThumbOpts) {
- const [w, h] = (await execAsync(`identify -ping -format "%w %h" ${path}`)).split(" ").map(parseInt);
- return w > this.getOpt("width", opts) || h > this.getOpt("height", opts);
- }
-
- static async #thumbnail(path: string, opts: ThumbOpts, attempts: number): Promise<string> {
- const thumbPath = await this.getThumbPath(path, opts);
-
- try {
- const width = this.getOpt("width", opts);
- const height = this.getOpt("height", opts);
- const cropCmd = this.getOpt("exact", opts)
- ? `-background none -gravity center -extent ${width}x${height}`
- : "";
- await execAsync(`magick ${path} -thumbnail ${width}x${height}^ ${cropCmd} -unsharp 0x.5 ${thumbPath}`);
- } catch {
- if (attempts >= config.maxAttempts.get()) {
- console.error(`Failed to generate thumbnail for ${path}`);
- return path;
- }
-
- await new Promise(r => setTimeout(r, config.timeBetweenAttempts.get()));
- return this.#thumbnail(path, opts, attempts + 1);
- }
-
- return thumbPath;
- }
-
- static async thumbnail(path: string, opts: ThumbOpts = {}): Promise<string> {
- if (!(await this.shouldThumbnail(path, opts))) return path;
-
- let thumbPath = await this.getThumbPath(path, opts);
-
- // Wait for existing thumbnail for path to finish
- while (this.#running.has(path)) await new Promise(r => setTimeout(r, 100));
-
- // If no thumbnail, generate
- if (!GLib.file_test(thumbPath, GLib.FileTest.EXISTS)) {
- this.#running.add(path);
-
- thumbPath = await this.#thumbnail(path, opts, 0);
-
- this.#running.delete(path);
- }
-
- return thumbPath;
- }
-
- // Static class
- private constructor() {}
-
- static {
- GLib.mkdir_with_parents(this.thumbnailDir, 0o755);
- }
-}