From 2372b94ba5e3ea505976cd736c035179fdc9e776 Mon Sep 17 00:00:00 2001 From: Backend Developer Date: Tue, 10 Mar 2026 09:06:39 +0000 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E7=AB=AF:=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=BB=9F=E8=AE=A1API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/index.js | 2 ++ backend/src/routes/stats.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 backend/src/routes/stats.js diff --git a/backend/src/index.js b/backend/src/index.js index 438c8f2..2b42646 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -9,6 +9,7 @@ const userRoutes = require('./routes/user'); const searchRoutes = require('./routes/search'); const batchRoutes = require('./routes/batch'); const trashRoutes = require('./routes/trash'); +const statsRoutes = require('./routes/stats'); const errorHandler = require('./middleware/errorHandler'); const app = express(); @@ -26,6 +27,7 @@ app.use('/api/user', userRoutes); app.use('/api/search', searchRoutes); app.use('/api/batch', batchRoutes); app.use('/api/trash', trashRoutes); +app.use('/api/stats', statsRoutes); app.use(errorHandler); diff --git a/backend/src/routes/stats.js b/backend/src/routes/stats.js new file mode 100644 index 0000000..e652d3b --- /dev/null +++ b/backend/src/routes/stats.js @@ -0,0 +1,33 @@ +const express = require('express'); +const db = require('../db'); + +const router = express.Router(); + +// Get user statistics +router.get('/', (req, res) => { + const token = req.headers.authorization?.replace('Bearer ', ''); + if (!token) return res.status(401).json({ error: 'No token' }); + + try { + const jwt = require('jsonwebtoken'); + const decoded = jwt.verify(token, process.env.JWT_SECRET || 'clouddisk-secret-key'); + + Promise.all([ + db.query('SELECT COUNT(*) as total FROM files WHERE user_id = ? AND deleted_at IS NULL', [decoded.userId]), + db.query('SELECT SUM(size) as used FROM files WHERE user_id = ? AND deleted_at IS NULL AND is_folder = 0', [decoded.userId]), + db.query('SELECT COUNT(*) as total FROM files WHERE user_id = ? AND deleted_at IS NOT NULL', [decoded.userId]), + db.query('SELECT COUNT(*) as total FROM shares WHERE file_id IN (SELECT id FROM files WHERE user_id = ?)', [decoded.userId]) + ]).then(([files, storage, trash, shares]) => { + res.json({ + totalFiles: files[0]?.total || 0, + storageUsed: storage[0]?.used || 0, + trashCount: trash[0]?.total || 0, + shareCount: shares[0]?.total || 0 + }); + }); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}); + +module.exports = router;