mirror of
https://github.com/TautvydasDerzinskas/Thingport.git
synced 2026-10-21 22:22:24 -05:00
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:
1 parent
f34a488fb2
commit
03fdd836a5
2 files changed
+13
-3
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user