Cleanup shows route & handle random

This commit is contained in:
Zoe Roux 2025-03-19 15:30:01 +01:00
parent 6ff00a1133
commit 27c5b34c5a
No known key found for this signature in database

View File

@ -1,43 +1,36 @@
import { stat } from "node:fs/promises"; import { stat } from "node:fs/promises";
import type { BunFile } from "bun"; import type { BunFile } from "bun";
import { type SQL, and, eq, sql } from "drizzle-orm"; import { type SQL, and, eq, sql } from "drizzle-orm";
import type { PgColumn } from "drizzle-orm/pg-core";
import Elysia, { type Context, t } from "elysia"; import Elysia, { type Context, t } from "elysia";
import { db } from "~/db"; import { db } from "~/db";
import { showTranslations, shows } from "~/db/schema"; import { showTranslations, shows } from "~/db/schema";
import { sqlarr } from "~/db/utils"; import { sqlarr } from "~/db/utils";
import { KError } from "~/models/error"; import { KError } from "~/models/error";
import { bubble } from "~/models/examples"; import { bubble } from "~/models/examples";
import { import { AcceptLanguage, isUuid, processLanguages } from "~/models/utils";
AcceptLanguage,
type Image,
isUuid,
processLanguages,
} from "~/models/utils";
import { imageDir } from "./seed/images"; import { imageDir } from "./seed/images";
function getRedirectToImageHandler({ function getRedirectToImageHandler({
image,
filter, filter,
}: { }: {
image: PgColumn<any, any, { $type: Image }>; filter?: SQL;
filter: SQL;
}) { }) {
return async function Handler({ return async function Handler({
params: { id }, params: { id, image },
headers: { "accept-language": languages }, headers: { "accept-language": languages },
query: { quality }, query: { quality },
set, set,
error, error,
redirect, redirect,
}: { }: {
params: { id: string }; params: { id: string; image: "poster" | "thumbnail" | "banner" | "logo" };
headers: { "accept-language": string }; headers: { "accept-language": string };
query: { quality: "high" | "medium" | "low" }; query: { quality: "high" | "medium" | "low" };
set: Context["set"]; set: Context["set"];
error: Context["error"]; error: Context["error"];
redirect: Context["redirect"]; redirect: Context["redirect"];
}) { }) {
id ??= "random";
const lang = processLanguages(languages); const lang = processLanguages(languages);
const item = db.$with("item").as( const item = db.$with("item").as(
db db
@ -59,7 +52,7 @@ function getRedirectToImageHandler({
const [ret] = await db const [ret] = await db
.with(item) .with(item)
.select({ .select({
image, image: showTranslations[image],
language: showTranslations.language, language: showTranslations.language,
}) })
.from(item) .from(item)
@ -140,10 +133,7 @@ export const imagesH = new Elysia({ tags: ["images"] })
) )
.guard({ .guard({
params: t.Object({ params: t.Object({
id: t.String({ image: t.UnionEnum(["poster", "thumbnail", "logo", "banner"]),
description: "The id or slug of the item to retrieve.",
example: bubble.slug,
}),
}), }),
query: t.Object({ query: t.Object({
quality: t.Optional( quality: t.Optional(
@ -168,44 +158,43 @@ export const imagesH = new Elysia({ tags: ["images"] })
422: KError, 422: KError,
}, },
}) })
.get("/shows/random/:image", getRedirectToImageHandler({}), {
detail: { description: "Get the specified image of a random show." },
})
.guard({
params: t.Object({
id: t.String({
description: "The id or slug of the item to retrieve.",
example: bubble.slug,
}),
image: t.UnionEnum(["poster", "thumbnail", "logo", "banner"]),
}),
})
.get( .get(
"/movies/:id/poster", "/movies/:id/:image",
getRedirectToImageHandler({ getRedirectToImageHandler({
filter: eq(shows.kind, "movie"), filter: eq(shows.kind, "movie"),
image: showTranslations.poster,
}), }),
{ {
detail: { description: "Get the poster of a movie" }, detail: { description: "Get the specified image of a movie" },
}, },
) )
.get( .get(
"/movies/:id/thumbnail", "/series/:id/:image",
getRedirectToImageHandler({ getRedirectToImageHandler({
filter: eq(shows.kind, "movie"), filter: eq(shows.kind, "serie"),
image: showTranslations.thumbnail,
}), }),
{ {
detail: { description: "Get the thumbnail of a movie" }, detail: { description: "Get the specified image of a serie" },
}, },
) )
.get( .get(
"/movies/:id/logo", "/collections/:id/:image",
getRedirectToImageHandler({ getRedirectToImageHandler({
filter: eq(shows.kind, "movie"), filter: eq(shows.kind, "collection"),
image: showTranslations.logo,
}), }),
{ {
detail: { description: "Get the logo of a movie" }, detail: { description: "Get the specified image of a collection" },
},
)
.get(
"/movies/:id/banner",
getRedirectToImageHandler({
filter: eq(shows.kind, "movie"),
image: showTranslations.banner,
}),
{
detail: { description: "Get the banner of a movie" },
}, },
); );