Note: We appreciate your feedback and bug reports to continue improving our platform. Thank you for your continued support!
This commit is contained in:
@@ -2,165 +2,45 @@ 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());
|
||||
router.use(express.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
|
||||
*/
|
||||
router.post('/wallpaper', (req, res) => {
|
||||
const userId = req.body.userId;
|
||||
const wallpaperUrl = req.body.wallpaperUrl;
|
||||
|
||||
|
||||
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 (!wallpaperUrl) {
|
||||
return res.status(400).send('No wallpaper URL provided.');
|
||||
}
|
||||
|
||||
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 });
|
||||
});
|
||||
updateUserWallpaper(userId, wallpaperUrl, res);
|
||||
});
|
||||
|
||||
router.post('/wallpaper', authenticateToken, (req, res) => {
|
||||
const newWallpaper = req.body.wallpaper;
|
||||
const updateUserWallpaper = (userId, wallpaperUrl, res) => {
|
||||
const userFilePath = path.join(__dirname, '../../../data', 'user.json');
|
||||
|
||||
fs.readFile(path.join(__dirname, '../../../data', 'user.json'), 'utf8', (err, data) => {
|
||||
fs.readFile(userFilePath, '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);
|
||||
|
||||
const userIndex = users.findIndex(u => u.id === userId);
|
||||
|
||||
if (userIndex !== -1) {
|
||||
users[userIndex].wallpaper = newWallpaper;
|
||||
users[userIndex].wallpaper = wallpaperUrl;
|
||||
|
||||
fs.writeFile(path.join(__dirname, '../../../data', 'user.json'), JSON.stringify(users, null, 2), (err) => {
|
||||
fs.writeFile(userFilePath, JSON.stringify(users, null, 2), err => {
|
||||
if (err) {
|
||||
return res.status(500).send('Error writing to the file');
|
||||
}
|
||||
res.send('Wallpaper updated');
|
||||
res.json({ wallpaper: wallpaperUrl });
|
||||
});
|
||||
} else {
|
||||
res.status(401).send('Unauthorized');
|
||||
res.status(404).send('User not found');
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = router;
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user