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;