Hygiene R140: Datei-Loesch-Helfer konsolidieren + DB-vor-Datei
Pentester-Hygiene zu a6b1dac:
1) Konsolidierung: neuer utils/fileCleanup.ts mit deleteFileAbsolute
(absoluter Pfad, z.B. Multer-Temp) + deleteUploadByRelativePath
(in DB gespeicherter /uploads/-Pfad). Ersetzt die 3x kopierten
deleteFileIfExists/cleanupFile in creditNote-, upload- und
customer-Service.
2) Reihenfolge: In deleteCreditNote/updateCreditNote erst die DB-
Operation, DANN die Datei loeschen. Schlaegt der DB-Schritt fehl,
bleibt die Datei erhalten (kein ins-Leere-zeigender Zustand).
Verifiziert: Update -> pdfPath null + alte Datei weg; Delete -> gibt
geloeschte Row zurueck (Audit) + Datei weg. Kein Regression.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
// ==================== DATEI-AUFRÄUMEN (Uploads) ====================
|
||||
// Gemeinsame Helfer zum Best-Effort-Löschen von Upload-Dateien. Ersetzt die
|
||||
// vorher mehrfach kopierten `deleteFileIfExists`/`cleanupFile`-Funktionen
|
||||
// (Pentest R140, Hygiene: eine Quelle statt Duplikate).
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
/**
|
||||
* Löscht eine Datei anhand ihres ABSOLUTEN Pfads. Wirft nie (loggt nur) –
|
||||
* z.B. für Multer-Temp-Dateien (`req.file.path`).
|
||||
*/
|
||||
export function deleteFileAbsolute(absolutePath: string | null | undefined): void {
|
||||
if (!absolutePath) return;
|
||||
try {
|
||||
if (fs.existsSync(absolutePath)) fs.unlinkSync(absolutePath);
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Löschen der Datei:', absolutePath, error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Löscht eine Datei anhand ihres in der DB gespeicherten RELATIVEN Upload-
|
||||
* Pfads (z.B. `/uploads/credit-notes/…`), aufgelöst gegen `process.cwd()`.
|
||||
*/
|
||||
export function deleteUploadByRelativePath(relativePath: string | null | undefined): void {
|
||||
if (!relativePath) return;
|
||||
deleteFileAbsolute(path.join(process.cwd(), relativePath));
|
||||
}
|
||||
Reference in New Issue
Block a user