Several modifications made to the API and several bug fixes
This commit is contained in:
158
routes/Dpanel/API/getFileFolder.js
Normal file
158
routes/Dpanel/API/getFileFolder.js
Normal file
@@ -0,0 +1,158 @@
|
||||
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.post('/:folderName', authenticateToken, async (req, res) => {
|
||||
const userName = req.userData.name;
|
||||
const folderName = req.params.folderName;
|
||||
const downloadDir = path.join('cdn-files', userName, folderName);
|
||||
|
||||
if (!fs.existsSync(downloadDir)) {
|
||||
return res.status(404).json({ error: 'The specified folder does not exist.' });
|
||||
}
|
||||
|
||||
try {
|
||||
const files = await fs.promises.readdir(downloadDir);
|
||||
|
||||
const fileDetails = files.map(file => {
|
||||
const filePath = path.join(downloadDir, file);
|
||||
const stats = fs.statSync(filePath);
|
||||
const fileType = stats.isDirectory() ? 'folder' : 'file';
|
||||
|
||||
return {
|
||||
name: file,
|
||||
type: fileType
|
||||
};
|
||||
});
|
||||
|
||||
res.json({ files: fileDetails });
|
||||
} catch (err) {
|
||||
console.error('Error reading directory:', err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user