From f899e9a859eec74ac6092e83b9ff8451178a514a Mon Sep 17 00:00:00 2001 From: Backend Developer Date: Tue, 10 Mar 2026 09:11:30 +0000 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E7=AB=AF:=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E9=87=8D=E5=91=BD=E5=90=8DAPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/index.js | 2 ++ backend/src/routes/rename.js | 61 ++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 backend/src/routes/rename.js diff --git a/backend/src/index.js b/backend/src/index.js index 6047f91..20469f1 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -11,6 +11,7 @@ const batchRoutes = require('./routes/batch'); const trashRoutes = require('./routes/trash'); const statsRoutes = require('./routes/stats'); const recentRoutes = require('./routes/recent'); +const renameRoutes = require('./routes/rename'); const errorHandler = require('./middleware/errorHandler'); const logger = require('./middleware/logger'); @@ -32,6 +33,7 @@ app.use('/api/batch', batchRoutes); app.use('/api/trash', trashRoutes); app.use('/api/stats', statsRoutes); app.use('/api/recent', recentRoutes); +app.use('/api/rename', renameRoutes); app.use(errorHandler); diff --git a/backend/src/routes/rename.js b/backend/src/routes/rename.js new file mode 100644 index 0000000..97fb708 --- /dev/null +++ b/backend/src/routes/rename.js @@ -0,0 +1,61 @@ +const express = require('express'); +const db = require('../db'); + +const router = express.Router(); + +// Rename file/folder +router.put('/:id', (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'); + + const { name } = req.body; + + if (!name || name.trim() === '') { + return res.status(400).json({ error: 'Name cannot be empty' }); + } + + // Check if name already exists in same folder + const parent = db.query( + 'SELECT parent_id FROM files WHERE id = ? AND user_id = ?', + [req.params.id, decoded.userId] + ).then(parents => { + if (parents.length === 0) { + throw new Error('File not found'); + } + return parents[0].parent_id; + }).then(parentId => { + return db.query( + 'SELECT id FROM files WHERE name = ? AND parent_id IS ? AND user_id = ? AND id != ?', + [name, parentId, decoded.userId, req.params.id] + ); + }).then(existing => { + if (existing.length > 0) { + throw new Error('Name already exists'); + } + return db.run( + 'UPDATE files SET name = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ? AND user_id = ?', + [name, req.params.id, decoded.userId] + ); + }); + + parent.then(() => { + res.json({ success: true }); + }).catch(err => { + if (err.message === 'File not found') { + res.status(404).json({ error: 'File not found' }); + } else if (err.message === 'Name already exists') { + res.status(400).json({ error: 'Name already exists' }); + } else { + res.status(500).json({ error: err.message }); + } + }); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}); + +module.exports = router;