Files
Dinawo ce8f1bbbac
All checks were successful
continuous-integration/drone/push Build is passing
Update v1.0.0-beta.13
Several modifications made to the API and several bug fixes
2024-06-02 18:13:28 +02:00

166 lines
4.7 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;
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/getfile?token={token}:
* post:
* security:
* - bearerAuth: []
* tags:
* - File
* summary: Get files and folders in a specific folder
* 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: 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:
* status:
* type: string
* message:
* 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
* 404:
* description: The specified file does not exist
* content:
* application/json:
* schema:
* type: object
* properties:
* status:
* type: string
* message:
* 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('/', authenticateToken, async (req, res) => {
const userName = req.userData.name;
const downloadDir = path.join('cdn-files', userName);
if (!fs.existsSync(downloadDir)) {
fs.mkdirSync(downloadDir, { recursive: true });
}
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;