diff --git a/backend/server.js b/backend/server.js index 7c75fa4..fad85ad 100644 --- a/backend/server.js +++ b/backend/server.js @@ -1571,6 +1571,88 @@ app.post('/api/user/update-ftp', } ); +// 获取SFTP存储空间使用情况 +app.get('/api/user/sftp-usage', authMiddleware, async (req, res) => { + let sftp = null; + + try { + // 检查用户是否配置了SFTP + if (!req.user.has_ftp_config) { + return res.status(400).json({ + success: false, + message: '未配置SFTP服务器' + }); + } + + // 连接SFTP + sftp = await connectToSFTP(req.user); + + // 递归计算目录大小的函数 + async function calculateDirSize(dirPath) { + let totalSize = 0; + let fileCount = 0; + let dirCount = 0; + + try { + const list = await sftp.list(dirPath); + + for (const item of list) { + // 跳过 . 和 .. 目录 + if (item.name === '.' || item.name === '..') continue; + + const itemPath = dirPath === '/' ? `/${item.name}` : `${dirPath}/${item.name}`; + + if (item.type === 'd') { + // 是目录,递归计算 + dirCount++; + const subResult = await calculateDirSize(itemPath); + totalSize += subResult.totalSize; + fileCount += subResult.fileCount; + dirCount += subResult.dirCount; + } else { + // 是文件,累加大小 + fileCount++; + totalSize += item.size || 0; + } + } + } catch (err) { + // 跳过无法访问的目录 + console.warn(`[SFTP统计] 无法访问目录 ${dirPath}: ${err.message}`); + } + + return { totalSize, fileCount, dirCount }; + } + + // 从根目录开始计算 + const result = await calculateDirSize('/'); + + res.json({ + success: true, + usage: { + totalSize: result.totalSize, + totalSizeFormatted: formatFileSize(result.totalSize), + fileCount: result.fileCount, + dirCount: result.dirCount + } + }); + + } catch (error) { + console.error('[SFTP统计] 获取失败:', error); + res.status(500).json({ + success: false, + message: '获取SFTP空间使用情况失败: ' + error.message + }); + } finally { + if (sftp) { + try { + await sftp.end(); + } catch (e) { + // 忽略关闭错误 + } + } + } +}); + // 修改管理员账号信息(仅管理员可修改用户名) app.post('/api/admin/update-profile', authMiddleware, diff --git a/frontend/app.html b/frontend/app.html index 347254c..21d52c3 100644 --- a/frontend/app.html +++ b/frontend/app.html @@ -1281,6 +1281,29 @@