优化 简化统一代码

This commit is contained in:
Chen committed 2025-12-03 02:00:07 +08:00
1 parent 1d206fe179
commit 59abbf30d9
11 files changed
+332 -401

No files matched your search

+4 -18
View File
@@ -74,14 +74,8 @@ class DatabasePool {
// 关闭数据库连接
if (conn.db && conn.db.db) {
// 执行 checkpoint 确保 WAL 写入主数据库
try {
conn.db.db.pragma('wal_checkpoint(TRUNCATE)');
} catch (e) {
console.warn(`[DBPool] WAL checkpoint warning:`, e.message);
}
// 关闭连接
// 注意:db.js 使用 DELETE 模式而非 WAL,无需 checkpoint
// 直接关闭连接即可
conn.db.close();
}
@@ -106,15 +100,8 @@ class DatabasePool {
// 强制设置引用计数为0
conn.refCount = 0;
// 执行 WAL checkpoint
if (conn.db && conn.db.db) {
try {
conn.db.db.pragma('wal_checkpoint(TRUNCATE)');
} catch (e) {
console.warn(`[DBPool] WAL checkpoint warning for ${path}:`, e.message);
}
}
// 注意:db.js 使用 DELETE 模式而非 WAL,无需 checkpoint
// 直接关闭连接即可
conn.db.close();
console.log(`[DBPool] Closed connection for: ${path}`);
} catch (error) {
@@ -195,7 +182,6 @@ class DatabasePool {
*/
clear() {
// 清理空闲连接
const now = Date.now();
const toClose = [];
for (const [path, conn] of this.connections.entries()) {
+4 -3
View File
@@ -6,6 +6,7 @@ const { exec } = require('child_process');
const dbPool = require('../database/dbPool');
const { generateThumbnail } = require('../utils/thumbnail');
const { getLibrary } = require('../utils/config');
const { mapImageForFrontend, mapFolderForFrontend } = require('../utils/fieldMapper');
// 限制并发缩略图请求数量(防止内存爆炸)
let activeThumbnailRequests = 0;
@@ -51,14 +52,14 @@ router.get('/', (req, res) => {
// 如果使用分页,返回分页格式;否则保持向后兼容
if (pagination) {
res.json({
images: result.images,
images: result.images.map(mapImageForFrontend),
total: result.total,
offset: result.offset,
limit: result.limit,
hasMore: result.hasMore
});
} else {
res.json({ images: result });
res.json({ images: result.map(mapImageForFrontend) });
}
} finally {
// 释放连接
@@ -170,7 +171,7 @@ router.get('/folders', (req, res) => {
try {
const folders = db.getFolderTree();
res.json({ folders });
res.json({ folders: folders.map(mapFolderForFrontend) });
} finally {
dbPool.release(library.path);
}
+17 -5
View File
@@ -107,14 +107,14 @@ router.delete('/:id', async (req, res) => {
}
const dbPool = require('../database/dbPool');
const fileWatcher = require('../utils/fileWatcher');
const scanManager = require('../utils/scanManager');
const lightweightWatcher = require('../utils/lightweightWatcher');
// 1. 停止扫描并清理状态
scanManager.clearState(id);
// 2. 停止文件监控
fileWatcher.unwatch(id);
lightweightWatcher.unwatch(id);
// 3. 关闭数据库连接
dbPool.close(library.path);
@@ -142,6 +142,7 @@ router.post('/:id/set-current', async (req, res) => {
const config = loadConfig();
const dbPool = require('../database/dbPool');
const scanManager = require('../utils/scanManager');
const { needsMigration, migrateThumbnails } = require('../utils/migrateThumbnails');
// 获取旧的当前素材库
const oldLibraryId = config.currentLibraryId;
@@ -168,6 +169,17 @@ router.post('/:id/set-current', async (req, res) => {
// 预热新数据库连接(避免第一个查询时才创建连接)
if (newLibrary) {
// 检查是否需要迁移缩略图
if (needsMigration(newLibrary.path)) {
console.log(`[Library] Migrating thumbnails for: ${newLibrary.name}`);
try {
await migrateThumbnails(newLibrary.path);
} catch (error) {
console.error(`[Library] Thumbnail migration failed:`, error);
// 不阻塞切换流程,继续执行
}
}
const db = dbPool.acquire(newLibrary.path);
// 执行一个简单查询来预热缓存
try {
@@ -220,9 +232,9 @@ router.post('/:id/release', (req, res) => {
dbPool.close(library.path);
// 2. 停止文件监控
const fileWatcher = req.app.get('fileWatcher');
if (fileWatcher && fileWatcher.isWatching(id)) {
fileWatcher.unwatch(id);
const lightweightWatcher = require('../utils/lightweightWatcher');
if (lightweightWatcher.isWatching(id)) {
lightweightWatcher.unwatch(id);
}
res.json({
-4
View File
@@ -3,7 +3,6 @@ const cors = require('cors');
const path = require('path');
const http = require('http');
const { Server } = require('socket.io');
const fileWatcher = require('./utils/fileWatcher');
const MemoryMonitor = require('./utils/memoryMonitor');
const CleanupManager = require('./utils/cleanupManager');
@@ -79,9 +78,6 @@ io.on('connection', (socket) => {
// Make io accessible to routes
app.set('io', io);
// Make fileWatcher accessible to routes
app.set('fileWatcher', fileWatcher);
// Initialize memory optimization components
const memoryConfig = require('./config/memory');
const dbPool = require('./database/dbPool');
+140
View File
@@ -0,0 +1,140 @@
/**
* 数据库字段与前端字段映射工具
* 统一处理 snake_case (数据库) 和 camelCase (前端) 的转换
*/
/**
* 将 snake_case 转换为 camelCase
*/
function snakeToCamel(str) {
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
}
/**
* 将 camelCase 转换为 snake_case
*/
function camelToSnake(str) {
return str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
}
/**
* 将数据库对象转换为前端对象(snake_case -> camelCase)
* 支持嵌套对象和数组
*/
function dbToFrontend(data) {
// 处理 null/undefined
if (data == null) {
return data;
}
// 处理数组
if (Array.isArray(data)) {
return data.map(item => dbToFrontend(item));
}
// 处理非对象类型
if (typeof data !== 'object') {
return data;
}
// 处理对象
const result = {};
for (const [key, value] of Object.entries(data)) {
const camelKey = snakeToCamel(key);
// 递归处理嵌套对象和数组
if (value && typeof value === 'object') {
result[camelKey] = dbToFrontend(value);
} else {
result[camelKey] = value;
}
}
return result;
}
/**
* 将前端对象转换为数据库对象(camelCase -> snake_case)
* 支持嵌套对象和数组
*/
function frontendToDb(data) {
// 处理 null/undefined
if (data == null) {
return data;
}
// 处理数组
if (Array.isArray(data)) {
return data.map(item => frontendToDb(item));
}
// 处理非对象类型
if (typeof data !== 'object') {
return data;
}
// 处理对象
const result = {};
for (const [key, value] of Object.entries(data)) {
const snakeKey = camelToSnake(key);
// 递归处理嵌套对象和数组
if (value && typeof value === 'object') {
result[snakeKey] = frontendToDb(value);
} else {
result[snakeKey] = value;
}
}
return result;
}
/**
* 转换图片对象(数据库 -> 前端)
* 只保留前端需要的字段,减少数据传输
*/
function mapImageForFrontend(dbImage) {
if (!dbImage) return null;
return {
id: dbImage.id,
path: dbImage.path,
filename: dbImage.filename,
folder: dbImage.folder,
size: dbImage.size,
width: dbImage.width,
height: dbImage.height,
format: dbImage.format,
fileType: dbImage.file_type,
thumbnailPath: dbImage.thumbnail_path,
createdAt: dbImage.created_at,
modifiedAt: dbImage.modified_at
};
}
/**
* 转换文件夹对象(数据库 -> 前端)
*/
function mapFolderForFrontend(dbFolder) {
if (!dbFolder) return null;
return {
id: dbFolder.id,
path: dbFolder.path,
parentPath: dbFolder.parent_path,
name: dbFolder.name,
imageCount: dbFolder.image_count,
lastScan: dbFolder.last_scan,
// 递归转换子文件夹
children: dbFolder.children ? dbFolder.children.map(mapFolderForFrontend) : []
};
}
module.exports = {
snakeToCamel,
camelToSnake,
dbToFrontend,
frontendToDb,
mapImageForFrontend,
mapFolderForFrontend
};
-247
View File
@@ -1,247 +0,0 @@
const { Worker } = require('worker_threads');
const path = require('path');
const { getLibrary } = require('./config');
const { applyChangesFromEvents } = require('./scanner');
const dbPool = require('../database/dbPool');
class FileWatcher {
constructor() {
this.workers = new Map(); // libraryId -> Worker
this.debounceTimers = new Map(); // libraryId -> { timer, forceRebuildFolders }
this.changeBuffers = new Map(); // libraryId -> { filesAdded, filesChanged, filesRemoved, dirsAdded, dirsRemoved }
this.ioRef = null; // Socket.IO 引用
}
// 获取或创建变更缓冲区
_getBuffer(libraryId) {
if (!this.changeBuffers.has(libraryId)) {
this.changeBuffers.set(libraryId, {
filesAdded: new Set(),
filesChanged: new Set(),
filesRemoved: new Set(),
dirsAdded: new Set(),
dirsRemoved: new Set()
});
}
return this.changeBuffers.get(libraryId);
}
// 缓冲区事件数量
_bufferCount(buf) {
return (
buf.filesAdded.size +
buf.filesChanged.size +
buf.filesRemoved.size +
buf.dirsAdded.size +
buf.dirsRemoved.size
);
}
// 启动监控(使用 Worker Thread,不阻塞主线程)
watch(libraryId, io) {
// 保存 io 引用
this.ioRef = io;
// 如果已经在监控,先停止
if (this.workers.has(libraryId)) {
this.unwatch(libraryId);
}
const library = getLibrary(libraryId);
if (!library) {
console.error(`Library ${libraryId} not found`);
return;
}
// 检查路径是否存在和可访问
const fs = require('fs');
try {
fs.accessSync(library.path, fs.constants.R_OK);
} catch (error) {
const errorMsg = `Cannot access library path: ${library.path} - ${error.message}`;
console.error(errorMsg);
throw new Error(errorMsg);
}
console.log(`[FileWatcher] Starting worker for: ${library.name} (${library.path})`);
// 创建 Worker Thread
const workerPath = path.join(__dirname, 'fileWatcherWorker.js');
const worker = new Worker(workerPath, {
workerData: {
libraryPath: library.path,
libraryName: library.name
}
});
// 处理 Worker 消息
worker.on('message', (msg) => {
this._handleWorkerMessage(libraryId, library, msg);
});
worker.on('error', (error) => {
console.error(`[FileWatcher] Worker error for ${library.name}:`, error);
});
worker.on('exit', (code) => {
if (code !== 0) {
console.error(`[FileWatcher] Worker exited with code ${code} for ${library.name}`);
}
this.workers.delete(libraryId);
});
this.workers.set(libraryId, worker);
}
// 处理 Worker 发来的消息
_handleWorkerMessage(libraryId, library, msg) {
switch (msg.type) {
case 'add':
console.log(`[FileWatcher] File added: ${msg.path}`);
this._getBuffer(libraryId).filesAdded.add(msg.path);
this._debouncedSync(libraryId, library);
break;
case 'unlink':
console.log(`[FileWatcher] File removed: ${msg.path}`);
this._getBuffer(libraryId).filesRemoved.add(msg.path);
this._debouncedSync(libraryId, library);
break;
case 'change':
console.log(`[FileWatcher] File changed: ${msg.path}`);
this._getBuffer(libraryId).filesChanged.add(msg.path);
this._debouncedSync(libraryId, library);
break;
case 'addDir':
console.log(`[FileWatcher] Directory added: ${msg.path}`);
this._getBuffer(libraryId).dirsAdded.add(msg.path);
this._debouncedSync(libraryId, library, true);
break;
case 'unlinkDir':
console.log(`[FileWatcher] Directory removed: ${msg.path}`);
this._getBuffer(libraryId).dirsRemoved.add(msg.path);
this._debouncedSync(libraryId, library, true);
break;
case 'ready':
console.log(`[FileWatcher] Ready for: ${library.name}`);
break;
case 'error':
console.error(`[FileWatcher] Error for ${library.name}:`, msg.message);
break;
case 'closed':
console.log(`[FileWatcher] Closed for: ${library.name}`);
break;
}
}
// 防抖同步
_debouncedSync(libraryId, library, forceRebuildFolders = false) {
const buf = this._getBuffer(libraryId);
const changeCount = this._bufferCount(buf);
// 清除之前的定时器
if (this.debounceTimers.has(libraryId)) {
const oldTimer = this.debounceTimers.get(libraryId);
clearTimeout(oldTimer.timer);
forceRebuildFolders = forceRebuildFolders || oldTimer.forceRebuildFolders;
}
// 自适应防抖时间
let debounceMs = 2000;
if (changeCount <= 1) debounceMs = 800;
else if (changeCount <= 5) debounceMs = 1000;
else if (changeCount <= 20) debounceMs = 1500;
else if (changeCount <= 100) debounceMs = 2000;
else debounceMs = 3000;
const timer = setTimeout(async () => {
const snapshot = this._getBuffer(libraryId);
// 清空缓冲区
this.changeBuffers.set(libraryId, {
filesAdded: new Set(),
filesChanged: new Set(),
filesRemoved: new Set(),
dirsAdded: new Set(),
dirsRemoved: new Set()
});
const db = dbPool.acquire(library.path);
try {
const results = await applyChangesFromEvents(library.path, db, {
filesAdded: Array.from(snapshot.filesAdded),
filesChanged: Array.from(snapshot.filesChanged),
filesRemoved: Array.from(snapshot.filesRemoved),
dirsAdded: Array.from(snapshot.dirsAdded),
dirsRemoved: Array.from(snapshot.dirsRemoved)
});
if (this.ioRef) {
this.ioRef.emit('scanComplete', { libraryId, results });
}
console.log(`[FileWatcher] Sync complete for ${library.name}:`, results);
} catch (error) {
console.error(`[FileWatcher] Sync error for ${library.name}:`, error);
if (this.ioRef) {
this.ioRef.emit('scanError', { libraryId, error: error.message });
}
} finally {
dbPool.release(library.path);
}
this.debounceTimers.delete(libraryId);
}, debounceMs);
this.debounceTimers.set(libraryId, { timer, forceRebuildFolders });
}
// 停止监控
unwatch(libraryId) {
const worker = this.workers.get(libraryId);
if (worker) {
// 发送关闭命令给 Worker
worker.postMessage({ type: 'close' });
// 给 Worker 一些时间优雅关闭,然后强制终止
setTimeout(() => {
if (this.workers.has(libraryId)) {
worker.terminate();
this.workers.delete(libraryId);
}
}, 1000);
console.log(`[FileWatcher] Stopping worker for: ${libraryId}`);
}
// 清除防抖定时器
if (this.debounceTimers.has(libraryId)) {
const timerObj = this.debounceTimers.get(libraryId);
clearTimeout(timerObj.timer);
this.debounceTimers.delete(libraryId);
}
// 清空缓冲区
if (this.changeBuffers.has(libraryId)) {
this.changeBuffers.delete(libraryId);
}
}
// 停止所有监控
unwatchAll() {
for (const libraryId of this.workers.keys()) {
this.unwatch(libraryId);
}
}
// 获取监控状态
isWatching(libraryId) {
return this.workers.has(libraryId);
}
// 获取所有正在监控的库
getWatchedLibraries() {
return Array.from(this.workers.keys());
}
}
// 单例模式
const fileWatcher = new FileWatcher();
module.exports = fileWatcher;
-71
View File
@@ -1,71 +0,0 @@
/**
* File Watcher Worker - 在独立线程中运行 chokidar
* 避免文件监控初始化阻塞主线程的事件循环
*/
const { parentPort, workerData } = require('worker_threads');
const chokidar = require('chokidar');
const { libraryPath, libraryName } = workerData;
console.log(`[Worker] Starting chokidar for: ${libraryName}`);
// 创建监控器(极致内存优化配置)
const watcher = chokidar.watch(libraryPath, {
ignored: [
'**/node_modules/**',
'**/.git/**',
'**/.flypic/**',
'**/.*'
],
persistent: true,
// 跳过初始扫描,避免启动时内存激增(Requirements 11.4)
ignoreInitial: true,
// 禁用轮询,使用原生文件系统事件(Requirements 11.2)
usePolling: false,
// 禁用写入完成检测,减少内存开销(Requirements 11.3)
awaitWriteFinish: false,
// 禁用文件统计缓存,避免缓存大量文件元数据(Requirements 11.1)
disableStatCache: true,
// 不自动获取文件统计信息
alwaysStat: false,
// 限制深度
depth: 99,
// 确保监听删除事件
ignorePermissionErrors: true
});
// 监听事件并发送到主线程
watcher
.on('add', (filePath) => {
parentPort.postMessage({ type: 'add', path: filePath });
})
.on('unlink', (filePath) => {
parentPort.postMessage({ type: 'unlink', path: filePath });
})
.on('change', (filePath) => {
parentPort.postMessage({ type: 'change', path: filePath });
})
.on('addDir', (dirPath) => {
parentPort.postMessage({ type: 'addDir', path: dirPath });
})
.on('unlinkDir', (dirPath) => {
parentPort.postMessage({ type: 'unlinkDir', path: dirPath });
})
.on('error', (error) => {
parentPort.postMessage({ type: 'error', message: error.message });
})
.on('ready', () => {
console.log(`[Worker] File watcher ready for: ${libraryName}`);
parentPort.postMessage({ type: 'ready' });
});
// 监听主线程的关闭命令
parentPort.on('message', (msg) => {
if (msg.type === 'close') {
console.log(`[Worker] Closing watcher for: ${libraryName}`);
watcher.close().then(() => {
parentPort.postMessage({ type: 'closed' });
process.exit(0);
});
}
});
+148
View File
@@ -0,0 +1,148 @@
/**
* 缩略图迁移工具
* 将旧结构的缩略图迁移到新的分片结构
*
* 旧结构:.flypic/thumbnails/hash.webp
* 新结构:.flypic/thumbnails/ab/hash.webp (分片存储)
*/
const fs = require('fs');
const path = require('path');
/**
* 迁移单个素材库的缩略图
* @param {string} libraryPath - 素材库路径
* @returns {Object} 迁移结果统计
*/
async function migrateThumbnails(libraryPath) {
const thumbnailsDir = path.join(libraryPath, '.flypic', 'thumbnails');
// 检查目录是否存在
if (!fs.existsSync(thumbnailsDir)) {
return { migrated: 0, skipped: 0, errors: 0, message: 'Thumbnails directory not found' };
}
const stats = {
migrated: 0,
skipped: 0,
errors: 0
};
try {
const entries = fs.readdirSync(thumbnailsDir, { withFileTypes: true });
for (const entry of entries) {
// 跳过已经是目录的(已经是新结构)
if (entry.isDirectory()) {
continue;
}
// 只处理 .webp 文件
if (!entry.name.endsWith('.webp')) {
continue;
}
try {
const oldPath = path.join(thumbnailsDir, entry.name);
// 提取 hash(去掉扩展名)
const hash = entry.name.replace('.webp', '');
// 计算分片目录(使用前2个字符)
const shard = hash.slice(0, 2);
const shardDir = path.join(thumbnailsDir, shard);
const newPath = path.join(shardDir, entry.name);
// 如果新路径已存在,跳过
if (fs.existsSync(newPath)) {
stats.skipped++;
continue;
}
// 创建分片目录
if (!fs.existsSync(shardDir)) {
fs.mkdirSync(shardDir, { recursive: true });
}
// 移动文件
fs.renameSync(oldPath, newPath);
stats.migrated++;
console.log(`[Migrate] Moved: ${entry.name} -> ${shard}/${entry.name}`);
} catch (error) {
console.error(`[Migrate] Error migrating ${entry.name}:`, error.message);
stats.errors++;
}
}
console.log(`[Migrate] Complete for ${libraryPath}:`, stats);
return stats;
} catch (error) {
console.error(`[Migrate] Error reading thumbnails directory:`, error);
return { ...stats, message: error.message };
}
}
/**
* 检查是否需要迁移
* @param {string} libraryPath - 素材库路径
* @returns {boolean} 是否需要迁移
*/
function needsMigration(libraryPath) {
const thumbnailsDir = path.join(libraryPath, '.flypic', 'thumbnails');
if (!fs.existsSync(thumbnailsDir)) {
return false;
}
try {
const entries = fs.readdirSync(thumbnailsDir, { withFileTypes: true });
// 检查是否有直接在 thumbnails 目录下的 .webp 文件
for (const entry of entries) {
if (entry.isFile() && entry.name.endsWith('.webp')) {
return true;
}
}
return false;
} catch (error) {
console.error(`[Migrate] Error checking migration need:`, error);
return false;
}
}
/**
* 批量迁移多个素材库
* @param {Array} libraries - 素材库列表
* @returns {Object} 总体迁移统计
*/
async function migrateAllLibraries(libraries) {
const totalStats = {
migrated: 0,
skipped: 0,
errors: 0,
librariesProcessed: 0
};
for (const library of libraries) {
if (needsMigration(library.path)) {
console.log(`[Migrate] Processing library: ${library.name}`);
const stats = await migrateThumbnails(library.path);
totalStats.migrated += stats.migrated;
totalStats.skipped += stats.skipped;
totalStats.errors += stats.errors;
totalStats.librariesProcessed++;
}
}
console.log(`[Migrate] Total stats:`, totalStats);
return totalStats;
}
module.exports = {
migrateThumbnails,
needsMigration,
migrateAllLibraries
};
-12
View File
@@ -645,18 +645,6 @@ async function generatePlaceholderThumbnail(outputPath, type, label) {
};
}
/**
* 获取文件类型对应的图标(Unicode)
*/
function getIconForType(type) {
const icons = {
video: '🎬',
document: '📄',
special: '🎨'
};
return icons[type] || '📁';
}
module.exports = {
isImageFile,
getFileType,
+15 -15
View File
@@ -257,9 +257,9 @@ function ImageWaterfall() {
width: img.width,
height: img.height,
filename: img.filename,
thumbnail_path: img.thumbnail_path,
thumbnailPath: img.thumbnailPath,
path: img.path,
file_type: img.file_type,
fileType: img.fileType,
format: img.format
}));
@@ -360,8 +360,8 @@ function ImageWaterfall() {
const rows = useWorker ? workerRows : syncRows;
const getThumbnailUrl = (image) => {
// 注意:store中将thumbnail_path转换为thumbnailPath(驼峰命名)
const thumbnailPath = image.thumbnailPath || image.thumbnail_path;
// 后端已统一转换为 camelCase
const thumbnailPath = image.thumbnailPath;
if (!currentLibraryId || !thumbnailPath) {
return '';
@@ -460,7 +460,7 @@ function ImageWaterfall() {
}}
onClick={(e) => handleImageClick(image, e, flatIndex)}
onDoubleClick={() => {
const fileType = image.file_type || 'image';
const fileType = image.fileType || 'image';
if (fileType === 'image') {
setPhotoIndex(flatIndex);
} else {
@@ -481,19 +481,19 @@ function ImageWaterfall() {
/>
{/* 文件类型标识 */}
{image.file_type && image.file_type !== 'image' && (
{image.fileType && image.fileType !== 'image' && (
<div className={`absolute top-2 right-2 rounded-md px-2 py-1 flex items-center gap-1 shadow-lg ${
image.file_type === 'video' ? 'bg-blue-500 bg-opacity-90' :
image.file_type === 'audio' ? 'bg-pink-500 bg-opacity-90' :
image.file_type === 'document' ? 'bg-green-500 bg-opacity-90' :
image.file_type === 'design' ? 'bg-purple-500 bg-opacity-90' :
image.fileType === 'video' ? 'bg-blue-500 bg-opacity-90' :
image.fileType === 'audio' ? 'bg-pink-500 bg-opacity-90' :
image.fileType === 'document' ? 'bg-green-500 bg-opacity-90' :
image.fileType === 'design' ? 'bg-purple-500 bg-opacity-90' :
'bg-gray-500 bg-opacity-90'
}`}>
{image.file_type === 'video' && <Play className="w-4 h-4 text-white fill-white" />}
{image.file_type === 'audio' && <Music className="w-4 h-4 text-white" />}
{image.file_type === 'document' && <FileText className="w-4 h-4 text-white" />}
{image.file_type === 'design' && <Palette className="w-4 h-4 text-white" />}
{image.file_type === 'other' && <File className="w-4 h-4 text-white" />}
{image.fileType === 'video' && <Play className="w-4 h-4 text-white fill-white" />}
{image.fileType === 'audio' && <Music className="w-4 h-4 text-white" />}
{image.fileType === 'document' && <FileText className="w-4 h-4 text-white" />}
{image.fileType === 'design' && <Palette className="w-4 h-4 text-white" />}
{image.fileType === 'other' && <File className="w-4 h-4 text-white" />}
<span className="text-white text-xs font-semibold uppercase">
{image.format || image.filename.split('.').pop()}
</span>
+4 -26
View File
@@ -34,21 +34,9 @@ const useStore = create((set, get) => ({
filteredImages: [],
totalImageCount: 0, // 整个素材库的总图片数
// 设置图片(只存储必要字段)
// 设置图片(后端已统一转换为 camelCase,直接使用)
setImages: (images) => {
// 只保留必要字段,减少内存占用
const lightweightImages = images.map(img => ({
id: img.id,
path: img.path,
filename: img.filename,
size: img.size,
format: img.format,
width: img.width,
height: img.height,
thumbnailPath: img.thumbnail_path || img.thumbnailPath,
folder: img.folder
}));
set({ images: lightweightImages, filteredImages: lightweightImages });
set({ images, filteredImages: images });
},
setFilteredImages: (filteredImages) => set({ filteredImages }),
@@ -75,19 +63,9 @@ const useStore = create((set, get) => ({
folders: [],
selectedFolder: null,
// 设置文件夹(只保留必要字段)
// 设置文件夹(后端已统一转换为 camelCase,直接使用)
setFolders: (folders) => {
// 递归转换文件夹,确保所有层级的字段名一致
const transformFolder = (folder) => ({
path: folder.path,
name: folder.name,
imageCount: folder.image_count || folder.imageCount,
parentPath: folder.parent_path || folder.parentPath,
children: (folder.children || []).map(transformFolder)
});
const lightweightFolders = folders.map(transformFolder);
set({ folders: lightweightFolders });
set({ folders });
},
// 切换文件夹时清理缓存