const express = require('express'); const fs = require('fs'); const path = require('path'); const router = express.Router(); const fileUpload = require('express-fileupload'); const authMiddleware = require('../../../Middlewares/authMiddleware'); const { loggers } = require('winston'); const ncp = require('ncp').ncp; let configFile = fs.readFileSync(path.join(__dirname, '../../../data', 'setup.json'), 'utf-8') let config = JSON.parse(configFile)[0]; const bodyParser = require('body-parser'); const crypto = require('crypto'); const os = require('os'); const { getUserData, getSetupData } = require('../../../Middlewares/watcherMiddleware'); let setupData = getSetupData(); let userData = getUserData(); router.use(bodyParser.json()); /** * @swagger * /dashboard/getfilefolder/{folderName}?token={token}: * post: * security: * - bearerAuth: [] * tags: * - Folder * summary: Get files and folders in a specific folder * description: This route allows you to get the files and folders in a specific folder. It requires a valid JWT token in the Authorization header. * parameters: * - in: path * name: folderName * required: true * schema: * type: string * description: The name of the folder * - in: header * name: Authorization * required: true * schema: * type: string * description: The JWT token of your account to have access * responses: * 200: * description: Success * content: * application/json: * schema: * type: object * properties: * files: * type: array * items: * type: object * properties: * name: * type: string * type: * type: string * 401: * description: Unauthorized * content: * application/json: * schema: * type: object * properties: * message: * type: string * 404: * description: The specified folder does not exist * content: * application/json: * schema: * type: object * properties: * error: * type: string * 500: * description: Internal server error * content: * application/json: * schema: * type: object * properties: * error: * type: string */ function authenticateToken(req, res, next) { let token = null; const authHeader = req.headers['authorization']; if (authHeader) { token = authHeader.split(' ')[1]; } else if (req.query.token) { token = req.query.token; } if (token == null) { if (req.user) { return next(); } else { return res.status(401).json({ message: 'Unauthorized' }); } } fs.readFile(path.join(__dirname, '../../../data', 'user.json'), 'utf8', (err, data) => { if (err) { console.error('Error reading user.json:', err); return res.status(401).json({ message: 'Unauthorized' }); } const users = JSON.parse(data); const user = users.find(u => u.token === token); if (user) { req.user = user; req.userData = user; next(); } else { return res.status(401).json({ message: 'Unauthorized' }); } }); } router.get('/wallpaper', authenticateToken, (req, res) => { fs.readFile(path.join(__dirname, '../../../data', 'user.json'), 'utf8', (err, data) => { if (err) { return res.status(500).send('Error reading the file'); } const users = JSON.parse(data); const user = users.find(u => u.token === req.userData.token); res.json({ wallpaper: user.wallpaper || null }); }); }); router.post('/wallpaper', authenticateToken, (req, res) => { const newWallpaper = req.body.wallpaper; fs.readFile(path.join(__dirname, '../../../data', 'user.json'), 'utf8', (err, data) => { if (err) { return res.status(500).send('Error reading the file'); } let users = JSON.parse(data); const userIndex = users.findIndex(u => u.token === req.userData.token); if (userIndex !== -1) { users[userIndex].wallpaper = newWallpaper; fs.writeFile(path.join(__dirname, '../../../data', 'user.json'), JSON.stringify(users, null, 2), (err) => { if (err) { return res.status(500).send('Error writing to the file'); } res.send('Wallpaper updated'); }); } else { res.status(401).send('Unauthorized'); } }); }); module.exports = router;