fix(restore): allow backups of any size
- default BACKUP_UPLOAD_LIMIT_MB to 0 (no hard cap, disk is the limit); a positive value still enables multer fileSize - scale request timeout by the configured limit and keep up to 24h when unlimited, cap headersTimeout at 120s - check Content-Length and free space before upload (413/507) and map multer ENOSPC to 507 - drop the hardcoded 500 MB fallback in the settings restore pre-check - cover new limit/timeout helpers in backup.selftest.js - update env template, compose, README, AGENTS and API docs
This commit is contained in:
@@ -3,6 +3,14 @@ const os = require('os');
|
||||
const path = require('path');
|
||||
|
||||
const FREE_SPACE_RESERVE_BYTES = 16 * 1024 * 1024;
|
||||
const UNLIMITED_BACKUP_TIMEOUT_MS = 24 * 60 * 60 * 1000;
|
||||
|
||||
function parseBackupLimitMb(raw) {
|
||||
if (raw === undefined || raw === null || String(raw).trim() === '') return 0;
|
||||
const n = parseInt(raw, 10);
|
||||
if (!Number.isFinite(n) || n < 0) return 0;
|
||||
return n;
|
||||
}
|
||||
|
||||
function formatBytes(bytes) {
|
||||
const n = Number(bytes) || 0;
|
||||
@@ -32,6 +40,26 @@ function assertRestoreSpace(dir, archiveBytes) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
function assertRestoreUploadSpace(dir, archiveBytes) {
|
||||
const free = freeBytes(dir);
|
||||
if (free == null) return;
|
||||
const need = Math.max(Number(archiveBytes) * 3, Number(archiveBytes) + FREE_SPACE_RESERVE_BYTES);
|
||||
if (free >= need) return;
|
||||
const err = new Error(
|
||||
`Недостаточно свободного места для загрузки и распаковки архива: нужно примерно ${formatBytes(need)}, свободно ${formatBytes(free)}`
|
||||
);
|
||||
err.code = 'RESTORE_NO_SPACE';
|
||||
throw err;
|
||||
}
|
||||
|
||||
function backupRequestTimeoutMs({ limitMb, uploadTimeoutMs, explicitMs }) {
|
||||
const explicit = Number(explicitMs) || 0;
|
||||
if (explicit > 0) return Math.max(300000, explicit);
|
||||
const base = Math.max(300000, Number(uploadTimeoutMs) || 0);
|
||||
if (Number(limitMb) > 0) return Math.max(base, Number(limitMb) * 7500);
|
||||
return Math.max(base, UNLIMITED_BACKUP_TIMEOUT_MS);
|
||||
}
|
||||
|
||||
function isDiskFullError(e) {
|
||||
const code = String((e && (e.code || e.errno)) || '');
|
||||
if (code === 'ENOSPC' || code === 'EDQUOT') return true;
|
||||
@@ -76,7 +104,10 @@ function sweepRestoreTempDirs() {
|
||||
module.exports = {
|
||||
formatBytes,
|
||||
freeBytes,
|
||||
parseBackupLimitMb,
|
||||
assertRestoreSpace,
|
||||
assertRestoreUploadSpace,
|
||||
backupRequestTimeoutMs,
|
||||
isDiskFullError,
|
||||
isEntryInside,
|
||||
tarEntryFilter,
|
||||
|
||||
Reference in New Issue
Block a user