Urgent correction of version v1.0.0-beta.14 due to crash issues when acting on the CDN.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
We would like to apologize for the inconvenience caused and we would like to thank you for the quick report.
This commit is contained in:
@@ -2,6 +2,7 @@ const express = require('express');
|
||||
const router = express.Router();
|
||||
const path = require('path');
|
||||
const fs = require('fs').promises;
|
||||
const fsStandard = require('fs');
|
||||
const mime = require('mime-types');
|
||||
const { logger, ErrorLogger } = require('../config/logs');
|
||||
const bcrypt = require('bcrypt');
|
||||
@@ -27,7 +28,12 @@ async function findFileInUserDir(userId, filename) {
|
||||
}
|
||||
|
||||
async function findFileInDir(dir, filename) {
|
||||
const files = await fs.readdir(dir, { withFileTypes: true });
|
||||
let files;
|
||||
try {
|
||||
files = await fs.readdir(dir, { withFileTypes: true });
|
||||
} catch (err) {
|
||||
return null; // Directory does not exist
|
||||
}
|
||||
|
||||
for (const file of files) {
|
||||
const filePath = path.join(dir, file.name);
|
||||
@@ -65,15 +71,16 @@ router.get('/:userId/:filename', async (req, res) => {
|
||||
fileInfoArray = JSON.parse(data);
|
||||
} catch (error) {
|
||||
console.error('Error parsing file_info.json:', error);
|
||||
return res.status(500).send('Error reading file info.');
|
||||
}
|
||||
|
||||
if (!Array.isArray(fileInfoArray)) {
|
||||
console.error('fileInfoArray is not an array');
|
||||
fileInfoArray = [];
|
||||
return res.status(500).send('Invalid file info format.');
|
||||
}
|
||||
|
||||
const fileInfo = fileInfoArray.find(info => info.fileName === filename && info.Id === userId);
|
||||
|
||||
|
||||
if (fileInfo) {
|
||||
const expiryDate = new Date(fileInfo.expiryDate);
|
||||
const now = new Date();
|
||||
@@ -88,26 +95,11 @@ router.get('/:userId/:filename', async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
const fileContent = await fs.readFile(filePath);
|
||||
let mimeType = mime.lookup(filePath);
|
||||
const readStream = fsStandard.createReadStream(filePath);
|
||||
let mimeType = mime.lookup(filePath) || 'application/octet-stream';
|
||||
|
||||
if (!mimeType) {
|
||||
if (filePath.endsWith('.txt')) {
|
||||
mimeType = 'text/plain';
|
||||
} else if (filePath.endsWith('.pdf')) {
|
||||
mimeType = 'application/pdf';
|
||||
}
|
||||
}
|
||||
|
||||
if (mimeType) {
|
||||
res.setHeader('Content-Type', mimeType);
|
||||
}
|
||||
|
||||
if (mimeType === 'text/plain') {
|
||||
res.end(fileContent);
|
||||
} else {
|
||||
res.send(fileContent);
|
||||
}
|
||||
res.setHeader('Content-Type', mimeType);
|
||||
readStream.pipe(res);
|
||||
|
||||
if (fileInfo) {
|
||||
req.session.passwordVerified = false;
|
||||
@@ -129,11 +121,12 @@ router.post('/:userId/:filename', async (req, res) => {
|
||||
fileInfoArray = JSON.parse(data);
|
||||
} catch (error) {
|
||||
console.error('Error parsing file_info.json:', error);
|
||||
return res.status(500).send('Error reading file info.');
|
||||
}
|
||||
|
||||
if (!Array.isArray(fileInfoArray)) {
|
||||
console.error('fileInfoArray is not an array');
|
||||
fileInfoArray = [];
|
||||
return res.status(500).send('Invalid file info format.');
|
||||
}
|
||||
|
||||
const fileInfo = fileInfoArray.find(info => info.fileName === filename && info.Id === userId);
|
||||
@@ -142,21 +135,21 @@ router.post('/:userId/:filename', async (req, res) => {
|
||||
return res.json({ success: false, message: 'File not found' });
|
||||
}
|
||||
|
||||
if (bcrypt.compareSync(enteredPassword, fileInfo.password)) {
|
||||
const passwordMatch = await bcrypt.compare(enteredPassword, fileInfo.password);
|
||||
if (passwordMatch) {
|
||||
req.session.passwordVerified = true;
|
||||
const filePath = await findFileInUserDir(userId, filename);
|
||||
const fileContent = await fs.readFile(filePath);
|
||||
let mimeType = mime.lookup(filePath);
|
||||
const readStream = fsStandard.createReadStream(filePath);
|
||||
let mimeType = mime.lookup(filePath) || 'application/octet-stream';
|
||||
|
||||
if (!mimeType) {
|
||||
if (filePath.endsWith('.txt')) {
|
||||
mimeType = 'text/plain';
|
||||
} else if (filePath.endsWith('.pdf')) {
|
||||
mimeType = 'application/pdf';
|
||||
}
|
||||
}
|
||||
let fileContent = '';
|
||||
readStream.on('data', chunk => {
|
||||
fileContent += chunk.toString('base64');
|
||||
});
|
||||
|
||||
res.json({ success: true, fileContent: fileContent.toString('base64'), mimeType });
|
||||
readStream.on('end', () => {
|
||||
res.json({ success: true, fileContent, mimeType });
|
||||
});
|
||||
} else {
|
||||
res.json({ success: false, message: 'Incorrect password' });
|
||||
}
|
||||
@@ -167,24 +160,31 @@ router.post('/:userId/:filename', async (req, res) => {
|
||||
});
|
||||
|
||||
async function deleteExpiredFiles() {
|
||||
let data = await fs.readFile(path.join(__dirname, '../data', 'file_info.json'), 'utf8');
|
||||
let data;
|
||||
try {
|
||||
data = await fs.readFile(path.join(__dirname, '../data', 'file_info.json'), 'utf8');
|
||||
} catch (error) {
|
||||
console.error('Error reading file_info.json:', error);
|
||||
return;
|
||||
}
|
||||
|
||||
let fileInfoArray;
|
||||
try {
|
||||
fileInfoArray = JSON.parse(data);
|
||||
} catch (error) {
|
||||
console.error('Error parsing file_info.json:', error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Array.isArray(fileInfoArray)) {
|
||||
console.error('fileInfoArray is not an array');
|
||||
fileInfoArray = [];
|
||||
return;
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
let newFileInfoArray = [];
|
||||
|
||||
for (let i = 0; i < fileInfoArray.length; i++) {
|
||||
const fileInfo = fileInfoArray[i];
|
||||
for (const fileInfo of fileInfoArray) {
|
||||
let expiryDate;
|
||||
if (fileInfo.expiryDate && fileInfo.expiryDate.trim() !== '') {
|
||||
expiryDate = new Date(fileInfo.expiryDate);
|
||||
@@ -193,10 +193,10 @@ async function deleteExpiredFiles() {
|
||||
}
|
||||
|
||||
if (expiryDate < now) {
|
||||
const samaccountname = await getSamAccountNameFromUserId(fileInfo.userId);
|
||||
const userDir = path.join(baseDir, samaccountname);
|
||||
const filePath = path.join(userDir, fileInfo.fileName);
|
||||
try {
|
||||
const samaccountname = await getSamAccountNameFromUserId(fileInfo.userId);
|
||||
const userDir = path.join(baseDir, samaccountname);
|
||||
const filePath = path.join(userDir, fileInfo.fileName);
|
||||
await fs.unlink(filePath);
|
||||
} catch (err) {
|
||||
ErrorLogger.error('Error deleting file:', err);
|
||||
@@ -207,7 +207,7 @@ async function deleteExpiredFiles() {
|
||||
}
|
||||
|
||||
try {
|
||||
await fs.writeFile(path.join(__dirname, '../data', 'file_info.json'), JSON.stringify(newFileInfoArray, null, 2), 'utf8');
|
||||
await fs.writeFile(path.join(__dirname, '../data', 'file_info.json'), JSON.stringify(newFileInfoArray, null, 2), 'utf8');
|
||||
} catch (err) {
|
||||
ErrorLogger.error('Error writing to file_info.json:', err);
|
||||
}
|
||||
@@ -215,4 +215,4 @@ async function deleteExpiredFiles() {
|
||||
|
||||
setInterval(deleteExpiredFiles, 24 * 60 * 60 * 1000);
|
||||
|
||||
module.exports = router;
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user