From 912a6336d3e15c00dcc6a2889afbb8b1d48ef348 Mon Sep 17 00:00:00 2001 From: matevip Date: Sun, 3 May 2026 09:12:44 +0800 Subject: [PATCH] fix(wiki): also remove the upload file from disk when a raw material is deleted --- .../wiki/service/WikiRawMaterialService.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/mateclaw-server/src/main/java/vip/mate/wiki/service/WikiRawMaterialService.java b/mateclaw-server/src/main/java/vip/mate/wiki/service/WikiRawMaterialService.java index 81c50ebe..f71f158b 100644 --- a/mateclaw-server/src/main/java/vip/mate/wiki/service/WikiRawMaterialService.java +++ b/mateclaw-server/src/main/java/vip/mate/wiki/service/WikiRawMaterialService.java @@ -367,6 +367,15 @@ public class WikiRawMaterialService { } catch (Exception e) { log.warn("[Wiki] Failed to cascade-delete chunks for raw={}: {}", id, e.getMessage()); } + + // Source file last — DB pointer is gone, no other row references this + // path (each upload gets a timestamp-prefixed unique name), so + // leaving it on disk would just accumulate as the upload tree grows. + // Failure here is soft-logged and non-blocking — operator can run a + // sweep later if disk usage matters more than the delete RTT. + if (entity != null) { + cleanupFile(entity.getSourcePath()); + } } /** @@ -584,14 +593,18 @@ public class WikiRawMaterialService { } /** - * Delete a file from disk if it exists (cleanup for dedup-discarded uploads). + * Best-effort delete of an upload-tree file. Used both when a fresh + * upload turns out to be a duplicate (the new file is redundant) and + * when a raw material row is deleted (its source file becomes a + * disk orphan with no DB pointer to it). Idempotent — silently + * succeeds when the path is null or the file is already gone. */ private void cleanupFile(String path) { - if (path == null) return; + if (path == null || path.isBlank()) return; try { java.nio.file.Files.deleteIfExists(java.nio.file.Paths.get(path)); } catch (Exception e) { - log.warn("[Wiki] Failed to clean up duplicate upload file {}: {}", path, e.getMessage()); + log.warn("[Wiki] Failed to clean up upload file {}: {}", path, e.getMessage()); } }