파일을 삭제할 경우 서버와 클라이언트에 저장된 썸네일을 함께 삭제하도록 개선

This commit is contained in:
static
2025-07-06 17:38:04 +09:00
parent 781642fed6
commit 8975a0200d
5 changed files with 59 additions and 29 deletions

View File

@@ -45,10 +45,17 @@ export const getFileInformation = async (userId: number, fileId: number) => {
};
};
const safeUnlink = async (path: string | null) => {
if (path) {
await unlink(path).catch(console.error);
}
};
export const deleteFile = async (userId: number, fileId: number) => {
try {
const { path } = await unregisterFile(userId, fileId);
unlink(path); // Intended
const { path, thumbnailPath } = await unregisterFile(userId, fileId);
safeUnlink(path); // Intended
safeUnlink(thumbnailPath); // Intended
} catch (e) {
if (e instanceof IntegrityError && e.message === "File not found") {
error(404, "Invalid file id");
@@ -126,9 +133,7 @@ export const uploadFileThumbnail = async (
await pipeline(encContentStream, createWriteStream(path, { flags: "wx", mode: 0o600 }));
const oldPath = await updateFileThumbnail(userId, fileId, dekVersion, path, encContentIv);
if (oldPath) {
safeUnlink(oldPath); // Intended
}
safeUnlink(oldPath); // Intended
} catch (e) {
await safeUnlink(path);
@@ -157,10 +162,6 @@ export const scanMissingFileThumbnails = async (userId: number) => {
return { files: fileIds };
};
const safeUnlink = async (path: string) => {
await unlink(path).catch(console.error);
};
export const uploadFile = async (
params: Omit<NewFile, "path" | "encContentHash">,
encContentStream: Readable,