44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { createAsset, listAssets } from "@/lib/server/data-store";
|
|
import { jsonError, jsonOk, readJsonBody } from "@/lib/server/api";
|
|
import { requireAppUser } from "@/lib/server/auth/current-user";
|
|
import type { AssetKind } from "@/lib/types";
|
|
|
|
export const runtime = "nodejs";
|
|
|
|
export async function GET() {
|
|
try {
|
|
const user = await requireAppUser();
|
|
return jsonOk({ assets: await listAssets(user.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);
|
|
const user = await requireAppUser();
|
|
if (!body.url) throw new Error("url is required");
|
|
const asset = await createAsset({
|
|
ownerId: user.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);
|
|
}
|
|
}
|