Comprehensive admin + user dashboards (production-ready)
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { storage } from "@/lib/storage";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
/**
|
||||
* Stream an episode's MP3 to anonymous visitors, authorized purely by a valid,
|
||||
* still-enabled public `shareId` (NOT a session). Returns 404 when the share is
|
||||
* disabled or the audio is missing so we never disclose private episode state.
|
||||
*
|
||||
* Supports HTTP Range requests so the audio element can seek/scrub.
|
||||
*/
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ shareId: string }> }
|
||||
) {
|
||||
const { shareId } = await params;
|
||||
|
||||
const episode = await prisma.episode.findUnique({
|
||||
where: { shareId },
|
||||
select: { audioAsset: { select: { storageKey: true } } },
|
||||
});
|
||||
const key = episode?.audioAsset?.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 total = data.byteLength;
|
||||
const contentType = "audio/mpeg";
|
||||
|
||||
const range = req.headers.get("range");
|
||||
if (range) {
|
||||
const match = /bytes=(\d+)-(\d*)/.exec(range);
|
||||
if (match) {
|
||||
const start = Number(match[1]);
|
||||
const end = match[2] ? Math.min(Number(match[2]), total - 1) : total - 1;
|
||||
if (start <= end && start < total) {
|
||||
const chunk = data.subarray(start, end + 1);
|
||||
return new Response(chunk as BodyInit, {
|
||||
status: 206,
|
||||
headers: {
|
||||
"Content-Type": contentType,
|
||||
"Content-Length": String(chunk.byteLength),
|
||||
"Content-Range": `bytes ${start}-${end}/${total}`,
|
||||
"Accept-Ranges": "bytes",
|
||||
"Cache-Control": "public, max-age=3600",
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new Response(data as BodyInit, {
|
||||
headers: {
|
||||
"Content-Type": contentType,
|
||||
"Content-Length": String(total),
|
||||
"Accept-Ranges": "bytes",
|
||||
"Cache-Control": "public, max-age=3600",
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
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",
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user