Initial 智念AIGC platform
This commit is contained in:
44
app/api/assets/[id]/inpaint/route.ts
Normal file
44
app/api/assets/[id]/inpaint/route.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { getAsset } from "@/lib/server/data-store";
|
||||
import { jsonError, jsonOk, readJsonBody } from "@/lib/server/api";
|
||||
import { requestOrigin } from "@/lib/server/runtime";
|
||||
import { saveMaskDataUrl } from "@/lib/server/storage";
|
||||
import { submitImageJob } from "@/lib/server/generation-service";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function POST(request: Request, context: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
const { id } = await context.params;
|
||||
const asset = await getAsset(id);
|
||||
if (!asset) return jsonError(new Error("Asset not found."), 404);
|
||||
const body = await readJsonBody<{
|
||||
prompt?: string;
|
||||
maskDataUrl?: string;
|
||||
maskUrl?: string;
|
||||
seed?: number;
|
||||
}>(request);
|
||||
let maskUrl = body.maskUrl;
|
||||
let maskAssetId: string | undefined;
|
||||
if (body.maskDataUrl) {
|
||||
const mask = await saveMaskDataUrl({
|
||||
ownerId: asset.ownerId,
|
||||
dataUrl: body.maskDataUrl,
|
||||
origin: requestOrigin(request),
|
||||
jobHint: asset.id
|
||||
});
|
||||
maskUrl = mask.url;
|
||||
maskAssetId = mask.id;
|
||||
}
|
||||
if (!maskUrl) throw new Error("maskDataUrl or maskUrl is required for inpainting.");
|
||||
const job = await submitImageJob({
|
||||
capability: "image.inpaint",
|
||||
prompt: body.prompt || "删除",
|
||||
imageUrls: [asset.url, maskUrl],
|
||||
inputAssetIds: [asset.id, ...(maskAssetId ? [maskAssetId] : [])],
|
||||
seed: typeof body.seed === "number" ? body.seed : undefined
|
||||
}, requestOrigin(request));
|
||||
return jsonOk({ job }, { status: 202 });
|
||||
} catch (error) {
|
||||
return jsonError(error);
|
||||
}
|
||||
}
|
||||
18
app/api/assets/[id]/route.ts
Normal file
18
app/api/assets/[id]/route.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { deleteAsset, getAsset } from "@/lib/server/data-store";
|
||||
import { jsonError, jsonOk } from "@/lib/server/api";
|
||||
import { deleteStoredAsset } from "@/lib/server/storage";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function DELETE(_request: Request, context: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
const { id } = await context.params;
|
||||
const asset = await getAsset(id);
|
||||
if (!asset) return jsonError("资产不存在", 404);
|
||||
await deleteStoredAsset(asset);
|
||||
await deleteAsset(id);
|
||||
return jsonOk({ ok: true, deletedAssetId: id });
|
||||
} catch (error) {
|
||||
return jsonError(error, 500);
|
||||
}
|
||||
}
|
||||
28
app/api/assets/[id]/upscale/route.ts
Normal file
28
app/api/assets/[id]/upscale/route.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { getAsset } from "@/lib/server/data-store";
|
||||
import { jsonError, jsonOk, readJsonBody } from "@/lib/server/api";
|
||||
import { requestOrigin } from "@/lib/server/runtime";
|
||||
import { submitImageJob } from "@/lib/server/generation-service";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function POST(request: Request, context: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
const { id } = await context.params;
|
||||
const asset = await getAsset(id);
|
||||
if (!asset) return jsonError(new Error("Asset not found."), 404);
|
||||
const body = await readJsonBody<{
|
||||
resolution?: "4k" | "8k";
|
||||
scale?: number;
|
||||
}>(request);
|
||||
const job = await submitImageJob({
|
||||
capability: "image.upscale",
|
||||
imageUrls: [asset.url],
|
||||
inputAssetIds: [asset.id],
|
||||
resolution: body.resolution === "8k" ? "8k" : "4k",
|
||||
scale: typeof body.scale === "number" ? body.scale : undefined
|
||||
}, requestOrigin(request));
|
||||
return jsonOk({ job }, { status: 202 });
|
||||
} catch (error) {
|
||||
return jsonError(error);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
26
app/api/assets/upload/route.ts
Normal file
26
app/api/assets/upload/route.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { jsonError, jsonOk } from "@/lib/server/api";
|
||||
import { DEFAULT_OWNER_ID, requestOrigin } from "@/lib/server/runtime";
|
||||
import { saveUploadAsset } from "@/lib/server/storage";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const form = await request.formData();
|
||||
const files = form.getAll("files").filter((item): item is File => item instanceof File);
|
||||
if (!files.length) throw new Error("No files uploaded.");
|
||||
const assets = await Promise.all(files.map(async (file) => {
|
||||
return saveUploadAsset({
|
||||
ownerId: DEFAULT_OWNER_ID,
|
||||
bytes: Buffer.from(await file.arrayBuffer()),
|
||||
fileName: file.name,
|
||||
contentType: file.type || "application/octet-stream",
|
||||
origin: requestOrigin(request),
|
||||
tags: ["upload"]
|
||||
});
|
||||
}));
|
||||
return jsonOk({ assets }, { status: 201 });
|
||||
} catch (error) {
|
||||
return jsonError(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user