Files
CDN-APP-INSIDER/routes/Dpanel/API/DeleteFolfder.js
Dinawo aaff0ed4ea
All checks were successful
continuous-integration/drone/push Build is passing
Urgent correction of version v1.0.0-beta.14 due to crash issues when acting on the CDN.
We would like to apologize for the inconvenience caused and we would like to thank you for the quick report.
2024-07-12 18:07:19 +02:00

183 lines
5.6 KiB
JavaScript

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;
const configFile = fs.readFileSync(path.join(__dirname, '../../../data', 'setup.json'), 'utf-8')
const config = JSON.parse(configFile);
const bodyParser = require('body-parser');
const crypto = require('crypto');
const os = require('os');
const { getUserData, getSetupData } = require('../../../Middlewares/watcherMiddleware');
const { logger, logRequestInfo, ErrorLogger, authLogger } = require('../../../config/logs');
let setupData = getSetupData();
let userData = getUserData();
router.use(bodyParser.json());
/**
* @swagger
* /dashboard/deletefolder/{folderName}?token={token}:
* post:
* security:
* - bearerAuth: []
* tags:
* - Folder
* summary: Delete a specific file in folder for a user
* description: This route allows you to delete a specific file for a user. It requires a valid JWT token in the Authorization header.
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* filename:
* type: string
* description: The name of the file to delete
* 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: Folder successfully deleted.
* content:
* application/json:
* schema:
* type: object
* properties:
* deleted:
* type: boolean
* success:
* type: string
* 400:
* description: Bad Request
* content:
* application/json:
* schema:
* type: object
* properties:
* error:
* type: string
* 401:
* description: Unauthorized
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* 403:
* description: You do not have permission to delete this folder.
* content:
* application/json:
* schema:
* type: object
* properties:
* error:
* type: string
* 404:
* description: The specified folder does not exist.
* content:
* application/json:
* schema:
* type: object
* properties:
* error:
* type: string
* 500:
* description: Error deleting the folder.
* content:
* application/json:
* schema:
* type: object
* properties:
* error:
* type: string
*/
function authenticateToken(req, res, next) {
authMiddleware(req, res, function(err) {
if (!err) {
return 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) {
return res.status(401).json({ message: 'Unauthorized' });
}
fs.readFile(path.join(__dirname, '../../../data', 'user.jso,'), 'utf8', (err, data) => {
if (err) {
console.error('Error reading user.jso,:', 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('/', (req, res) => {
res.status(400).json({ error: 'Bad Request. The request cannot be fulfilled due to bad syntax or missing parameters.' });
});
router.delete('/:folderName', authenticateToken, (req, res) => {
const userId = req.userData ? req.userData.name : null;
if (!userId) {
return res.status(401).json({ error: 'Unauthorized' });
}
const folderName = req.params.folderName;
const userFolderPath = path.join('cdn-files', userId);
const folderPath = path.join(userFolderPath, folderName);
if (!fs.existsSync(folderPath)) {
return res.status(404).json({ error: 'The specified folder does not exist.' });
}
if (!folderPath.startsWith(userFolderPath)) {
return res.status(403).json({ error: 'You do not have permission to delete this folder.' });
}
fs.rmdir(folderPath, { recursive: true }, (err) => {
if (err) {
return res.status(500).json({ error: 'Error deleting the folder.' });
}
res.json({ deleted: true, success: 'Folder successfully deleted.' });
});
});
module.exports = router;