v3: code quality pass, various bug fixes
This commit is contained in:
@@ -374,22 +374,32 @@ export async function replaceFileDump(
|
||||
}
|
||||
|
||||
const data = new Uint8Array(await file.arrayBuffer());
|
||||
await Deno.writeFile(`${DUMPS_DIR}/${dumpId}`, data);
|
||||
const filePath = `${DUMPS_DIR}/${dumpId}`;
|
||||
// Read old file contents so we can restore on DB failure
|
||||
const oldData = await Deno.readFile(filePath).catch(() => null);
|
||||
await Deno.writeFile(filePath, data);
|
||||
|
||||
const now = new Date();
|
||||
const newSlug = makeSlug(file.name, dumpId);
|
||||
db.prepare(
|
||||
`UPDATE dumps SET title = ?, slug = ?, file_name = ?, file_mime = ?, file_size = ?, comment = ?, updated_at = ? WHERE id = ?;`,
|
||||
).run(
|
||||
file.name,
|
||||
newSlug,
|
||||
file.name,
|
||||
file.type,
|
||||
file.size,
|
||||
comment ?? null,
|
||||
now.toISOString(),
|
||||
dumpId,
|
||||
);
|
||||
try {
|
||||
db.prepare(
|
||||
`UPDATE dumps SET title = ?, slug = ?, file_name = ?, file_mime = ?, file_size = ?, comment = ?, updated_at = ? WHERE id = ?;`,
|
||||
).run(
|
||||
file.name,
|
||||
newSlug,
|
||||
file.name,
|
||||
file.type,
|
||||
file.size,
|
||||
comment ?? null,
|
||||
now.toISOString(),
|
||||
dumpId,
|
||||
);
|
||||
} catch (err) {
|
||||
// Roll back the file to its previous contents on DB failure
|
||||
if (oldData) await Deno.writeFile(filePath, oldData).catch(() => {});
|
||||
else await Deno.remove(filePath).catch(() => {});
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (comment) notifyMentions(dump.userId, comment, "dump", dumpId, file.name);
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user