Make processImages synchronous

This commit is contained in:
Zoe Roux 2025-01-26 14:38:07 +01:00
parent 962672e4ed
commit 5ca0ef08d4
No known key found for this signature in database
5 changed files with 39 additions and 28 deletions

View File

@ -1,6 +1,9 @@
import type { Image } from "~/models/utils"; import type { Image } from "~/models/utils";
export const processImage = async (url: string): Promise<Image> => { // this will only push a task to the image downloader service and not download it instantly.
// this is both done to prevent to many requests to be sent at once and to make sure POST
// requests are not blocked by image downloading or blurhash calculation
export const processImage = (url: string): Image => {
const hasher = new Bun.CryptoHasher("sha256"); const hasher = new Bun.CryptoHasher("sha256");
hasher.update(url); hasher.update(url);
@ -13,7 +16,7 @@ export const processImage = async (url: string): Promise<Image> => {
}; };
}; };
export const processOptImage = (url: string | null): Promise<Image | null> => { export const processOptImage = (url: string | null): Image | null => {
if (!url) return Promise.resolve(null); if (!url) return null;
return processImage(url); return processImage(url);
}; };

View File

@ -3,6 +3,7 @@ import { db } from "~/db";
import { showTranslations, shows } from "~/db/schema"; import { showTranslations, shows } from "~/db/schema";
import { conflictUpdateAllExcept } from "~/db/utils"; import { conflictUpdateAllExcept } from "~/db/utils";
import type { SeedMovie } from "~/models/movie"; import type { SeedMovie } from "~/models/movie";
import type { SeedSerie } from "~/models/serie";
import { getYear } from "~/utils"; import { getYear } from "~/utils";
import { processOptImage } from "../images"; import { processOptImage } from "../images";
@ -11,22 +12,22 @@ type ShowTrans = typeof showTranslations.$inferInsert;
export const insertShow = async ( export const insertShow = async (
show: Show, show: Show,
translations: SeedMovie["translations"], translations: SeedMovie["translations"] | SeedSerie["translations"],
) => { ) => {
return await db.transaction(async (tx) => { return await db.transaction(async (tx) => {
const ret = await insertBaseShow(tx, show); const ret = await insertBaseShow(tx, show);
if ("status" in ret) return ret; if ("status" in ret) return ret;
const trans: ShowTrans[] = await Promise.all( const trans: ShowTrans[] = Object.entries(translations).map(
Object.entries(translations).map(async ([lang, tr]) => ({ ([lang, tr]) => ({
pk: ret.pk, pk: ret.pk,
language: lang, language: lang,
...tr, ...tr,
poster: await processOptImage(tr.poster), poster: processOptImage(tr.poster),
thumbnail: await processOptImage(tr.thumbnail), thumbnail: processOptImage(tr.thumbnail),
logo: await processOptImage(tr.logo), logo: processOptImage(tr.logo),
banner: await processOptImage(tr.banner), banner: processOptImage(tr.banner),
})), }),
); );
await tx await tx
.insert(showTranslations) .insert(showTranslations)

View File

@ -43,7 +43,7 @@ export const seedMovie = async (
const { translations, videos: vids, ...bMovie } = seed; const { translations, videos: vids, ...bMovie } = seed;
const nextRefresh = guessNextRefresh(bMovie.airDate ?? new Date()); const nextRefresh = guessNextRefresh(bMovie.airDate ?? new Date());
const ret = await insertShow( const show = await insertShow(
{ {
kind: "movie", kind: "movie",
startAir: bMovie.airDate, startAir: bMovie.airDate,
@ -52,12 +52,13 @@ export const seedMovie = async (
}, },
translations, translations,
); );
if ("status" in ret) return ret; if ("status" in show) return show;
// even if never shown to the user, a movie still has an entry. // even if never shown to the user, a movie still has an entry.
const [entry] = await insertEntries(ret.pk, [ const [entry] = await insertEntries(show, [
{ {
kind: "movie", kind: "movie",
order: 1,
nextRefresh, nextRefresh,
...bMovie, ...bMovie,
}, },
@ -70,12 +71,12 @@ export const seedMovie = async (
.select( .select(
db db
.select({ .select({
entry: sql<number>`${ret.entry}`.as("entry"), entry: sql<number>`${show.entry}`.as("entry"),
video: videos.pk, video: videos.pk,
// TODO: do not add rendering if all videos of the entry have the same rendering // TODO: do not add rendering if all videos of the entry have the same rendering
slug: sql<string>` slug: sql<string>`
concat( concat(
${ret.slug}::text, ${show.slug}::text,
case when ${videos.part} <> null then concat('-p', ${videos.part}) else '' end, case when ${videos.part} <> null then concat('-p', ${videos.part}) else '' end,
case when ${videos.version} <> 1 then concat('-v', ${videos.version}) else '' end case when ${videos.version} <> 1 then concat('-v', ${videos.version}) else '' end
) )
@ -90,9 +91,9 @@ export const seedMovie = async (
} }
return { return {
updated: ret.updated, updated: show.updated,
id: ret.id, id: show.id,
slug: ret.slug, slug: show.slug,
videos: retVideos, videos: retVideos,
}; };
}; };

View File

@ -1,13 +1,19 @@
import { t } from "elysia"; import { t } from "elysia";
import type { SeedSerie } from "~/models/serie"; import type { SeedSerie } from "~/models/serie";
import { getYear } from "~/utils"; import { getYear } from "~/utils";
import { insertEntries } from "./insert/entries";
import { insertShow } from "./insert/shows"; import { insertShow } from "./insert/shows";
import { guessNextRefresh } from "./refresh"; import { guessNextRefresh } from "./refresh";
import { insertEntries } from "./insert/entries";
export const SeedSerieResponse = t.Object({ export const SeedSerieResponse = t.Object({
id: t.String({ format: "uuid" }), id: t.String({ format: "uuid" }),
slug: t.String({ format: "slug", examples: ["bubble"] }), slug: t.String({ format: "slug", examples: ["made-in-abyss"] }),
entries: t.Array(
t.Object({
id: t.String({ format: "uuid" }),
slug: t.String({ format: "slug", examples: ["made-in-abyss-s1e1"] }),
}),
),
}); });
export type SeedSerieResponse = typeof SeedSerieResponse.static; export type SeedSerieResponse = typeof SeedSerieResponse.static;
@ -31,7 +37,7 @@ export const seedSerie = async (
const { translations, seasons, entries, ...serie } = seed; const { translations, seasons, entries, ...serie } = seed;
const nextRefresh = guessNextRefresh(serie.startAir ?? new Date()); const nextRefresh = guessNextRefresh(serie.startAir ?? new Date());
const ret = await insertShow( const show = await insertShow(
{ {
kind: "serie", kind: "serie",
nextRefresh, nextRefresh,
@ -39,13 +45,14 @@ export const seedSerie = async (
}, },
translations, translations,
); );
if ("status" in ret) return ret; if ("status" in show) return show;
const retEntries = await insertEntries(ret.pk, entries); const retEntries = await insertEntries(show, entries);
return { return {
updated: ret.updated, updated: show.updated,
id: ret.id, id: show.id,
slug: ret.slug, slug: show.slug,
entries: retEntries,
}; };
}; };

View File

@ -2,7 +2,6 @@
"$schema": "https://biomejs.dev/schemas/1.8.1/schema.json", "$schema": "https://biomejs.dev/schemas/1.8.1/schema.json",
"formatter": { "formatter": {
"enabled": true, "enabled": true,
"formatWithErrors": false,
"indentStyle": "tab", "indentStyle": "tab",
"indentWidth": 2, "indentWidth": 2,
"lineEnding": "lf", "lineEnding": "lf",