43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
|
|
import { NextRequest } from "next/server";
|
||
|
|
import { prisma } from "@/lib/db";
|
||
|
|
import { storage } from "@/lib/storage";
|
||
|
|
|
||
|
|
export const dynamic = "force-dynamic";
|
||
|
|
|
||
|
|
const CONTENT_TYPES: Record<string, string> = {
|
||
|
|
png: "image/png",
|
||
|
|
jpg: "image/jpeg",
|
||
|
|
jpeg: "image/jpeg",
|
||
|
|
webp: "image/webp",
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Serve an episode's cover art to anonymous visitors, authorized by a valid,
|
||
|
|
* still-enabled public `shareId`. Used as a fallback when the storage provider
|
||
|
|
* doesn't expose a directly-fetchable public URL for cover art.
|
||
|
|
*/
|
||
|
|
export async function GET(
|
||
|
|
_req: NextRequest,
|
||
|
|
{ params }: { params: Promise<{ shareId: string }> }
|
||
|
|
) {
|
||
|
|
const { shareId } = await params;
|
||
|
|
|
||
|
|
const episode = await prisma.episode.findUnique({
|
||
|
|
where: { shareId },
|
||
|
|
select: { coverArt: { select: { storageKey: true } } },
|
||
|
|
});
|
||
|
|
const key = episode?.coverArt?.storageKey;
|
||
|
|
if (!key) return new Response("Not found", { status: 404 });
|
||
|
|
if (!(await storage().exists(key))) return new Response("Not found", { status: 404 });
|
||
|
|
|
||
|
|
const data = await storage().get(key);
|
||
|
|
const ext = key.split(".").pop()?.toLowerCase() ?? "png";
|
||
|
|
return new Response(data as BodyInit, {
|
||
|
|
headers: {
|
||
|
|
"Content-Type": CONTENT_TYPES[ext] ?? "image/png",
|
||
|
|
"Content-Length": String(data.byteLength),
|
||
|
|
"Cache-Control": "public, max-age=3600",
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|