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";
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");
hasher.update(url);
@ -13,7 +16,7 @@ export const processImage = async (url: string): Promise<Image> => {
};
};
export const processOptImage = (url: string | null): Promise<Image | null> => {
if (!url) return Promise.resolve(null);
export const processOptImage = (url: string | null): Image | null => {
if (!url) return null;
return processImage(url);
};

View File

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

View File

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

View File

@ -1,13 +1,19 @@
import { t } from "elysia";
import type { SeedSerie } from "~/models/serie";
import { getYear } from "~/utils";
import { insertEntries } from "./insert/entries";
import { insertShow } from "./insert/shows";
import { guessNextRefresh } from "./refresh";
import { insertEntries } from "./insert/entries";
export const SeedSerieResponse = t.Object({
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;
@ -31,7 +37,7 @@ export const seedSerie = async (
const { translations, seasons, entries, ...serie } = seed;
const nextRefresh = guessNextRefresh(serie.startAir ?? new Date());
const ret = await insertShow(
const show = await insertShow(
{
kind: "serie",
nextRefresh,
@ -39,13 +45,14 @@ export const seedSerie = async (
},
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 {
updated: ret.updated,
id: ret.id,
slug: ret.slug,
updated: show.updated,
id: show.id,
slug: show.slug,
entries: retEntries,
};
};

View File

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