Several modifications made to the API and several bug fixes
This commit is contained in:
@@ -18,42 +18,112 @@ let setupData = getSetupData();
|
||||
let userData = getUserData();
|
||||
router.use(bodyParser.json());
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /dashboard/newfolder?token={token}:
|
||||
* post:
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* tags:
|
||||
* - Folder
|
||||
* summary: Create a new folder
|
||||
* description: This route allows you to create a new folder. It requires a valid JWT token in the Authorization header.
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* folderName:
|
||||
* type: string
|
||||
* description: The name of the new folder
|
||||
* parameters:
|
||||
* - 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:
|
||||
* message:
|
||||
* type: string
|
||||
* 400:
|
||||
* description: Bad Request
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* message:
|
||||
* type: string
|
||||
* 401:
|
||||
* description: Unauthorized
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* message:
|
||||
* type: string
|
||||
* 500:
|
||||
* description: Error creating the folder
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* message:
|
||||
* type: string
|
||||
* 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' });
|
||||
authMiddleware(req, res, function(err) {
|
||||
if (!err) {
|
||||
return next();
|
||||
}
|
||||
}
|
||||
|
||||
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' });
|
||||
|
||||
let token = null;
|
||||
const authHeader = req.headers['authorization'];
|
||||
|
||||
if (authHeader) {
|
||||
token = authHeader.split(' ')[1];
|
||||
} else if (req.query.token) {
|
||||
token = req.query.token;
|
||||
}
|
||||
|
||||
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' });
|
||||
|
||||
if (token == null) {
|
||||
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('/', (req, res) => {
|
||||
@@ -62,12 +132,10 @@ router.get('/', (req, res) => {
|
||||
|
||||
router.post('/', authenticateToken, (req, res) => {
|
||||
try {
|
||||
logger.info('Received POST request to create a new folder.');
|
||||
|
||||
const userId = req.userData.name;
|
||||
const userId = req.user.name;
|
||||
let { folderName } = req.body;
|
||||
|
||||
logger.info('Received folderName:', folderName);
|
||||
|
||||
if (!folderName || typeof folderName !== 'string') {
|
||||
ErrorLogger.error('Invalid folderName:', folderName);
|
||||
@@ -83,7 +151,6 @@ router.post('/', authenticateToken, (req, res) => {
|
||||
const folderPath = path.join('cdn-files', userId, folderName);
|
||||
|
||||
if (fs.existsSync(folderPath)) {
|
||||
logger.info('Folder already exists:', folderPath);
|
||||
return res.status(400).json({ message: 'Folder already exists.' });
|
||||
}
|
||||
|
||||
@@ -92,7 +159,6 @@ router.post('/', authenticateToken, (req, res) => {
|
||||
ErrorLogger.error(err);
|
||||
return res.status(500).json({ message: 'Error creating folder.', error: err });
|
||||
}
|
||||
logger.info('Folder created successfully:', folderPath);
|
||||
res.status(200).json({ message: 'Folder created successfully.' });
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user