From 6e642db7db5d59314122cbc5ab6333d1771830c4 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 16 Mar 2025 18:10:21 +0100 Subject: [PATCH] Cleanup original images handling --- api/src/controllers/seed/insert/shows.ts | 90 ++++++++++++++---------- api/src/controllers/seed/movies.ts | 13 ++++ api/src/controllers/seed/series.ts | 13 ++++ 3 files changed, 79 insertions(+), 37 deletions(-) diff --git a/api/src/controllers/seed/insert/shows.ts b/api/src/controllers/seed/insert/shows.ts index f84e72eb..66cf81dc 100644 --- a/api/src/controllers/seed/insert/shows.ts +++ b/api/src/controllers/seed/insert/shows.ts @@ -5,6 +5,7 @@ import { conflictUpdateAllExcept, sqlarr } from "~/db/utils"; import type { SeedCollection } from "~/models/collections"; import type { SeedMovie } from "~/models/movie"; import type { SeedSerie } from "~/models/serie"; +import type { Original } from "~/models/utils"; import { getYear } from "~/utils"; import { enqueueOptImage } from "../images"; @@ -12,53 +13,68 @@ type Show = typeof shows.$inferInsert; type ShowTrans = typeof showTranslations.$inferInsert; export const insertShow = async ( - show: Omit & { originalLanguage: string }, + show: Omit, + original: Original & { + poster: string | null; + thumbnail: string | null; + banner: string | null; + logo: string | null; + }, translations: | SeedMovie["translations"] | SeedSerie["translations"] | SeedCollection["translations"], ) => { return await db.transaction(async (tx) => { - const trans: (Omit & { latinName: string | null })[] = - await Promise.all( - Object.entries(translations).map(async ([lang, tr]) => ({ - language: lang, - ...tr, - latinName: tr.latinName ?? null, - poster: await enqueueOptImage(tx, { - url: tr.poster, - column: showTranslations.poster, - }), - thumbnail: await enqueueOptImage(tx, { - url: tr.thumbnail, - column: showTranslations.thumbnail, - }), - logo: await enqueueOptImage(tx, { - url: tr.logo, - column: showTranslations.logo, - }), - banner: await enqueueOptImage(tx, { - url: tr.banner, - column: showTranslations.banner, - }), - })), - ); - const original = trans.find((x) => x.language === show.originalLanguage); - - if (!original) { - tx.rollback(); - return { - status: 422 as const, - message: "No translation available in the original language.", - }; - } - - const ret = await insertBaseShow(tx, { ...show, original }); + const orig = { + ...original, + poster: await enqueueOptImage(tx, { + url: original.poster, + column: shows.original.poster, + }), + thumbnail: await enqueueOptImage(tx, { + url: original.thumbnail, + column: shows.original.thumbnail, + }), + banner: await enqueueOptImage(tx, { + url: original.banner, + column: shows.original.banner, + }), + logo: await enqueueOptImage(tx, { + url: original.logo, + column: shows.original.logo, + }), + }; + const ret = await insertBaseShow(tx, { ...show, original: orig }); if ("status" in ret) return ret; + const trans: ShowTrans[] = await Promise.all( + Object.entries(translations).map(async ([lang, tr]) => ({ + pk: ret.pk, + language: lang, + ...tr, + latinName: tr.latinName ?? null, + poster: await enqueueOptImage(tx, { + url: tr.poster, + column: showTranslations.poster, + }), + thumbnail: await enqueueOptImage(tx, { + url: tr.thumbnail, + column: showTranslations.thumbnail, + }), + logo: await enqueueOptImage(tx, { + url: tr.logo, + column: showTranslations.logo, + }), + banner: await enqueueOptImage(tx, { + url: tr.banner, + column: showTranslations.banner, + }), + })), + ); await tx .insert(showTranslations) - .values(trans.map((x) => ({ ...x, pk: ret.pk }))) + .values(trans) .onConflictDoUpdate({ target: [showTranslations.pk, showTranslations.language], set: conflictUpdateAllExcept(showTranslations, ["pk", "language"]), diff --git a/api/src/controllers/seed/movies.ts b/api/src/controllers/seed/movies.ts index 2102b6ce..158c94bb 100644 --- a/api/src/controllers/seed/movies.ts +++ b/api/src/controllers/seed/movies.ts @@ -55,6 +55,14 @@ export const seedMovie = async ( const { translations, videos, collection, studios, staff, ...movie } = seed; const nextRefresh = guessNextRefresh(movie.airDate ?? new Date()); + const original = translations[movie.originalLanguage]; + if (!original) { + return { + status: 422, + message: "No translation available in the original language.", + }; + } + const col = await insertCollection(collection, { kind: "movie", nextRefresh, @@ -70,6 +78,11 @@ export const seedMovie = async ( entriesCount: 1, ...movie, }, + { + ...original, + latinName: original.latinName ?? null, + language: movie.originalLanguage, + }, translations, ); if ("status" in show) return show; diff --git a/api/src/controllers/seed/series.ts b/api/src/controllers/seed/series.ts index d207439f..500aa57a 100644 --- a/api/src/controllers/seed/series.ts +++ b/api/src/controllers/seed/series.ts @@ -92,6 +92,14 @@ export const seedSerie = async ( } = seed; const nextRefresh = guessNextRefresh(serie.startAir ?? new Date()); + const original = translations[serie.originalLanguage]; + if (!original) { + return { + status: 422, + message: "No translation available in the original language.", + }; + } + const col = await insertCollection(collection, { kind: "serie", nextRefresh, @@ -106,6 +114,11 @@ export const seedSerie = async ( entriesCount: entries.length, ...serie, }, + { + ...original, + latinName: original.latinName ?? null, + language: serie.originalLanguage, + }, translations, ); if ("status" in show) return show;