Paste a CivitAI model link (civitai.com, .red or .green), a download link or an AIR id into "Add model": pick a version and its files, and whether to follow new versions. The model is stored as source "civitai" with repoId civitai/<modelId>, pinned to the version id, in the folder civitai/<id>-<name>; files keep CivitAI's SHA-256 for deep verify. Early access and creator-required logins are caught before anything is queued. Migration 0027 adds models.display_name, nsfw and source_card, and model_files.civitai (file id and attributes). The model page links to CivitAI instead of the Hub, and Hub checks, updates and adding files are refused for CivitAI models for now. The worker fails CivitAI download jobs until it can run them (next step). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
80 lines
3.4 KiB
TypeScript
80 lines
3.4 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { withAuth } from "@/lib/api-auth";
|
|
import { apiError, handleApiError } from "@/lib/api-errors";
|
|
import { REPO_ID_RE, SHA_RE } from "@/lib/hub/client";
|
|
import { queueDownload } from "@/lib/jobs";
|
|
import { addCivitaiModel, addLocalModel, addModel, ModelError } from "@/lib/models";
|
|
import { addTag, normalizeTagName, TagError } from "@/lib/tags";
|
|
|
|
const Body = z.object({
|
|
repoId: z.string().regex(REPO_ID_RE),
|
|
revision: z.string().trim().min(1).max(255),
|
|
sha: z.string().regex(SHA_RE, "must be a full 40-character commit SHA"),
|
|
include: z.array(z.string().min(1).max(1024)).max(10_000),
|
|
exclude: z.array(z.string().min(1).max(1024)).max(1_000),
|
|
download: z.boolean().default(true),
|
|
tags: z.array(z.string()).max(50).default([]),
|
|
/** Import an existing folder from the models folder (as found by the worker's scan). */
|
|
localPath: z.string().min(1).max(512).optional(),
|
|
});
|
|
|
|
/** A folder from the models folder without a Hub repo, added as it is. */
|
|
const LocalBody = z.object({
|
|
source: z.literal("local"),
|
|
localPath: z.string().min(1).max(512),
|
|
tags: z.array(z.string()).max(50).default([]),
|
|
});
|
|
|
|
/** A CivitAI model at the version the user reviewed. */
|
|
const CivitaiBody = z.object({
|
|
source: z.literal("civitai"),
|
|
modelId: z.number().int().positive(),
|
|
versionId: z.number().int().positive(),
|
|
track: z.enum(["latest", "pinned"]).default("latest"),
|
|
fileIds: z.array(z.number().int().positive()).min(1).max(1_000),
|
|
download: z.boolean().default(true),
|
|
tags: z.array(z.string()).max(50).default([]),
|
|
});
|
|
|
|
/**
|
|
* Add a model to the library, pinned to the SHA the user reviewed, and queue its download. With
|
|
* `source: "civitai"`, a CivitAI model pinned to a version. With `source: "local"`, add a folder
|
|
* from the models folder that has no Hub repo instead (nothing queued).
|
|
*/
|
|
export const POST = withAuth(async (req) => {
|
|
try {
|
|
const json = await req.json();
|
|
if (json?.source === "civitai") {
|
|
const body = CivitaiBody.parse(json);
|
|
body.tags.forEach(normalizeTagName);
|
|
const { id } = await addCivitaiModel(body);
|
|
for (const tag of body.tags) await addTag(id, tag);
|
|
const job = body.download ? await queueDownload(id, "initial") : null;
|
|
return NextResponse.json({ id, jobId: job?.jobId ?? null }, { status: 201 });
|
|
}
|
|
if (json?.source === "local") {
|
|
const body = LocalBody.parse(json);
|
|
body.tags.forEach(normalizeTagName);
|
|
const { id } = await addLocalModel({ localPath: body.localPath });
|
|
for (const tag of body.tags) await addTag(id, tag);
|
|
return NextResponse.json({ id, jobId: null }, { status: 201 });
|
|
}
|
|
const body = Body.parse(json);
|
|
body.tags.forEach(normalizeTagName); // validate before anything is written
|
|
const { id } = await addModel({
|
|
repoId: body.repoId,
|
|
revision: body.revision,
|
|
sha: body.sha,
|
|
rule: { include: body.include, exclude: body.exclude },
|
|
localPath: body.localPath,
|
|
});
|
|
for (const tag of body.tags) await addTag(id, tag);
|
|
const job = body.download ? await queueDownload(id, body.localPath ? "import" : "initial") : null;
|
|
return NextResponse.json({ id, jobId: job?.jobId ?? null }, { status: 201 });
|
|
} catch (err) {
|
|
if (err instanceof ModelError || err instanceof TagError) return apiError(err.status, err.code, err.message);
|
|
return handleApiError(err);
|
|
}
|
|
});
|