进一步优化 合理拆分文件和优化

This commit is contained in:
Chen committed 2025-12-07 16:08:00 +08:00
1 parent ed76ec5da3
commit fa5bb66d14
30 files changed
+3007 -2363

No files matched your search

+2 -1
View File
@@ -42,7 +42,8 @@ module.exports = {
CACHE_SIZE_KB: 4096,
CLEANUP_INTERVAL_MS: 60000, // 清理间隔:1分钟
DB_IDLE_TIMEOUT_MS: 60000, // 数据库空闲超时:60秒
DB_CLEANUP_CHECK_INTERVAL: 10000 // 数据库清理检查间隔:10秒
DB_CLEANUP_CHECK_INTERVAL: 10000, // 数据库清理检查间隔:10秒(已优化)
WAL_CHECKPOINT_INTERVAL_MS: 600000 // WAL checkpoint间隔:10分钟(降低I/O开销)
},
// 数据库配置
+3 -3
View File
@@ -66,10 +66,10 @@ router.patch('/rename', asyncHandler(async (req, res) => {
/**
* 移动文件或文件夹
* POST /api/file/move
* Body: { libraryId, items: [{type, path}], targetFolder }
* Body: { libraryId, items: [{type, path}], targetFolder, conflictAction?: 'skip'|'replace'|'rename' }
*/
router.post('/move', asyncHandler(async (req, res) => {
const { libraryId, items, targetFolder } = req.body;
const { libraryId, items, targetFolder, conflictAction } = req.body;
if (!libraryId || !items || !Array.isArray(items) || targetFolder === undefined) {
return res.status(400).json({
@@ -78,7 +78,7 @@ router.post('/move', asyncHandler(async (req, res) => {
});
}
const results = await fileService.moveItems(libraryId, items, targetFolder);
const results = await fileService.moveItems(libraryId, items, targetFolder, conflictAction);
res.json({
success: true,
+51 -7
View File
@@ -556,11 +556,12 @@ class FileService {
* @param {string} libraryId - 素材库ID
* @param {Array} items - 待移动项
* @param {string} targetFolder - 目标文件夹(相对路径)
* @param {string} conflictAction - 冲突处理方式: 'skip'|'replace'|'rename'
*/
async moveItems(libraryId, items, targetFolder) {
async moveItems(libraryId, items, targetFolder, conflictAction = 'rename') {
const db = this._getDatabase(libraryId);
const libraryPath = db.libraryPath;
const results = { success: [], failed: [] };
const results = { success: [], failed: [], conflicts: [] };
for (const item of items) {
try {
@@ -570,12 +571,12 @@ class FileService {
const normalizedTarget = targetFolder ? targetFolder.replace(/\\/g, '/') : '';
// 目标文件夹相对路径
const newRelativeFolder = normalizedTarget
let newRelativeFolder = normalizedTarget
? `${normalizedTarget}/${fileName}`
: fileName;
const oldFullPath = path.join(libraryPath, oldPath);
const newFullPath = path.join(libraryPath, newRelativeFolder);
let newFullPath = path.join(libraryPath, newRelativeFolder);
// 检查源路径是否存在
if (!fs.existsSync(oldFullPath)) {
@@ -591,10 +592,53 @@ class FileService {
fs.mkdirSync(targetFullPath, { recursive: true });
}
// 检查目标是否已存在同名文件/文件夹
// 处理冲突
const isDirectory = item.type === 'folder';
if (fs.existsSync(newFullPath)) {
results.failed.push({ path: oldPath, error: '目标位置已存在同名文件' });
continue;
// 目标位置已存在同名文件/文件夹
results.conflicts.push({ path: oldPath, name: fileName });
if (conflictAction === 'skip') {
// 跳过冲突文件
console.log(`⏭️ 跳过冲突: ${fileName}`);
continue;
} else if (conflictAction === 'replace') {
// 覆盖:先删除目标文件/文件夹
console.log(`🔄 覆盖: ${fileName}`);
if (fs.statSync(newFullPath).isDirectory()) {
// 删除目标物理目录
fs.rmSync(newFullPath, { recursive: true, force: true });
// 同时删除数据库中目标路径下的记录,避免后续路径更新时触发 UNIQUE(path) 冲突
const normalizedTargetFolder = newRelativeFolder.replace(/\\/g, '/');
db.deleteImagesByFolderPrefix(normalizedTargetFolder);
db.deleteFoldersByPrefix(normalizedTargetFolder);
} else {
// 删除目标物理文件
fs.unlinkSync(newFullPath);
// 删除数据库中该文件的记录,避免 _updatePathInDatabase 更新到已存在路径时报 UNIQUE 约束错误
const normalizedTargetPath = newRelativeFolder.replace(/\\/g, '/');
db.deleteImage(normalizedTargetPath);
}
// 继续执行移动
} else if (conflictAction === 'rename') {
// 重命名:自动编号
const ext = isDirectory ? '' : path.extname(fileName);
const basename = isDirectory ? fileName : path.basename(fileName, ext);
let counter = 1;
while (fs.existsSync(newFullPath)) {
const numberedName = isDirectory
? `${basename} (${counter})`
: `${basename} (${counter})${ext}`;
newRelativeFolder = normalizedTarget
? `${normalizedTarget}/${numberedName}`
: numberedName;
newFullPath = path.join(libraryPath, newRelativeFolder);
counter++;
}
console.log(`✏️ 重命名为: ${path.basename(newFullPath)}`);
}
}
if (item.type === 'folder') {