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 (