添加 素材库路径检查 处理

This commit is contained in:
Chen committed 2025-12-05 14:27:31 +08:00
1 parent 204180b754
commit fd8dae1823
11 files changed
+1108 -49

No files matched your search

+13 -1
View File
@@ -79,10 +79,12 @@ router.put('/:id', asyncHandler(async (req, res) => {
/**
* 删除素材库
* DELETE /api/library/:id
* Query: autoSelectNext - 是否自动选择下一个素材库,默认 true
*/
router.delete('/:id', asyncHandler(async (req, res) => {
const { id } = req.params;
const result = await libraryService.deleteLibrary(id);
const autoSelectNext = req.query.autoSelectNext !== 'false';
const result = await libraryService.deleteLibrary(id, autoSelectNext);
res.json({ success: true, data: result });
}));
@@ -106,4 +108,14 @@ router.get('/:id/stats', asyncHandler(async (req, res) => {
res.json({ success: true, data: result });
}));
/**
* 验证素材库路径是否存在
* GET /api/library/:id/validate
*/
router.get('/:id/validate', asyncHandler(async (req, res) => {
const { id } = req.params;
const result = libraryService.validateLibraryPath(id);
res.json({ success: true, data: result });
}));
module.exports = router;
+42 -3
View File
@@ -6,7 +6,7 @@
const fs = require('fs');
const path = require('path');
const { NotFoundError, ValidationError } = require('../middleware/errorHandler');
const { getFlypicPath, getDatabasePath } = require('../config');
const { getFlypicPath, getDatabasePath, getThumbnailsPath } = require('../config');
class LibraryService {
constructor(configManager, dbPool, scanManager, lightweightWatcher, io) {
@@ -126,8 +126,10 @@ class LibraryService {
/**
* 删除素材库
* @param {string} id - 素材库ID
* @param {boolean} autoSelectNext - 是否自动选择下一个素材库,默认 true
*/
async deleteLibrary(id) {
async deleteLibrary(id, autoSelectNext = true) {
const config = this.configManager.load();
const library = config.libraries.find(lib => lib.id === id);
@@ -139,7 +141,7 @@ class LibraryService {
await this._cleanupLibraryResources(id, library.path);
// 删除配置
this.configManager.removeLibrary(id);
this.configManager.removeLibrary(id, autoSelectNext);
return {
success: true,
@@ -239,6 +241,43 @@ class LibraryService {
await new Promise(resolve => setTimeout(resolve, 300));
}
/**
* 验证素材库路径是否存在
* 返回三种状态:
* - status: 'ok' - 素材库正常(文件夹和索引都存在)
* - status: 'missing_index' - 文件夹存在但索引不存在(需要重新扫描)
* - status: 'missing_folder' - 文件夹不存在(需要打开其他或新建)
*/
validateLibraryPath(libraryId) {
const config = this.configManager.load();
const library = config.libraries.find(lib => lib.id === libraryId);
if (!library) {
throw new NotFoundError('Library', libraryId);
}
const folderExists = fs.existsSync(library.path);
const flypicPath = getFlypicPath(library.path);
const dbPath = getDatabasePath(library.path);
const indexExists = fs.existsSync(flypicPath) && fs.existsSync(dbPath);
let status = 'ok';
if (!folderExists) {
status = 'missing_folder';
} else if (!indexExists) {
status = 'missing_index';
}
return {
libraryId,
path: library.path,
name: library.name,
folderExists,
indexExists,
status
};
}
/**
* 获取素材库统计信息
*/