修复 左侧文件夹计数和添加素材库大小统计

This commit is contained in:
Chen committed 2025-12-03 01:15:25 +08:00
1 parent de15191f33
commit 1d206fe179
6 files changed
+71 -15

No files matched your search

+30
View File
@@ -122,6 +122,36 @@ router.get('/count', (req, res) => {
}
});
// Get library statistics (count and total size)
router.get('/stats', (req, res) => {
try {
const { libraryId } = req.query;
if (!libraryId) {
return res.status(400).json({ error: 'Library ID is required' });
}
const library = getLibrary(libraryId);
if (!library) {
return res.status(404).json({ error: 'Library not found' });
}
const db = dbPool.acquire(library.path);
try {
const stats = db.db.prepare('SELECT COUNT(*) as count, SUM(size) as totalSize FROM images').get();
res.json({
count: stats.count || 0,
totalSize: stats.totalSize || 0
});
} finally {
dbPool.release(library.path);
}
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Get folders
router.get('/folders', (req, res) => {
try {
+1 -1
View File
@@ -56,7 +56,7 @@ function App() {
useEffect(() => {
// Setup Socket.IO - 开发模式连接后端端口,生产模式使用同源
const socketUrl = import.meta.env.DEV
? 'http://localhost:3000' // 开发模式:后端端口
? 'http://localhost:15002' // 开发模式:后端端口
: window.location.origin; // 生产模式:同源
console.log('🔌 Connecting to Socket.IO:', socketUrl);
+32 -10
View File
@@ -1,6 +1,7 @@
import React from 'react';
import React, { useState, useEffect } from 'react';
import useStore from '../store/useStore';
import { Folder, Image as ImageIcon, Search } from 'lucide-react';
import { Folder, Image as ImageIcon, HardDrive } from 'lucide-react';
import { imageAPI } from '../services/api';
function Dashboard() {
const {
@@ -11,12 +12,35 @@ function Dashboard() {
} = useStore();
const currentLibrary = getCurrentLibrary();
const [librarySize, setLibrarySize] = useState(0);
// Calculate stats
const folderCount = folders.length;
// Group folders by parent to find top-level folders
const topLevelFolders = folders.filter(f => !f.parent_path || f.parent_path === '.' || f.parent_path === '');
const topLevelFolders = folders.filter(f => !f.parentPath || f.parentPath === '.' || f.parentPath === '');
// Fetch library size
useEffect(() => {
if (currentLibraryId) {
imageAPI.getStats(currentLibraryId)
.then(res => {
setLibrarySize(res.data.totalSize || 0);
})
.catch(err => {
console.error('Failed to fetch library stats:', err);
});
}
}, [currentLibraryId, totalImageCount]); // Re-fetch when library or image count changes
// Format bytes to human readable format
const formatBytes = (bytes) => {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
};
return (
<div className="flex-1 h-full overflow-y-auto bg-gray-50 dark:bg-gray-900 p-8">
@@ -63,15 +87,13 @@ function Dashboard() {
<div className="bg-white dark:bg-gray-800 rounded-xl p-6 shadow-sm border border-gray-100 dark:border-gray-700">
<div className="flex items-center gap-4">
<div className="p-3 bg-purple-100 dark:bg-purple-900/30 rounded-lg text-purple-600 dark:text-purple-400">
<Search className="w-6 h-6" />
<HardDrive className="w-6 h-6" />
</div>
<div>
<div className="text-sm font-medium text-gray-900 dark:text-white mb-1">
快速开始
</div>
<div className="text-xs text-gray-500 dark:text-gray-400">
使用左侧目录树浏览或顶部搜索栏查找图片
<div className="text-2xl font-bold text-gray-900 dark:text-white">
{formatBytes(librarySize)}
</div>
<div className="text-sm text-gray-500 dark:text-gray-400">素材库大小</div>
</div>
</div>
</div>
@@ -99,7 +121,7 @@ function Dashboard() {
</div>
</div>
<div className="text-xs text-gray-500 dark:text-gray-400">
{folder.image_count || 0} 张图片
{folder.imageCount || 0} 张图片
</div>
</div>
))}
+1 -1
View File
@@ -437,7 +437,7 @@ function Sidebar() {
</span>
{/* 图片数量 */}
<span className="text-xs text-gray-400 ml-2 flex-shrink-0">{folder.image_count}</span>
<span className="text-xs text-gray-400 ml-2 flex-shrink-0">{folder.imageCount}</span>
</div>
{hasChildren && isExpanded && renderFolderTree(folder.children, level + 1)}
</div>
+1
View File
@@ -23,6 +23,7 @@ export const imageAPI = {
return axios.get(`${API_BASE}/image?${query}`, options);
},
getCount: (libraryId) => axios.get(`${API_BASE}/image/count?libraryId=${libraryId}`),
getStats: (libraryId) => axios.get(`${API_BASE}/image/stats?libraryId=${libraryId}`),
getFolders: (libraryId) => axios.get(`${API_BASE}/image/folders?libraryId=${libraryId}`),
getCacheMeta: (libraryId) => axios.get(`${API_BASE}/image/cache-meta?libraryId=${libraryId}`),
getThumbnailUrl: (libraryId, size, filename) => `${API_BASE}/image/thumbnail/${libraryId}/${size}/${filename}`,
+6 -3
View File
@@ -77,13 +77,16 @@ const useStore = create((set, get) => ({
// 设置文件夹(只保留必要字段)
setFolders: (folders) => {
const lightweightFolders = folders.map(folder => ({
// 递归转换文件夹,确保所有层级的字段名一致
const transformFolder = (folder) => ({
path: folder.path,
name: folder.name,
imageCount: folder.image_count || folder.imageCount,
parentPath: folder.parent_path || folder.parentPath,
children: folder.children || []
}));
children: (folder.children || []).map(transformFolder)
});
const lightweightFolders = folders.map(transformFolder);
set({ folders: lightweightFolders });
},