Initial 智念AIGC platform
This commit is contained in:
41
app/api/assets/route.ts
Normal file
41
app/api/assets/route.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user