Files

65 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

import { adminForbidden, isAdminRequest } from "../../../lib/admin-auth";
import { ensureSchema, getRawDb, getUploadBucket } from "../../../lib/mvp-db";
import { parseResultScreenshotKeys } from "../../../lib/result-screenshots";
import { getOwnerScope, isDistributionOwner, ownerForbidden } from "../../../lib/owner-scope";
export async function GET(request: Request) {
if (!(await isAdminRequest(request))) return adminForbidden();
try {
await ensureSchema();
const url = new URL(request.url);
const distributionId = url.searchParams.get("distribution")?.trim();
const scope = await getOwnerScope(request);
if (
scope.kind === "owner" &&
!(await isDistributionOwner(getRawDb(), distributionId ?? "", scope.wecomUserId))
) {
return ownerForbidden();
}
// kind=publish 时返回 KOC 上传的发布截图(分发详情页核对用)
const imageKind = url.searchParams.get("kind") === "publish" ? "publish" : "result";
const imageIndex = Math.max(
1,
Number(url.searchParams.get("index") || 1),
);
if (!distributionId) {
return Response.json({ error: "缺少任务记录" }, { status: 400 });
}
const row = await getRawDb()
.prepare(
`SELECT result_screenshot_key, publish_screenshot_key FROM distributions
WHERE id = ?`,
)
.bind(distributionId)
.first<{
result_screenshot_key: string | null;
publish_screenshot_key: string | null;
}>();
const screenshotKey =
imageKind === "publish"
? row?.publish_screenshot_key ?? null
: row
? parseResultScreenshotKeys(row.result_screenshot_key)[imageIndex - 1]
: undefined;
if (!screenshotKey) {
return Response.json({ error: "任务截图不存在" }, { status: 404 });
}
const object = await getUploadBucket().get(screenshotKey);
if (!object) {
return Response.json({ error: "任务截图不存在" }, { status: 404 });
}
const headers = new Headers({
"Cache-Control": "private, no-store",
"Content-Disposition": 'inline; filename="task-result-screenshot"',
"X-Content-Type-Options": "nosniff",
});
object.writeHttpMetadata(headers);
return new Response(await object.arrayBuffer(), { headers });
} catch (error) {
return Response.json(
{ error: error instanceof Error ? error.message : "截图读取失败" },
{ status: 500 },
);
}
}