feat(organize): record filters, stats and delete with files (HIS-1)
List query gains kind/since/organizeOnly and wildcard/hash search; new GET /organization-records/stats and POST /organization-records/delete (optional source/library files to trash via files delete). Public record adds posterPath, sizeBytes and download summary. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
f60254e265
commit
51e82f1cd9
8 files changed
+850
-17
No files matched your search
@@ -0,0 +1,121 @@
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import { createHarness, type Harness, pgAvailable, seedEntity } from "./harness.ts";
|
||||
|
||||
// HIS-1 整理记录列表筛选与统计:类型(电影 / 剧集 / 未识别)、时间、关键字(标题 / 路径 / 文件名 / 种子 hash,支持 * ?)、
|
||||
// 只看整理类、状态计数与列表同口径;列表带海报、大小、下载器与 hash。
|
||||
|
||||
const skip = !(await pgAvailable());
|
||||
|
||||
describe.skipIf(skip)("整理记录筛选与统计", () => {
|
||||
let h: Harness;
|
||||
const ids: Record<string, string> = {};
|
||||
beforeAll(async () => {
|
||||
h = await createHarness("recfilter");
|
||||
const film = await seedEntity(h.db, { kind: "movie", title: "Arrival", year: 2016 });
|
||||
await h.db
|
||||
.updateTable("media_entity")
|
||||
.set({ metadata: JSON.stringify({ posterPath: { value: "/arrival.jpg" } }) })
|
||||
.where("id", "=", film)
|
||||
.execute();
|
||||
const show = await seedEntity(h.db, { kind: "series", title: "Severance", year: 2022 });
|
||||
const inst = await h.db
|
||||
.insertInto("downloader_instance")
|
||||
.values({ name: "家里的 qB", base_url: "http://127.0.0.1:1" })
|
||||
.returning("id")
|
||||
.executeTakeFirstOrThrow();
|
||||
const task = await h.db
|
||||
.insertInto("download_task")
|
||||
.values({ instance_id: inst.id, torrent_hash: "abcdef0123456789abcdef0123456789abcdef01", name: "Arrival.2016" })
|
||||
.returning("id")
|
||||
.executeTakeFirstOrThrow();
|
||||
const old = new Date(Date.now() - 40 * 86_400_000);
|
||||
const rows = [
|
||||
{ k: "film", entity: film, status: "succeeded", action: "copy", src: "/dl/Arrival.2016.mkv", task: task.id },
|
||||
{ k: "show", entity: show, status: "failed", action: "move", src: "/dl/Severance.S01E01.mkv", at: old },
|
||||
{ k: "unknown", entity: null, status: "failed", action: "copy", src: "/dl/random.mp4" },
|
||||
{ k: "rename", entity: film, status: "succeeded", action: "rename", src: "/lib/Arrival/a.mkv" },
|
||||
] as const;
|
||||
for (const r of rows) {
|
||||
const rec = await h.db
|
||||
.insertInto("organization_record")
|
||||
.values({
|
||||
trigger: "auto",
|
||||
actor_type: "system",
|
||||
media_entity_id: r.entity,
|
||||
download_task_id: "task" in r ? r.task : null,
|
||||
source_path: r.src,
|
||||
action: r.action,
|
||||
status: r.status,
|
||||
...("at" in r ? { created_at: r.at } : {}),
|
||||
})
|
||||
.returning("id")
|
||||
.executeTakeFirstOrThrow();
|
||||
ids[r.k] = rec.id;
|
||||
await h.db
|
||||
.insertInto("organization_file_result")
|
||||
.values({
|
||||
organization_record_id: rec.id,
|
||||
mode: r.action,
|
||||
source_path: `${r.src}`,
|
||||
source_size_bytes: 1000,
|
||||
status: r.status === "failed" ? "failed" : "succeeded",
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
});
|
||||
afterAll(async () => h?.close());
|
||||
|
||||
const list = async (q: string) => {
|
||||
const r = await h.api("GET", `/organization-records?${q}`);
|
||||
expect(r.status).toBe(200);
|
||||
return r.body.data.items as { id: string }[];
|
||||
};
|
||||
const idsOf = (items: { id: string }[]) => items.map((x) => x.id).sort();
|
||||
|
||||
it("类型:电影 / 剧集 / 未识别", async () => {
|
||||
expect(idsOf(await list("kind=movie"))).toEqual([ids.film, ids.rename].sort());
|
||||
expect(idsOf(await list("kind=series"))).toEqual([ids.show]);
|
||||
expect(idsOf(await list("kind=none"))).toEqual([ids.unknown]);
|
||||
});
|
||||
|
||||
it("时间:since 之后创建的", async () => {
|
||||
const since = new Date(Date.now() - 7 * 86_400_000).toISOString();
|
||||
const items = await list(`since=${encodeURIComponent(since)}`);
|
||||
expect(items.map((x) => x.id)).not.toContain(ids.show);
|
||||
expect(items).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("关键字:标题、文件名、种子 hash、通配", async () => {
|
||||
expect(idsOf(await list("q=Severance"))).toEqual([ids.show]);
|
||||
expect(idsOf(await list(`q=${encodeURIComponent("abcdef0123")}`))).toEqual([ids.film]);
|
||||
expect(idsOf(await list(`q=${encodeURIComponent("*.mp4")}`))).toEqual([ids.unknown]);
|
||||
expect(idsOf(await list(`q=${encodeURIComponent("/dl/?rrival*")}`))).toEqual([ids.film]);
|
||||
// 通配里的 % 与 _ 按字面匹配
|
||||
expect(await list(`q=${encodeURIComponent("100%")}`)).toEqual([]);
|
||||
});
|
||||
|
||||
it("只看整理类:不含改名 / 删除 / 还原", async () => {
|
||||
const items = await list("organizeOnly=true");
|
||||
expect(items.map((x) => x.id)).not.toContain(ids.rename);
|
||||
expect(items).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("统计与列表同口径,不受状态筛选影响", async () => {
|
||||
const all = await h.api("GET", "/organization-records/stats");
|
||||
expect(all.status).toBe(200);
|
||||
expect(all.body.data).toEqual({ total: 4, succeeded: 2, partial: 0, failed: 2, running: 0, cancelled: 0 });
|
||||
const movies = await h.api("GET", "/organization-records/stats?kind=movie&organizeOnly=true");
|
||||
expect(movies.body.data).toMatchObject({ total: 1, succeeded: 1, failed: 0 });
|
||||
});
|
||||
|
||||
it("列表项带海报、大小、下载器名称与 hash", async () => {
|
||||
const [film] = await list("q=Arrival.2016");
|
||||
expect(film).toMatchObject({
|
||||
media: { title: "Arrival", posterPath: "/arrival.jpg" },
|
||||
sizeBytes: 1000,
|
||||
download: { name: "Arrival.2016", hash: "abcdef0123456789abcdef0123456789abcdef01", downloaderName: "家里的 qB" },
|
||||
});
|
||||
const [unknown] = await list("kind=none");
|
||||
expect(unknown).toMatchObject({ media: null, download: null, sizeBytes: 1000 });
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { Api, type OrganizationChangesResponse } from "@umt/contracts";
|
||||
import { sql } from "@umt/db";
|
||||
import { Api, type OrganizationChangesResponse, type OrganizationStatsQuery } from "@umt/contracts";
|
||||
import { type DB, sql } from "@umt/db";
|
||||
import type { ExpressionBuilder, SelectQueryBuilder } from "kysely";
|
||||
import { ApiException } from "../../../http/errors.ts";
|
||||
import type { ModuleApp } from "../../types.ts";
|
||||
import { type AoDeps, ensureTaskRow, markTask } from "../services/common.ts";
|
||||
@@ -28,27 +29,39 @@ export function registerOrganizationRoutes(app: ModuleApp, d: AoDeps) {
|
||||
app,
|
||||
Api.organization.list,
|
||||
async (_c, { query }) => {
|
||||
let q = db().selectFrom("organization_record").selectAll();
|
||||
let q = recordFilters(db().selectFrom("organization_record").selectAll(), query);
|
||||
if (query.status) q = q.where("status", "=", query.status);
|
||||
if (query.trigger) q = q.where("trigger", "=", query.trigger);
|
||||
if (query.mediaEntityId) q = q.where("media_entity_id", "=", query.mediaEntityId);
|
||||
if (query.downloadTaskId) q = q.where("download_task_id", "=", query.downloadTaskId);
|
||||
if (query.q) {
|
||||
const like = `%${String(query.q).replace(/[\\%_]/g, (m) => `\\${m}`)}%`;
|
||||
q = q.where((eb) =>
|
||||
eb.or([
|
||||
eb("source_path", "ilike", like),
|
||||
eb("target_path", "ilike", like),
|
||||
eb("media_entity_id", "in", eb.selectFrom("media_entity").select("id").where("title", "ilike", like)),
|
||||
]),
|
||||
);
|
||||
}
|
||||
const p = await paginate(q, query);
|
||||
return { items: await loadRecordsPublic(db(), p.rows, false), nextCursor: p.nextCursor };
|
||||
},
|
||||
{ tag: TAG },
|
||||
);
|
||||
|
||||
// HIS-1:按状态计数,筛选与列表一致(不含状态本身);必须先于 {id} 注册
|
||||
mount(
|
||||
app,
|
||||
Api.organization.stats,
|
||||
async (_c, { query }) => {
|
||||
const rows = await recordFilters(
|
||||
db()
|
||||
.selectFrom("organization_record")
|
||||
.select(["status", (eb) => eb.fn.countAll<string>().as("n")])
|
||||
.groupBy("status"),
|
||||
query,
|
||||
).execute();
|
||||
const n = (s: string) => Number(rows.find((r) => r.status === s)?.n ?? 0);
|
||||
return {
|
||||
total: rows.reduce((a, r) => a + Number(r.n), 0),
|
||||
succeeded: n("succeeded"),
|
||||
partial: n("partial"),
|
||||
failed: n("failed"),
|
||||
running: n("running") + n("queued"),
|
||||
cancelled: n("cancelled"),
|
||||
};
|
||||
},
|
||||
{ tag: TAG },
|
||||
);
|
||||
|
||||
// 增量读取:change_xid 早于当前快照最小活跃事务,避免"序号小但提交晚"被跳过
|
||||
mount(
|
||||
app,
|
||||
@@ -239,6 +252,54 @@ export function registerOrganizationRoutes(app: ModuleApp, d: AoDeps) {
|
||||
);
|
||||
}
|
||||
|
||||
/** 通配(* ?)转 ILIKE 模式;不带通配时两侧补 % */
|
||||
export function likePattern(input: string): string {
|
||||
const esc = input.replace(/[\\%_]/g, (m) => `\\${m}`);
|
||||
return /[*?]/.test(input) ? esc.replace(/\*/g, "%").replace(/\?/g, "_") : `%${esc}%`;
|
||||
}
|
||||
|
||||
/** 列表 / 统计共用筛选(不含状态) */
|
||||
// biome-ignore lint/suspicious/noExplicitAny: 同一张表的 select 与 groupBy 查询共用
|
||||
function recordFilters<Q extends SelectQueryBuilder<DB, "organization_record", any>>(
|
||||
q0: Q,
|
||||
query: OrganizationStatsQuery,
|
||||
): Q {
|
||||
// biome-ignore lint/suspicious/noExplicitAny: kysely 泛型在链式 where 后无法保持 Q
|
||||
let q = q0 as any;
|
||||
if (query.trigger) q = q.where("trigger", "=", query.trigger);
|
||||
if (query.mediaEntityId) q = q.where("media_entity_id", "=", query.mediaEntityId);
|
||||
if (query.downloadTaskId) q = q.where("download_task_id", "=", query.downloadTaskId);
|
||||
if (query.since) q = q.where("created_at", ">=", new Date(query.since));
|
||||
if (query.organizeOnly) q = q.where("action", "in", ["copy", "move", "hardlink"]);
|
||||
if (query.kind === "none") q = q.where("media_entity_id", "is", null);
|
||||
else if (query.kind)
|
||||
q = q.where("media_entity_id", "in", (eb: ExpressionBuilder<DB, "organization_record">) =>
|
||||
eb
|
||||
.selectFrom("media_entity")
|
||||
.select("id")
|
||||
.where("kind", query.kind === "movie" ? "=" : "<>", "movie"),
|
||||
);
|
||||
if (query.q) {
|
||||
const like = likePattern(String(query.q).trim());
|
||||
q = q.where((eb: ExpressionBuilder<DB, "organization_record">) =>
|
||||
eb.or([
|
||||
eb("source_path", "ilike", like),
|
||||
eb("target_path", "ilike", like),
|
||||
eb("media_entity_id", "in", eb.selectFrom("media_entity").select("id").where("title", "ilike", like)),
|
||||
eb("download_task_id", "in", eb.selectFrom("download_task").select("id").where("torrent_hash", "ilike", like)),
|
||||
eb.exists(
|
||||
eb
|
||||
.selectFrom("organization_file_result as f")
|
||||
.select("f.id")
|
||||
.whereRef("f.organization_record_id", "=", "organization_record.id")
|
||||
.where((e) => e.or([e("f.source_path", "ilike", like), e("f.target_path", "ilike", like)])),
|
||||
),
|
||||
]),
|
||||
);
|
||||
}
|
||||
return q as Q;
|
||||
}
|
||||
|
||||
async function audit(d: AoDeps, actor: string, action: string, detail: Record<string, unknown>) {
|
||||
await d.ctx.db
|
||||
.insertInto("audit_event")
|
||||
|
||||
@@ -1821,7 +1821,8 @@ export async function loadRecordsPublic(exec: Exec, rows: OrganizationRecordRow[
|
||||
if (rows.length === 0) return [];
|
||||
const ids = rows.map((r) => r.id);
|
||||
const entityIds = [...new Set(rows.map((r) => r.media_entity_id).filter((x): x is string => !!x))];
|
||||
const [files, ops, entities, idents, units] = await Promise.all([
|
||||
const dlIds = [...new Set(rows.map((r) => r.download_task_id).filter((x): x is string => !!x))];
|
||||
const [files, ops, entities, idents, units, sizes, dls] = await Promise.all([
|
||||
includeFiles
|
||||
? exec
|
||||
.selectFrom("organization_file_result")
|
||||
@@ -1854,8 +1855,27 @@ export async function loadRecordsPublic(exec: Exec, rows: OrganizationRecordRow[
|
||||
"00000000-0000-0000-0000-000000000000",
|
||||
])
|
||||
.execute(),
|
||||
// HIS-1:列表也要大小,不带逐文件明细时单独汇总
|
||||
exec
|
||||
.selectFrom("organization_file_result")
|
||||
.select(["organization_record_id", (eb) => eb.fn.sum<string | null>("source_size_bytes").as("bytes")])
|
||||
.where("organization_record_id", "in", ids)
|
||||
.groupBy("organization_record_id")
|
||||
.execute(),
|
||||
dlIds.length
|
||||
? exec
|
||||
.selectFrom("download_task as t")
|
||||
.leftJoin("downloader_instance as i", "i.id", "t.instance_id")
|
||||
.select(["t.id", "t.name", "t.torrent_hash", "i.name as downloader_name"])
|
||||
.where("t.id", "in", dlIds)
|
||||
.execute()
|
||||
: Promise.resolve([]),
|
||||
]);
|
||||
const iso = (v: Date | null) => (v ? v.toISOString() : null);
|
||||
const posterOf = (metadata: unknown) => {
|
||||
const v = (metadata as { posterPath?: { value?: unknown } } | null)?.posterPath?.value;
|
||||
return typeof v === "string" && v ? v : null;
|
||||
};
|
||||
// blocked:* 的说明在 error.conflicts[].detail(中文),有就用它,不把内部串直接给人看
|
||||
const conflictText = (err: unknown) =>
|
||||
((err as { conflicts?: { detail?: string }[] } | null)?.conflicts ?? [])
|
||||
@@ -1897,6 +1917,7 @@ export async function loadRecordsPublic(exec: Exec, rows: OrganizationRecordRow[
|
||||
externalIds: idents
|
||||
.filter((i) => i.media_entity_id === e.id)
|
||||
.map((i) => ({ provider: i.provider, entityType: i.entity_type, externalId: i.external_id })),
|
||||
posterPath: posterOf(e.metadata),
|
||||
}
|
||||
: null,
|
||||
trigger: r.trigger,
|
||||
@@ -1913,6 +1934,14 @@ export async function loadRecordsPublic(exec: Exec, rows: OrganizationRecordRow[
|
||||
target: targetRef,
|
||||
reason: reason(r.reason, r.error && r.status === "failed" ? r.error : null),
|
||||
counts: { total: r.file_total, succeeded: r.file_succeeded, failed: r.file_failed, skipped: r.file_skipped },
|
||||
sizeBytes: (() => {
|
||||
const b = sizes.find((x) => x.organization_record_id === r.id)?.bytes;
|
||||
return b == null ? null : Number(b);
|
||||
})(),
|
||||
download: (() => {
|
||||
const t = dls.find((x) => x.id === r.download_task_id);
|
||||
return t ? { taskId: t.id, name: t.name, hash: t.torrent_hash, downloaderName: t.downloader_name } : null;
|
||||
})(),
|
||||
files: includeFiles
|
||||
? files
|
||||
.filter((f) => f.organization_record_id === r.id)
|
||||
|
||||
@@ -0,0 +1,278 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { lstat } from "node:fs/promises";
|
||||
import { basename, isAbsolute, join } from "node:path";
|
||||
import type {
|
||||
FileRef,
|
||||
FilesDeleteResult,
|
||||
OrganizationDeleteBlock,
|
||||
OrganizationDeleteRequest,
|
||||
OrganizationDeleteResult,
|
||||
} from "@umt/contracts";
|
||||
import type { Principal } from "../../http/auth.ts";
|
||||
import { FilesError, type FmDeps, isAdmin, loadRootDefs, type RootDef, within } from "./common.ts";
|
||||
import { audit } from "./ledger.ts";
|
||||
import { deleteFiles } from "./trash.ts";
|
||||
|
||||
// HIS-1 删除整理记录(对照 MP DELETE /history/transfer?deletesrc&deletedest):
|
||||
// 可选把源文件 / 媒体库里的文件一并移入回收站(走文件管理的删除:做种义务、已入库文件、绑定目录的检查都照旧),
|
||||
// 下载器里的任务不动。整条记录的文件有任何一个不能删,这条记录就不删(不留半截)。
|
||||
// 只有复制 / 移动 / 硬链接这三类整理记录有"源文件 / 媒体库文件";改名、删除、还原类记录只删记录本身。
|
||||
|
||||
const FILE_ACTIONS = new Set(["copy", "move", "hardlink"]);
|
||||
|
||||
const BLOCK_TEXT: Record<OrganizationDeleteBlock, string> = {
|
||||
running: "还在整理,等结束后再删",
|
||||
not_found: "记录不存在",
|
||||
download_active: "下载还没完成或做种时间不够",
|
||||
needs_unbind: "是作品的绑定目录",
|
||||
is_root: "在只读的媒体库目录里",
|
||||
root_unavailable: "目录不可访问或未挂载",
|
||||
escape: "路径指向登记目录之外",
|
||||
outside_roots: "文件不在登记目录里",
|
||||
failed: "删除失败",
|
||||
};
|
||||
|
||||
type Ref = FileRef & { abs: string };
|
||||
type Plan = {
|
||||
id: string;
|
||||
blocked: OrganizationDeleteBlock | null;
|
||||
message: string | null;
|
||||
sources: string[];
|
||||
targets: string[];
|
||||
downloadTaskId: string | null;
|
||||
};
|
||||
|
||||
const exists = (abs: string) =>
|
||||
lstat(abs).then(
|
||||
() => true,
|
||||
() => false,
|
||||
);
|
||||
|
||||
async function planRecords(
|
||||
d: FmDeps,
|
||||
body: OrganizationDeleteRequest,
|
||||
): Promise<{ plans: Plan[]; refs: Map<string, Ref> }> {
|
||||
const db = d.ctx.db;
|
||||
const ids = [...new Set(body.ids)];
|
||||
const recs = await db
|
||||
.selectFrom("organization_record")
|
||||
.select(["id", "status", "action", "source_path_view", "download_task_id"])
|
||||
.where("id", "in", ids)
|
||||
.execute();
|
||||
const files =
|
||||
body.deleteSource || body.deleteTarget
|
||||
? await db
|
||||
.selectFrom("organization_file_result as f")
|
||||
.leftJoin("library_root as lr", "lr.id", "f.target_library_root_id")
|
||||
.select([
|
||||
"f.organization_record_id",
|
||||
"f.status",
|
||||
"f.source_path",
|
||||
"f.target_path",
|
||||
"lr.local_path as root_path",
|
||||
])
|
||||
.where("f.organization_record_id", "in", ids)
|
||||
.orderBy("f.ordinal")
|
||||
.execute()
|
||||
: [];
|
||||
const defs: RootDef[] = (await loadRootDefs(db)).sort((a, b) => b.hostPath.length - a.hostPath.length);
|
||||
const refs = new Map<string, Ref>();
|
||||
const toRef = (abs: string): Ref | null => {
|
||||
const def = defs.find((x) => within(abs, x.hostPath));
|
||||
return def ? { root: def.id, path: abs === def.hostPath ? "" : abs.slice(def.hostPath.length + 1), abs } : null;
|
||||
};
|
||||
|
||||
const plans: Plan[] = [];
|
||||
for (const id of ids) {
|
||||
const r = recs.find((x) => x.id === id);
|
||||
const plan: Plan = {
|
||||
id,
|
||||
blocked: null,
|
||||
message: null,
|
||||
sources: [],
|
||||
targets: [],
|
||||
downloadTaskId: r?.download_task_id ?? null,
|
||||
};
|
||||
plans.push(plan);
|
||||
if (!r) {
|
||||
plan.blocked = "not_found";
|
||||
continue;
|
||||
}
|
||||
if (r.status === "queued" || r.status === "running") {
|
||||
plan.blocked = "running";
|
||||
continue;
|
||||
}
|
||||
if (!FILE_ACTIONS.has(r.action)) continue;
|
||||
const mine = files.filter((f) => f.organization_record_id === id);
|
||||
const want: { abs: string; side: "sources" | "targets" }[] = [];
|
||||
if (body.deleteSource && r.source_path_view === "local")
|
||||
for (const f of mine) want.push({ abs: f.source_path, side: "sources" });
|
||||
if (body.deleteTarget)
|
||||
for (const f of mine) {
|
||||
if (f.status !== "succeeded" || !f.target_path) continue;
|
||||
const abs = isAbsolute(f.target_path) ? f.target_path : f.root_path ? join(f.root_path, f.target_path) : null;
|
||||
if (abs) want.push({ abs, side: "targets" });
|
||||
}
|
||||
for (const w of want) {
|
||||
// 已经不在的(移动过、之前删过)不算,也不拦
|
||||
if (plan[w.side].includes(w.abs) || !(await exists(w.abs))) continue;
|
||||
const ref = toRef(w.abs);
|
||||
if (!ref) {
|
||||
plan.blocked ??= "outside_roots";
|
||||
plan.message ??= `${basename(w.abs)}:${BLOCK_TEXT.outside_roots}`;
|
||||
continue;
|
||||
}
|
||||
refs.set(w.abs, ref);
|
||||
plan[w.side].push(w.abs);
|
||||
}
|
||||
}
|
||||
return { plans, refs };
|
||||
}
|
||||
|
||||
/** 文件管理删除的核对(dryRun),按绝对路径回填每条记录的阻断原因 */
|
||||
async function checkFiles(
|
||||
d: FmDeps,
|
||||
principal: Principal | undefined,
|
||||
plans: Plan[],
|
||||
refs: Map<string, Ref>,
|
||||
): Promise<Map<string, FilesDeleteResult["checks"][number]>> {
|
||||
const out = new Map<string, FilesDeleteResult["checks"][number]>();
|
||||
const paths = [...new Set(plans.filter((p) => !p.blocked).flatMap((p) => [...p.sources, ...p.targets]))];
|
||||
if (paths.length === 0) return out;
|
||||
const dry = await deleteFiles(d, principal, {
|
||||
idempotencyKey: randomUUID(),
|
||||
items: paths.map((abs) => ({ root: refs.get(abs)!.root, path: refs.get(abs)!.path })),
|
||||
mode: "trash",
|
||||
dryRun: true,
|
||||
retireAssets: true,
|
||||
unbind: false,
|
||||
});
|
||||
paths.forEach((abs, i) => {
|
||||
const c = dry.checks[i];
|
||||
if (c) out.set(abs, c);
|
||||
});
|
||||
for (const p of plans) {
|
||||
if (p.blocked) continue;
|
||||
for (const abs of [...p.sources, ...p.targets]) {
|
||||
const b = out.get(abs)?.blocked;
|
||||
if (!b || b === "not_found" || b === "needs_retire") continue;
|
||||
p.blocked = b === "escape" ? "escape" : b;
|
||||
p.message = `${basename(abs)}:${BLOCK_TEXT[p.blocked]}`;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export async function deleteRecords(
|
||||
d: FmDeps,
|
||||
principal: Principal | undefined,
|
||||
body: OrganizationDeleteRequest,
|
||||
): Promise<OrganizationDeleteResult> {
|
||||
const withFiles = body.deleteSource || body.deleteTarget;
|
||||
if (withFiles && !isAdmin(principal))
|
||||
throw new FilesError(
|
||||
"forbidden",
|
||||
"API Key 不能在这里删文件。先用 /files/delete 删文件(需要一次性审批),再删记录",
|
||||
);
|
||||
const db = d.ctx.db;
|
||||
const { plans, refs } = await planRecords(d, body);
|
||||
const checks = await checkFiles(d, principal, plans, refs);
|
||||
const sizeOf = (abs: string) => checks.get(abs)?.size ?? 0;
|
||||
|
||||
// 删记录后没有别的整理记录、下载已完成但没入库的任务,会被自动整理再处理一次
|
||||
const okIds = plans.filter((p) => !p.blocked).map((p) => p.id);
|
||||
const taskIds = [...new Set(plans.filter((p) => !p.blocked && p.downloadTaskId).map((p) => p.downloadTaskId!))];
|
||||
const retry = taskIds.length
|
||||
? await db
|
||||
.selectFrom("download_task as t")
|
||||
.select("t.id")
|
||||
.where("t.id", "in", taskIds)
|
||||
.where("t.status", "=", "completed")
|
||||
.where("t.managed", "=", true)
|
||||
.where("t.removed_at", "is", null)
|
||||
.where((eb) =>
|
||||
eb.not(
|
||||
eb.exists(
|
||||
eb
|
||||
.selectFrom("organization_record as r")
|
||||
.select("r.id")
|
||||
.whereRef("r.download_task_id", "=", "t.id")
|
||||
.where("r.id", "not in", okIds),
|
||||
),
|
||||
),
|
||||
)
|
||||
.execute()
|
||||
: [];
|
||||
|
||||
const summary = (removed: string[], trashRecordId: string | null): OrganizationDeleteResult => {
|
||||
const live = plans.filter((p) => (body.dryRun ? !p.blocked : removed.includes(p.id)));
|
||||
const src = [...new Set(live.flatMap((p) => p.sources))];
|
||||
const dst = [...new Set(live.flatMap((p) => p.targets))];
|
||||
return {
|
||||
dryRun: body.dryRun,
|
||||
items: plans.map((p) => ({
|
||||
id: p.id,
|
||||
sourceFiles: p.sources.length,
|
||||
targetFiles: p.targets.length,
|
||||
bytes: [...p.sources, ...p.targets].reduce((a, x) => a + sizeOf(x), 0),
|
||||
blocked: p.blocked,
|
||||
message: p.message ?? (p.blocked ? BLOCK_TEXT[p.blocked] : null),
|
||||
})),
|
||||
removed,
|
||||
sourceFiles: src.length,
|
||||
targetFiles: dst.length,
|
||||
bytes: [...src, ...dst].reduce((a, x) => a + sizeOf(x), 0),
|
||||
assets: dst.reduce((a, x) => a + (checks.get(x)?.assets.length ?? 0), 0),
|
||||
autoRetry: retry.length,
|
||||
trashRecordId,
|
||||
};
|
||||
};
|
||||
if (body.dryRun) return summary([], null);
|
||||
|
||||
// 执行:先把文件移入回收站,失败的文件所属记录保留
|
||||
let trashRecordId: string | null = null;
|
||||
const failedPaths = new Set<string>();
|
||||
const paths = [...new Set(plans.filter((p) => !p.blocked).flatMap((p) => [...p.sources, ...p.targets]))].filter(
|
||||
(abs) => checks.get(abs)?.blocked !== "not_found",
|
||||
);
|
||||
if (paths.length > 0) {
|
||||
const items = paths.map((abs) => ({ root: refs.get(abs)!.root, path: refs.get(abs)!.path }));
|
||||
const base = { mode: "trash" as const, retireAssets: true, unbind: false };
|
||||
const dry = await deleteFiles(d, principal, { idempotencyKey: randomUUID(), items, dryRun: true, ...base });
|
||||
const res = await deleteFiles(d, principal, {
|
||||
idempotencyKey: randomUUID(),
|
||||
items,
|
||||
dryRun: false,
|
||||
planId: dry.planId ?? undefined,
|
||||
...base,
|
||||
});
|
||||
trashRecordId = res.recordId;
|
||||
for (const f of res.failed) {
|
||||
const hit = paths.find((abs, i) => items[i]!.path === f.path || abs.endsWith(`/${f.path}`));
|
||||
if (hit) failedPaths.add(hit);
|
||||
}
|
||||
}
|
||||
for (const p of plans) {
|
||||
if (p.blocked) continue;
|
||||
const bad = [...p.sources, ...p.targets].find((abs) => failedPaths.has(abs));
|
||||
if (bad) {
|
||||
p.blocked = "failed";
|
||||
p.message = `${basename(bad)}:${BLOCK_TEXT.failed}`;
|
||||
}
|
||||
}
|
||||
const removable = plans.filter((p) => !p.blocked).map((p) => p.id);
|
||||
const removed = removable.length
|
||||
? (await db.deleteFrom("organization_record").where("id", "in", removable).returning("id").execute()).map(
|
||||
(r) => r.id,
|
||||
)
|
||||
: [];
|
||||
await audit(d, principal, "organize_records_delete", {
|
||||
removed,
|
||||
deleteSource: body.deleteSource,
|
||||
deleteTarget: body.deleteTarget,
|
||||
trashRecordId,
|
||||
kept: plans.filter((p) => p.blocked).map((p) => ({ id: p.id, blocked: p.blocked })),
|
||||
});
|
||||
return summary(removed, trashRecordId);
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import { mkdirIn, renameEntry } from "./mutate.ts";
|
||||
import { organizeFiles } from "./organize.ts";
|
||||
import { previewFile } from "./preview.ts";
|
||||
import { probeFiles, scrape, scrapeBatch } from "./probe.ts";
|
||||
import { deleteRecords } from "./record-delete.ts";
|
||||
import { findRecords } from "./records.ts";
|
||||
import { suggestName } from "./suggest-name.ts";
|
||||
import { transfer } from "./transfer.ts";
|
||||
@@ -123,4 +124,6 @@ export function registerFileRoutes(app: ModuleApp, d: FmDeps) {
|
||||
mount(app, F.tree, async (_c, { query }) => treeOf(d, query));
|
||||
mount(app, F.recordSources, async (_c, { query }) => recordSources(d, query.recordId));
|
||||
mount(app, F.suggestName, async (_c, { body }) => suggestName(d, body));
|
||||
// HIS-1:删除整理记录(可选源 / 媒体库文件进回收站);路径在 /organization-records 下,实现复用文件管理的删除
|
||||
mount(app, Api.organization.remove, async (c, { body }) => deleteRecords(d, c.get("principal"), body));
|
||||
}
|
||||
Reference in new issue
Block a user