diff --git a/backend/server.js b/backend/server.js index f90e308..4c5c701 100644 --- a/backend/server.js +++ b/backend/server.js @@ -1133,6 +1133,101 @@ app.post('/api/files/mkdir', authMiddleware, async (req, res) => { } }); +// 获取文件夹详情(大小统计) +app.post('/api/files/folder-info', authMiddleware, async (req, res) => { + const { path, folderName } = req.body; + let storage; + + if (!folderName) { + return res.status(400).json({ + success: false, + message: '缺少文件夹名称参数' + }); + } + + // 只支持本地存储 + if (req.user.current_storage_type !== 'local') { + return res.status(400).json({ + success: false, + message: '只有本地存储支持此功能' + }); + } + + try { + const { StorageInterface } = require('./storage'); + const storageInterface = new StorageInterface(req.user); + storage = await storageInterface.connect(); + + // 构造文件夹路径 + const basePath = path || '/'; + const folderPath = basePath === '/' ? `/${folderName}` : `${basePath}/${folderName}`; + const fullPath = storage.getFullPath(folderPath); + + // 检查是否存在且是文件夹 + if (!fs.existsSync(fullPath)) { + return res.status(404).json({ + success: false, + message: '文件夹不存在' + }); + } + + const stats = fs.statSync(fullPath); + if (!stats.isDirectory()) { + return res.status(400).json({ + success: false, + message: '指定路径不是文件夹' + }); + } + + // 计算文件夹大小 + const folderSize = storage.calculateFolderSize(fullPath); + + // 计算文件数量 + function countFiles(dirPath) { + let fileCount = 0; + let folderCount = 0; + + const items = fs.readdirSync(dirPath, { withFileTypes: true }); + + for (const item of items) { + const itemPath = path.join(dirPath, item.name); + + if (item.isDirectory()) { + folderCount++; + const subCounts = countFiles(itemPath); + fileCount += subCounts.fileCount; + folderCount += subCounts.folderCount; + } else { + fileCount++; + } + } + + return { fileCount, folderCount }; + } + + const counts = countFiles(fullPath); + + res.json({ + success: true, + data: { + name: folderName, + path: folderPath, + size: folderSize, + fileCount: counts.fileCount, + folderCount: counts.folderCount + } + }); + } catch (error) { + console.error('[获取文件夹详情失败]', error); + res.status(500).json({ + success: false, + message: '获取文件夹详情失败: ' + error.message + }); + } finally { + if (storage) await storage.end(); + } +}); + // 删除文件 app.post('/api/files/delete', authMiddleware, async (req, res) => { const { fileName, path } = req.body; diff --git a/frontend/app.html b/frontend/app.html index 7900fc4..adf7992 100644 --- a/frontend/app.html +++ b/frontend/app.html @@ -856,6 +856,50 @@ + +
+