Files
CDN-APP-INSIDER/routes/Dpanel/API/GetMetaDataFile.js
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

130 lines
4.0 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/getfile?token={token}:
* post:
* security:
* - bearerAuth: []
* tags:
* - File
* summary: Get file information
* description: This route allows you to get information about a specific file. It requires a valid JWT token in the Authorization header.
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* fileLink:
* type: string
* description: The link of the file to get information about
* 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:
* Id:
* type: string
* fileName:
* 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 or no information found for the file
* content:
* application/json:
* schema:
* type: object
* properties:
* error:
* type: string
* 500:
* description: Error reading the file
* content:
* application/json:
* schema:
* type: object
* properties:
* error:
* type: string
*/
router.get('/', (req, res) => {
res.status(400).json({ error: 'Bad Request. The request cannot be fulfilled due to bad syntax or missing parameters.' });
});
router.post('/file_info', authMiddleware, (req, res) => {
const filePath = path.join(__dirname, '../../../data', 'file_info.json');
if (!fs.existsSync(filePath)) {
return res.status(404).json({ error: 'The specified file does not exist.' });
}
fs.readFile(filePath, 'utf-8', (err, data) => {
if (err) {
console.error(err);
return res.status(500).json({ error: 'Error reading the file.' });
}
const fileInfos = JSON.parse(data);
const fileLink = req.body.fileLink;
const fileName = fileLink.split('/').pop();
const fileInfo = fileInfos.find(file => file.fileName === fileName && file.Id);
console.log(fileInfos);
if (!fileInfo) {
return res.status(404).json({ error: `No information found for the file ${fileName}.` });
}
res.json(fileInfo);
});
});
module.exports = router;