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:
@@ -35,7 +35,15 @@ const {
|
||||
restoredCounts,
|
||||
isSupportedBackupVersion,
|
||||
} = require('./backup-restore');
|
||||
const { assertRestoreSpace, isDiskFullError, tarEntryFilter, sweepRestoreTempDirs } = require('./lib/restore');
|
||||
const {
|
||||
assertRestoreSpace,
|
||||
assertRestoreUploadSpace,
|
||||
backupRequestTimeoutMs,
|
||||
isDiskFullError,
|
||||
parseBackupLimitMb,
|
||||
tarEntryFilter,
|
||||
sweepRestoreTempDirs,
|
||||
} = require('./lib/restore');
|
||||
|
||||
const https = require('https');
|
||||
const path = require('path');
|
||||
@@ -5881,9 +5889,14 @@ async function aiCorrectText(text, profileOverride) {
|
||||
}
|
||||
}
|
||||
|
||||
const BACKUP_UPLOAD_LIMIT_MB = Math.max(1, parseInt(process.env.BACKUP_UPLOAD_LIMIT_MB || '500', 10) || 500);
|
||||
const BACKUP_UPLOAD_LIMIT_MB = parseBackupLimitMb(process.env.BACKUP_UPLOAD_LIMIT_MB);
|
||||
const BACKUP_UPLOAD_LIMIT_BYTES = BACKUP_UPLOAD_LIMIT_MB > 0 ? BACKUP_UPLOAD_LIMIT_MB * 1024 * 1024 : 0;
|
||||
const BACKUP_ARCHIVE_EXT = /\.(?:tar\.gz|tgz|gz)$/i;
|
||||
|
||||
function backupTooLargeError() {
|
||||
return `Архив больше допустимых ${BACKUP_UPLOAD_LIMIT_MB} МБ. Увеличьте BACKUP_UPLOAD_LIMIT_MB (0 — без ограничения) или восстанавливайте большой архив на самом сервере, минуя прокси.`;
|
||||
}
|
||||
|
||||
const uploadBackup = multer({
|
||||
storage: multer.diskStorage({
|
||||
destination: (req, __, cb) => {
|
||||
@@ -5896,13 +5909,30 @@ const uploadBackup = multer({
|
||||
cb(null, `backup-${Date.now()}${ext}`);
|
||||
},
|
||||
}),
|
||||
limits: { fileSize: BACKUP_UPLOAD_LIMIT_MB * 1024 * 1024 },
|
||||
...(BACKUP_UPLOAD_LIMIT_BYTES ? { limits: { fileSize: BACKUP_UPLOAD_LIMIT_BYTES } } : {}),
|
||||
fileFilter: (_, file, cb) => {
|
||||
if (!BACKUP_ARCHIVE_EXT.test(file.originalname || '')) return cb(new Error('Not allowed extension'));
|
||||
cb(null, true);
|
||||
},
|
||||
});
|
||||
|
||||
function backupUploadGuard(req, res, next) {
|
||||
const declared = Number(req.headers['content-length']);
|
||||
if (!Number.isFinite(declared) || declared <= 0) return next();
|
||||
if (BACKUP_UPLOAD_LIMIT_BYTES && declared > BACKUP_UPLOAD_LIMIT_BYTES + 1024 * 1024) {
|
||||
return res.status(413).json({ error: backupTooLargeError() });
|
||||
}
|
||||
try {
|
||||
assertRestoreUploadSpace(os.tmpdir(), declared);
|
||||
} catch (e) {
|
||||
if (e && e.code === 'RESTORE_NO_SPACE') {
|
||||
return res.status(507).json({ error: e.message + '. Освободите место на сервере и повторите восстановление.' });
|
||||
}
|
||||
return next(e);
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
|
||||
const BACKUP_TTL_MS = 30 * 60 * 1000;
|
||||
const BACKUP_TICKETS_MAX = 3;
|
||||
@@ -6114,7 +6144,7 @@ function peekGunzip(filePath, n) {
|
||||
});
|
||||
}
|
||||
|
||||
app.post('/api/restore', requireAdmin, uploadBackup.single('backup'), async (req, res) => {
|
||||
app.post('/api/restore', requireAdmin, backupUploadGuard, uploadBackup.single('backup'), async (req, res) => {
|
||||
if (!req.file) return res.status(400).json({ error: 'backup file required' });
|
||||
let data;
|
||||
let legacyPhotos = [];
|
||||
@@ -6517,11 +6547,12 @@ app.post('/api/restore', requireAdmin, uploadBackup.single('backup'), async (req
|
||||
|
||||
app.use('/api/restore', (err, req, res, next) => {
|
||||
cleanupUpload(req);
|
||||
if (isDiskFullError(err)) {
|
||||
return res.status(507).json({ error: 'Не хватает свободного места на сервере для загрузки архива. Освободите место и повторите восстановление.' });
|
||||
}
|
||||
if (err instanceof multer.MulterError) {
|
||||
if (err.code === 'LIMIT_FILE_SIZE') {
|
||||
return res.status(413).json({
|
||||
error: `Архив больше допустимых ${BACKUP_UPLOAD_LIMIT_MB} МБ. Увеличьте BACKUP_UPLOAD_LIMIT_MB или восстанавливайте большой архив на самом сервере, минуя прокси.`,
|
||||
});
|
||||
return res.status(413).json({ error: backupTooLargeError() });
|
||||
}
|
||||
return res.status(400).json({ error: 'Не удалось загрузить архив: ' + err.message });
|
||||
}
|
||||
@@ -13169,11 +13200,12 @@ const certPath = path.join(__dirname, 'certs', 'cert.pem');
|
||||
const keyPath = path.join(__dirname, 'certs', 'key.pem');
|
||||
|
||||
function tuneServer(srv) {
|
||||
const explicit = parseInt(process.env.UPLOAD_REQUEST_TIMEOUT_MS || '0', 10);
|
||||
srv.requestTimeout = explicit
|
||||
? Math.max(300000, explicit)
|
||||
: Math.max(UPLOAD_REQUEST_TIMEOUT_MS, BACKUP_UPLOAD_LIMIT_MB * 7500);
|
||||
srv.headersTimeout = srv.requestTimeout + 60000;
|
||||
srv.requestTimeout = backupRequestTimeoutMs({
|
||||
limitMb: BACKUP_UPLOAD_LIMIT_MB,
|
||||
uploadTimeoutMs: UPLOAD_REQUEST_TIMEOUT_MS,
|
||||
explicitMs: process.env.UPLOAD_REQUEST_TIMEOUT_MS,
|
||||
});
|
||||
srv.headersTimeout = Math.min(srv.requestTimeout + 60000, 120000);
|
||||
return srv;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user