52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import {
|
|
ensureSchema,
|
|
getRawDb,
|
|
getUploadBucket,
|
|
} from "../../../lib/mvp-db";
|
|
import { adminForbidden, isAdminRequest } from "../../../lib/admin-auth";
|
|
|
|
export async function GET(request: Request) {
|
|
if (!(await isAdminRequest(request))) return adminForbidden();
|
|
try {
|
|
await ensureSchema();
|
|
const distributionId = new URL(request.url).searchParams
|
|
.get("distribution")
|
|
?.trim();
|
|
if (!distributionId) {
|
|
return Response.json({ error: "缺少分发记录" }, { status: 400 });
|
|
}
|
|
const row = await getRawDb()
|
|
.prepare(
|
|
`SELECT screenshot_key FROM distributions
|
|
WHERE id = ?`,
|
|
)
|
|
.bind(distributionId)
|
|
.first<{ screenshot_key: string | null }>();
|
|
if (
|
|
!row?.screenshot_key ||
|
|
!row.screenshot_key.startsWith("creator-center/")
|
|
) {
|
|
return Response.json({ error: "创作者截图不存在" }, { status: 404 });
|
|
}
|
|
const object = await getUploadBucket().get(row.screenshot_key);
|
|
if (!object) {
|
|
return Response.json({ error: "创作者截图不存在" }, { status: 404 });
|
|
}
|
|
const headers = new Headers({
|
|
"Cache-Control": "private, no-store",
|
|
"Content-Disposition": 'inline; filename="creator-center-screenshot"',
|
|
"X-Content-Type-Options": "nosniff",
|
|
});
|
|
object.writeHttpMetadata(headers);
|
|
if (!headers.get("Content-Type")) {
|
|
headers.set("Content-Type", "image/jpeg");
|
|
}
|
|
return new Response(await object.arrayBuffer(), { headers });
|
|
} catch (error) {
|
|
return Response.json(
|
|
{ error: error instanceof Error ? error.message : "截图读取失败" },
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
}
|