From 9e3a9af0ef401166febdbf2fbbc28b47b6e8f8f4 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 26 May 2025 00:52:00 +0200 Subject: [PATCH] Properly handle image cache --- .github/workflows/auth-hurl.yml | 2 +- api/src/controllers/seed/images.ts | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.github/workflows/auth-hurl.yml b/.github/workflows/auth-hurl.yml index b19999fa..154d17d3 100644 --- a/.github/workflows/auth-hurl.yml +++ b/.github/workflows/auth-hurl.yml @@ -48,7 +48,7 @@ jobs: working-directory: ./auth run: | ./keibi > logs & - wget --retry-connrefused --retry-on-http-error=502 http://localhost:4568/health + wget --retry-connrefused --retry-on-http-error=502 http://localhost:4568/auth/health hurl --error-format long --variable host=http://localhost:4568/auth tests/* env: PGHOST: localhost diff --git a/api/src/controllers/seed/images.ts b/api/src/controllers/seed/images.ts index 621c91b0..391c23dd 100644 --- a/api/src/controllers/seed/images.ts +++ b/api/src/controllers/seed/images.ts @@ -131,7 +131,7 @@ export const processImages = async () => { if (evt.channel !== "kyoo_image") return; processAll(); }); - await client.query("listen image"); + await client.query("listen kyoo_image"); // start processing old tasks await processAll(); @@ -139,7 +139,13 @@ export const processImages = async () => { }; async function downloadImage(id: string, url: string): Promise { - // TODO: check if file exists before downloading + const low = await getFile(path.join(imageDir, `${id}.low.jpg`)) + .arrayBuffer() + .catch(() => false as const); + if (low) { + return await getBlurhash(sharp(low)); + } + const resp = await fetch(url, { headers: { "User-Agent": `Kyoo v${version}` }, }); @@ -167,20 +173,15 @@ async function downloadImage(id: string, url: string): Promise { await Bun.write(file, buffer, { mode: 0o660 }); }), ); + return await getBlurhash(image); +} +async function getBlurhash(image: sharp.Sharp): Promise { const { data, info } = await image .resize(32, 32, { fit: "inside" }) .ensureAlpha() .raw() .toBuffer({ resolveWithObject: true }); - const blurHash = encode( - new Uint8ClampedArray(data), - info.width, - info.height, - 4, - 3, - ); - - return blurHash; + return encode(new Uint8ClampedArray(data), info.width, info.height, 4, 3); }