CivitAI: add models from civitai.com / civitai.red links

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>
This commit is contained in:
Pascal LinxweilerandClaude Opus 5.5 committed 2026-11-10 15:14:14 +01:00
1 parent 765dac2ed2
commit a1acd8c0a5
19 files changed
+2276 -16

No files matched your search

+22 -2
View File
@@ -4,7 +4,7 @@ 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 { addLocalModel, addModel, ModelError } from "@/lib/models";
import { addCivitaiModel, addLocalModel, addModel, ModelError } from "@/lib/models";
import { addTag, normalizeTagName, TagError } from "@/lib/tags";
const Body = z.object({
@@ -26,13 +26,33 @@ const LocalBody = z.object({
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: "local"`, add a folder from the models folder that has no Hub repo instead (nothing queued).
* `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);