Cleanup original images handling

This commit is contained in:
Zoe Roux 2025-03-16 18:10:21 +01:00
parent 9ef114b91a
commit 6e642db7db
No known key found for this signature in database
3 changed files with 79 additions and 37 deletions

View File

@ -5,6 +5,7 @@ import { conflictUpdateAllExcept, sqlarr } from "~/db/utils";
import type { SeedCollection } from "~/models/collections"; import type { SeedCollection } from "~/models/collections";
import type { SeedMovie } from "~/models/movie"; import type { SeedMovie } from "~/models/movie";
import type { SeedSerie } from "~/models/serie"; import type { SeedSerie } from "~/models/serie";
import type { Original } from "~/models/utils";
import { getYear } from "~/utils"; import { getYear } from "~/utils";
import { enqueueOptImage } from "../images"; import { enqueueOptImage } from "../images";
@ -12,53 +13,68 @@ type Show = typeof shows.$inferInsert;
type ShowTrans = typeof showTranslations.$inferInsert; type ShowTrans = typeof showTranslations.$inferInsert;
export const insertShow = async ( export const insertShow = async (
show: Omit<Show, "original"> & { originalLanguage: string }, show: Omit<Show, "original">,
original: Original & {
poster: string | null;
thumbnail: string | null;
banner: string | null;
logo: string | null;
},
translations: translations:
| SeedMovie["translations"] | SeedMovie["translations"]
| SeedSerie["translations"] | SeedSerie["translations"]
| SeedCollection["translations"], | SeedCollection["translations"],
) => { ) => {
return await db.transaction(async (tx) => { return await db.transaction(async (tx) => {
const trans: (Omit<ShowTrans, "pk"> & { latinName: string | null })[] = const orig = {
await Promise.all( ...original,
Object.entries(translations).map(async ([lang, tr]) => ({ poster: await enqueueOptImage(tx, {
language: lang, url: original.poster,
...tr, column: shows.original.poster,
latinName: tr.latinName ?? null, }),
poster: await enqueueOptImage(tx, { thumbnail: await enqueueOptImage(tx, {
url: tr.poster, url: original.thumbnail,
column: showTranslations.poster, column: shows.original.thumbnail,
}), }),
thumbnail: await enqueueOptImage(tx, { banner: await enqueueOptImage(tx, {
url: tr.thumbnail, url: original.banner,
column: showTranslations.thumbnail, column: shows.original.banner,
}), }),
logo: await enqueueOptImage(tx, { logo: await enqueueOptImage(tx, {
url: tr.logo, url: original.logo,
column: showTranslations.logo, column: shows.original.logo,
}), }),
banner: await enqueueOptImage(tx, { };
url: tr.banner, const ret = await insertBaseShow(tx, { ...show, original: orig });
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 });
if ("status" in ret) return ret; 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 await tx
.insert(showTranslations) .insert(showTranslations)
.values(trans.map((x) => ({ ...x, pk: ret.pk }))) .values(trans)
.onConflictDoUpdate({ .onConflictDoUpdate({
target: [showTranslations.pk, showTranslations.language], target: [showTranslations.pk, showTranslations.language],
set: conflictUpdateAllExcept(showTranslations, ["pk", "language"]), set: conflictUpdateAllExcept(showTranslations, ["pk", "language"]),

View File

@ -55,6 +55,14 @@ export const seedMovie = async (
const { translations, videos, collection, studios, staff, ...movie } = seed; const { translations, videos, collection, studios, staff, ...movie } = seed;
const nextRefresh = guessNextRefresh(movie.airDate ?? new Date()); 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, { const col = await insertCollection(collection, {
kind: "movie", kind: "movie",
nextRefresh, nextRefresh,
@ -70,6 +78,11 @@ export const seedMovie = async (
entriesCount: 1, entriesCount: 1,
...movie, ...movie,
}, },
{
...original,
latinName: original.latinName ?? null,
language: movie.originalLanguage,
},
translations, translations,
); );
if ("status" in show) return show; if ("status" in show) return show;

View File

@ -92,6 +92,14 @@ export const seedSerie = async (
} = seed; } = seed;
const nextRefresh = guessNextRefresh(serie.startAir ?? new Date()); 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, { const col = await insertCollection(collection, {
kind: "serie", kind: "serie",
nextRefresh, nextRefresh,
@ -106,6 +114,11 @@ export const seedSerie = async (
entriesCount: entries.length, entriesCount: entries.length,
...serie, ...serie,
}, },
{
...original,
latinName: original.latinName ?? null,
language: serie.originalLanguage,
},
translations, translations,
); );
if ("status" in show) return show; if ("status" in show) return show;