fix(data): 清理临时目录时跳过子目录,并回收 multer 残片

transfers/ 下面还套着 multer 的 uploads/ 子目录;原来只清理一层,目录本身会被当成过期文件去 unlink(失败但刷日志),
而 uploads/ 里因体积超限被丢弃的残片又永远不会被清。现在只删普通文件,并两处都清。
This commit is contained in:
Drsakura
2026-10-23 13:52:14 +08:00
parent 5acfbcea7d
commit ee7550c890
2 changed files with 4 additions and 0 deletions
+2
View File
@@ -122,6 +122,8 @@ function cleanupUploads(dir, { maxAgeMs = 24 * 60 * 60 * 1000 } = {}) {
const file = path.join(dir, name) const file = path.join(dir, name)
try { try {
const stat = fs.statSync(file) const stat = fs.statSync(file)
/* 只删普通文件:目录里还套着 multer 的 uploads/,别把目录本身当过期文件删 */
if (!stat.isFile()) continue
if (Date.now() - stat.mtimeMs > maxAgeMs) { fs.unlinkSync(file); removed++ } if (Date.now() - stat.mtimeMs > maxAgeMs) { fs.unlinkSync(file); removed++ }
} catch { /* 单个文件失败不影响其它清理 */ } } catch { /* 单个文件失败不影响其它清理 */ }
} }
+2
View File
@@ -121,6 +121,8 @@ router.post('/import/preview', guard, upload.single('file'), async (req, res) =>
fs.renameSync(req.file.path, target) fs.renameSync(req.file.path, target)
/* 顺手清掉 24 小时前的旧包,避免上传目录无限增长(本次刚移入的 mtime 是现在,不会被删) */ /* 顺手清掉 24 小时前的旧包,避免上传目录无限增长(本次刚移入的 mtime 是现在,不会被删) */
transfer.cleanupUploads(TRANSFER_DIR) transfer.cleanupUploads(TRANSFER_DIR)
/* multer 落盘后因体积/字段错误被丢弃的残片留在 uploads/,一并清 */
transfer.cleanupUploads(UPLOAD_DIR)
const categories = parseCategories(req.body?.categories) const categories = parseCategories(req.body?.categories)
const preview = await transfer.previewImport(db, target, { const preview = await transfer.previewImport(db, target, {