Files
MikrotikManager/backend/src/services/backup-deletion.ts
T

21 lines
1.1 KiB
TypeScript

import path from "node:path"
export function backupDeletionPath(directory: string, filename: string): string {
if (!filename || filename === "." || filename === ".." || /[\\/]/.test(filename) || path.isAbsolute(filename)) throw new Error("Некорректное имя файла резервной копии")
const target = path.resolve(directory, filename), relative = path.relative(path.resolve(directory), target)
if (!relative || relative.startsWith("..") || path.isAbsolute(relative)) throw new Error("Файл резервной копии вне хранилища")
return target
}
/** Keep the catalog entry until every configured storage cleanup has acknowledged success. */
export async function deleteStoredBackup(entry: { id: string; filename: string; s3Key?: string | null }, directory: string, deps: {
removeS3: (key: string) => Promise<void>
removeLocal: (file: string) => Promise<void>
removeRecord: (id: string) => Promise<void>
}): Promise<void> {
const file = backupDeletionPath(directory, entry.filename)
if (entry.s3Key) await deps.removeS3(entry.s3Key)
await deps.removeLocal(file)
await deps.removeRecord(entry.id)
}