mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-08-29 01:35:39 -04:00
Introduce a local disk cache for TMDB images served through a proxy API route (/api/images/[...path]), eliminating direct client-side CDN dependencies. Images are cached by category (posters, backdrops, stills, logos) and served with immutable cache headers. Move all tmdbImageUrl() calls from client components to API routes and server components so clients receive ready-to-use URLs. This removes the need to expose TMDB_IMAGE_BASE_URL and IMAGE_CACHE_ENABLED via next.config.ts env block. The landing page is split into a server wrapper (app/page.tsx) and client component (components/landing-page.tsx). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import path from "node:path";
|
|
import { eq } from "drizzle-orm";
|
|
import { Vibrant } from "node-vibrant/node";
|
|
import { db } from "@/lib/db/client";
|
|
import { titles } from "@/lib/db/schema";
|
|
import {
|
|
getLocalImagePath,
|
|
imageCacheEnabled,
|
|
isImageCached,
|
|
} from "@/lib/services/image-cache";
|
|
import { tmdbImageUrl } from "@/lib/tmdb/image";
|
|
|
|
export interface ColorPalette {
|
|
vibrant: string | null;
|
|
darkVibrant: string | null;
|
|
lightVibrant: string | null;
|
|
muted: string | null;
|
|
darkMuted: string | null;
|
|
lightMuted: string | null;
|
|
}
|
|
|
|
export async function extractAndStoreColors(
|
|
titleId: string,
|
|
posterPath: string | null,
|
|
): Promise<ColorPalette | null> {
|
|
if (!posterPath) return null;
|
|
|
|
// Prefer local file when image cache is active
|
|
let source: string;
|
|
const filename = path.basename(posterPath);
|
|
if (imageCacheEnabled() && isImageCached("posters", filename)) {
|
|
source = getLocalImagePath("posters", filename);
|
|
} else {
|
|
const url = tmdbImageUrl(posterPath, "w300");
|
|
if (!url) return null;
|
|
source = url;
|
|
}
|
|
|
|
try {
|
|
const palette = await Vibrant.from(source).getPalette();
|
|
|
|
const colors: ColorPalette = {
|
|
vibrant: palette.Vibrant?.hex ?? null,
|
|
darkVibrant: palette.DarkVibrant?.hex ?? null,
|
|
lightVibrant: palette.LightVibrant?.hex ?? null,
|
|
muted: palette.Muted?.hex ?? null,
|
|
darkMuted: palette.DarkMuted?.hex ?? null,
|
|
lightMuted: palette.LightMuted?.hex ?? null,
|
|
};
|
|
|
|
await db
|
|
.update(titles)
|
|
.set({ colorPalette: JSON.stringify(colors) })
|
|
.where(eq(titles.id, titleId))
|
|
.run();
|
|
|
|
return colors;
|
|
} catch (err) {
|
|
console.error(`Failed to extract colors for title ${titleId}:`, err);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function parseColorPalette(raw: string | null): ColorPalette | null {
|
|
if (!raw) return null;
|
|
try {
|
|
return JSON.parse(raw) as ColorPalette;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|