Files
NianAIGC/app/api/assets/route.ts
2026-05-29 10:26:02 +08:00

42 lines
1.2 KiB
TypeScript

import { createAsset, listAssets } from "@/lib/server/data-store";
import { jsonError, jsonOk, readJsonBody } from "@/lib/server/api";
import { DEFAULT_OWNER_ID } from "@/lib/server/runtime";
import type { AssetKind } from "@/lib/types";
export const runtime = "nodejs";
export async function GET() {
try {
return jsonOk({ assets: await listAssets(DEFAULT_OWNER_ID) });
} catch (error) {
return jsonError(error, 500);
}
}
export async function POST(request: Request) {
try {
const body = await readJsonBody<{
url?: string;
name?: string;
kind?: AssetKind;
tags?: string[];
source?: "upload" | "generated" | "edited" | "upscaled" | "external" | "seed";
}>(request);
if (!body.url) throw new Error("url is required");
const asset = await createAsset({
ownerId: DEFAULT_OWNER_ID,
kind: body.kind || "image",
name: body.name || "外部图片",
url: body.url,
source: body.source || "external",
tags: body.tags || ["external"],
metadata: {
registeredFrom: "api"
}
});
return jsonOk({ asset }, { status: 201 });
} catch (error) {
return jsonError(error);
}
}