Properly handle image cache

This commit is contained in:
Zoe Roux 2025-05-26 00:52:00 +02:00
parent af97c52e48
commit 9e3a9af0ef
No known key found for this signature in database
2 changed files with 13 additions and 12 deletions

View File

@ -48,7 +48,7 @@ jobs:
working-directory: ./auth working-directory: ./auth
run: | run: |
./keibi > logs & ./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/* hurl --error-format long --variable host=http://localhost:4568/auth tests/*
env: env:
PGHOST: localhost PGHOST: localhost

View File

@ -131,7 +131,7 @@ export const processImages = async () => {
if (evt.channel !== "kyoo_image") return; if (evt.channel !== "kyoo_image") return;
processAll(); processAll();
}); });
await client.query("listen image"); await client.query("listen kyoo_image");
// start processing old tasks // start processing old tasks
await processAll(); await processAll();
@ -139,7 +139,13 @@ export const processImages = async () => {
}; };
async function downloadImage(id: string, url: string): Promise<string> { async function downloadImage(id: string, url: string): Promise<string> {
// 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, { const resp = await fetch(url, {
headers: { "User-Agent": `Kyoo v${version}` }, headers: { "User-Agent": `Kyoo v${version}` },
}); });
@ -167,20 +173,15 @@ async function downloadImage(id: string, url: string): Promise<string> {
await Bun.write(file, buffer, { mode: 0o660 }); await Bun.write(file, buffer, { mode: 0o660 });
}), }),
); );
return await getBlurhash(image);
}
async function getBlurhash(image: sharp.Sharp): Promise<string> {
const { data, info } = await image const { data, info } = await image
.resize(32, 32, { fit: "inside" }) .resize(32, 32, { fit: "inside" })
.ensureAlpha() .ensureAlpha()
.raw() .raw()
.toBuffer({ resolveWithObject: true }); .toBuffer({ resolveWithObject: true });
const blurHash = encode( return encode(new Uint8ClampedArray(data), info.width, info.height, 4, 3);
new Uint8ClampedArray(data),
info.width,
info.height,
4,
3,
);
return blurHash;
} }