Files
MikrotikManager/backend/src/services/backup-local-file-state.ts
T

29 lines
1.1 KiB
TypeScript

import { lstat } from "node:fs/promises"
import type { BackupItemDto } from "@mmapp/contracts/backups"
import { backupDeletionPath } from "./backup-deletion.js"
/** Inspect local copies without probing S3; preserve unknown states and bound filesystem work. */
export async function withBackupLocalFileStates<T extends BackupItemDto>(
rows: T[], directory: string, inspect = lstat,
): Promise<T[]> {
const result = [...rows]
let cursor = 0
await Promise.all(Array.from({ length: Math.min(8, rows.length) }, async () => {
while (cursor < rows.length) {
const index = cursor++, row = rows[index]
let status: BackupItemDto["localFileStatus"] = "not-stored"
if (row.storage !== "s3") {
try {
const info = await inspect(backupDeletionPath(directory, row.filename))
status = info.isFile() ? "available" : "unknown"
} catch (error) {
const code = (error as NodeJS.ErrnoException).code
status = code === "ENOENT" || code === "ENOTDIR" ? "missing" : "unknown"
}
}
result[index] = { ...row, localFileStatus: status }
}
}))
return result
}