fix(printService): restore file on failed plate database update

If the plate database update fails after moving a file, the file's
location no longer matches the stored path. Wrap the update in a
try-catch to restore the file to its original location on failure,
so the stored path still finds it.
This commit is contained in:
Tautvydas Derzinskas committed 2026-10-19 20:59:18 +02:00
1 parent f34a488fb2
commit 03fdd836a5
2 files changed
+13 -3

No files matched your search

+1 -1
View File
@@ -501,7 +501,7 @@ async function addProfilePlateWithDetails(
const naming = profilePlateNaming(existing.plates, file.filename, instanceId, details.titles);
// The earlier profiles first, so the new file is placed under its own name once and never moved while its preview
// renders. Only profiles' own plates are renamed, so the content match addProfilePlate makes on untagged ones holds.
// Best-effort: the profile comes in either way.
// Best-effort: a failed move leaves the file where its plate says it is, and the profile comes in either way.
await renameProfilePlates(existing.print.id, naming.renames).catch(() => undefined);
const result = await addProfilePlate(existing, { ...file, filename: naming.filename }, instanceId);
if (!result.profileAdded) return result;
+12 -2
View File
@@ -356,9 +356,19 @@ export async function relocatePrint(print: PrintLike, plates: Plate[], template?
if (oldExists) {
await fs.mkdir(path.dirname(newPath), { recursive: true });
await fs.rename(oldPath, newPath);
await pruneEmptyStorageDirs(path.dirname(oldPath));
}
await prisma.plate.update({ where: { id: plate.id }, data: { storagePath: newRelative } });
try {
await prisma.plate.update({ where: { id: plate.id }, data: { storagePath: newRelative } });
} catch (err) {
// Put the file back, so the plate's stored path still finds it.
if (oldExists) {
await fs.mkdir(path.dirname(oldPath), { recursive: true });
await fs.rename(newPath, oldPath);
await pruneEmptyStorageDirs(path.dirname(newPath));
}
throw err;
}
if (oldExists) await pruneEmptyStorageDirs(path.dirname(oldPath));
}
}