From 1d206fe17946f16a601b31406d60cdeb351982d4 Mon Sep 17 00:00:00 2001 From: Chen Date: Wed, 3 Dec 2025 01:15:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E5=B7=A6=E4=BE=A7?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9=E8=AE=A1=E6=95=B0=E5=92=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=B4=A0=E6=9D=90=E5=BA=93=E5=A4=A7=E5=B0=8F=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/routes/image.js | 30 +++++++++++++++++++ frontend/src/App.jsx | 2 +- frontend/src/components/Dashboard.jsx | 42 ++++++++++++++++++++------- frontend/src/components/Sidebar.jsx | 2 +- frontend/src/services/api.js | 1 + frontend/src/store/useStore.js | 9 ++++-- 6 files changed, 71 insertions(+), 15 deletions(-) diff --git a/backend/routes/image.js b/backend/routes/image.js index 5d30a60..aa6630b 100644 --- a/backend/routes/image.js +++ b/backend/routes/image.js @@ -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 { diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index d2e8cab..4db58cf 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -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); diff --git a/frontend/src/components/Dashboard.jsx b/frontend/src/components/Dashboard.jsx index 1fdefe0..7cdaa98 100644 --- a/frontend/src/components/Dashboard.jsx +++ b/frontend/src/components/Dashboard.jsx @@ -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 (
@@ -63,15 +87,13 @@ function Dashboard() {
- +
-
- 快速开始 -
-
- 使用左侧目录树浏览或顶部搜索栏查找图片 +
+ {formatBytes(librarySize)}
+
素材库大小
@@ -99,7 +121,7 @@ function Dashboard() {
- {folder.image_count || 0} 张图片 + {folder.imageCount || 0} 张图片
))} diff --git a/frontend/src/components/Sidebar.jsx b/frontend/src/components/Sidebar.jsx index 28fe9da..4422472 100644 --- a/frontend/src/components/Sidebar.jsx +++ b/frontend/src/components/Sidebar.jsx @@ -437,7 +437,7 @@ function Sidebar() { {/* 图片数量 */} - {folder.image_count} + {folder.imageCount} {hasChildren && isExpanded && renderFolderTree(folder.children, level + 1)} diff --git a/frontend/src/services/api.js b/frontend/src/services/api.js index a2aa7c2..0bcccac 100644 --- a/frontend/src/services/api.js +++ b/frontend/src/services/api.js @@ -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}`, diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.js index c2f52d6..d0ac4de 100644 --- a/frontend/src/store/useStore.js +++ b/frontend/src/store/useStore.js @@ -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 }); },