40 lines
1.5 KiB
TypeScript
40 lines
1.5 KiB
TypeScript
import { deleteAsset, deleteGenerationJob, getAsset, getGenerationJob } from "@/lib/server/data-store";
|
|
import { jsonError, jsonOk } from "@/lib/server/api";
|
|
import { requireAppUser } from "@/lib/server/auth/current-user";
|
|
import { deleteStoredAsset } from "@/lib/server/storage";
|
|
|
|
export const runtime = "nodejs";
|
|
|
|
export async function GET(_request: Request, context: { params: Promise<{ id: string }> }) {
|
|
try {
|
|
const user = await requireAppUser();
|
|
const { id } = await context.params;
|
|
const job = await getGenerationJob(id);
|
|
if (!job || job.ownerId !== user.id) return jsonError(new Error("Generation job not found."), 404);
|
|
return jsonOk({ job });
|
|
} catch (error) {
|
|
return jsonError(error, 500);
|
|
}
|
|
}
|
|
|
|
export async function DELETE(_request: Request, context: { params: Promise<{ id: string }> }) {
|
|
try {
|
|
const user = await requireAppUser();
|
|
const { id } = await context.params;
|
|
const job = await getGenerationJob(id);
|
|
if (!job || job.ownerId !== user.id || job.capability === "video.generate") return jsonError("任务不存在", 404);
|
|
const deletedAssetIds: string[] = [];
|
|
for (const assetId of job.outputAssetIds) {
|
|
const asset = await getAsset(assetId);
|
|
if (!asset) continue;
|
|
await deleteStoredAsset(asset);
|
|
await deleteAsset(asset.id);
|
|
deletedAssetIds.push(asset.id);
|
|
}
|
|
await deleteGenerationJob(id);
|
|
return jsonOk({ ok: true, deletedJobId: id, deletedAssetIds });
|
|
} catch (error) {
|
|
return jsonError(error, 500);
|
|
}
|
|
}
|