Roll back a Rule copy when its files cannot be saved on their own.

This commit is contained in:
adilallo committed 2026-11-24 18:58:30 -07:00
1 parent 94cee1dc5b
commit aab7962450
6 files changed
+186 -22

No files matched your search

+36 -7
View File
@@ -1,4 +1,4 @@
import { copyFile } from "node:fs/promises";
import { copyFile, unlink } from "node:fs/promises";
import path from "node:path";
import { randomUUID } from "node:crypto";
import type { Prisma } from "@prisma/client";
@@ -19,12 +19,14 @@ type SourceUploadRow = {
purpose: string;
};
type CopiedUpload = { id: string; absolutePath: string };
async function copyOneUpload(opts: {
row: SourceUploadRow;
root: string;
destRuleId: string;
userId: string;
}): Promise<string | null> {
}): Promise<CopiedUpload | null> {
const resolved = await resolveUploadedFileById(opts.row.id);
if (!resolved) {
logger.warn("[uploads] skip copy; missing file", opts.row.id);
@@ -48,16 +50,19 @@ async function copyOneUpload(opts: {
ruleId: opts.destRuleId,
},
});
return newId;
return { id: newId, absolutePath: destPath };
} catch (err) {
logger.warn("[uploads] failed to copy upload", opts.row.id, err);
await unlink(destPath).catch(() => undefined);
return null;
}
}
/**
* Give `destRuleId` its own copies of files this document names that belong
* to `sourceRuleId`, rewrite paths, and persist the rewritten document.
* to `sourceRuleId` or are not linked to any Rule, rewrite paths, and persist
* the rewritten document. Throws when that save fails, after dropping the
* copies just made.
*/
export async function copyNamedUploadsForRule(opts: {
document: unknown;
@@ -69,7 +74,10 @@ export async function copyNamedUploadsForRule(opts: {
if (ids.length === 0) return opts.document;
const rows = await prisma.upload.findMany({
where: { id: { in: ids }, ruleId: opts.sourceRuleId },
where: {
id: { in: ids },
OR: [{ ruleId: opts.sourceRuleId }, { ruleId: null }],
},
select: {
id: true,
mimeType: true,
@@ -87,14 +95,17 @@ export async function copyNamedUploadsForRule(opts: {
await ensureUploadRootExists(root);
const replacements = new Map<string, string>();
const copies: CopiedUpload[] = [];
for (const row of rows) {
const copiedId = await copyOneUpload({
const copied = await copyOneUpload({
row,
root,
destRuleId: opts.destRuleId,
userId: opts.userId,
});
if (copiedId) replacements.set(row.id, copiedId);
if (!copied) continue;
replacements.set(row.id, copied.id);
copies.push(copied);
}
if (replacements.size === 0) return opts.document;
@@ -110,6 +121,24 @@ export async function copyNamedUploadsForRule(opts: {
opts.destRuleId,
err,
);
await dropCopies(copies);
throw err;
}
return document;
}
async function dropCopies(copies: CopiedUpload[]): Promise<void> {
if (copies.length === 0) return;
await prisma.upload
.deleteMany({ where: { id: { in: copies.map((copy) => copy.id) } } })
.catch((err) => {
logger.warn("[uploads] failed to drop copies after persist error", err);
});
await Promise.all(
copies.map((copy) =>
unlink(copy.absolutePath).catch((err) => {
logger.warn("[uploads] failed to remove copy after persist error", err);
}),
),
);
}