logging update

This commit is contained in:
coast 2025-10-02 11:55:05 +03:30
parent a74b0624ca
commit 13feee7b17

146
server.js
View file

@ -5,14 +5,35 @@ const fs = require('fs');
const app = express(); const app = express();
function loadConfig() {
try {
const configPath = path.join(__dirname, 'config.conf');
const content = fs.readFileSync(configPath, 'utf8');
const match = content.match(/^logs\s*=\s*(.*)$/m);
if (match && match[1].trim().toLowerCase() === 'disable') {
return { loggingEnabled: false };
}
} catch (e) {
}
return { loggingEnabled: true };
}
const config = loadConfig();
function logAction(message) {
if (config.loggingEnabled) {
console.log(`[ACTION] ${message}`);
}
}
const storage = multer.diskStorage({ const storage = multer.diskStorage({
destination: (req, file, cb) => { destination: (req, file, cb) => {
cb(null, 'uploads/'); cb(null, 'uploads/');
}, },
filename: (req, file, cb) => { filename: (req, file, cb) => {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9); const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
cb(null, uniqueSuffix + '-' + file.originalname); cb(null, uniqueSuffix + '-' + file.originalname);
} }
}); });
const upload = multer({ storage: storage }); const upload = multer({ storage: storage });
@ -20,69 +41,82 @@ const upload = multer({ storage: storage });
app.use(express.static(__dirname)); app.use(express.static(__dirname));
app.post('/upload', upload.array('files'), (req, res) => { app.post('/upload', upload.array('files'), (req, res) => {
if (!req.files || req.files.length === 0) { if (!req.files || req.files.length === 0) {
return res.status(400).send('No files were uploaded.'); logAction(`FAILED upload attempt: No files received.`);
} return res.status(400).send('No files were uploaded.');
res.send(`Successfully uploaded ${req.files.length} file(s)!`); }
const uploadedNames = req.files.map(f => f.filename).join(', ');
logAction(`UPLOADED ${req.files.length} file(s): ${uploadedNames}`);
res.send(`Successfully uploaded ${req.files.length} file(s)!`);
}); });
app.get('/files', (req, res) => { app.get('/files', (req, res) => {
const directoryPath = path.join(__dirname, 'uploads'); logAction(`LIST files requested.`);
fs.readdir(directoryPath, (err, files) => { const directoryPath = path.join(__dirname, 'uploads');
if (err) { fs.readdir(directoryPath, (err, files) => {
return res.status(500).send('Unable to scan directory: ' + err); if (err) {
} return res.status(500).send('Unable to scan directory: ' + err);
const fileListPromises = files.map(file => { }
return new Promise((resolve) => { const fileListPromises = files.map(file => {
const filePath = path.join(directoryPath, file); return new Promise((resolve) => {
fs.stat(filePath, (err, stats) => { const filePath = path.join(directoryPath, file);
if (err) { fs.stat(filePath, (err, stats) => {
return resolve(null); if (err) {
} return resolve(null);
resolve({ }
name: file, resolve({
path: `/uploads/${file}`, name: file,
size: stats.size, path: `/uploads/${file}`,
date: stats.mtime size: stats.size,
}); date: stats.mtime
}); });
}); });
}); });
});
Promise.all(fileListPromises).then(fileList => { Promise.all(fileListPromises).then(fileList => {
res.json(fileList.filter(file => file !== null)); res.json(fileList.filter(file => file !== null));
}); });
}); });
}); });
app.get('/uploads/:filename', (req, res) => { app.get('/uploads/:filename', (req, res) => {
const filePath = path.join(__dirname, 'uploads', req.params.filename); const filename = req.params.filename;
res.sendFile(filePath, err => { logAction(`PREVIEW/ACCESS file: ${filename}`);
if (err) { const filePath = path.join(__dirname, 'uploads', filename);
res.status(404).send('File not found'); res.sendFile(filePath, err => {
} if (err) {
}); res.status(404).send(`File not found: ${filename}`);
}
});
}); });
app.get('/download/:filename', (req, res) => { app.get('/download/:filename', (req, res) => {
const filePath = path.join(__dirname, 'uploads', req.params.filename); const filename = req.params.filename;
res.download(filePath, err => { logAction(`DOWNLOAD file: ${filename}`);
if (err) { const filePath = path.join(__dirname, 'uploads', filename);
res.status(404).send('File not found'); res.download(filePath, err => {
} if (err) {
}); res.status(404).send(`File not found for download: ${filename}`);
}
});
}); });
app.delete('/delete/:filename', (req, res) => { app.delete('/delete/:filename', (req, res) => {
const filePath = path.join(__dirname, 'uploads', req.params.filename); const filename = req.params.filename;
fs.unlink(filePath, err => { const filePath = path.join(__dirname, 'uploads', filename);
if (err) { fs.unlink(filePath, err => {
return res.status(404).send('File not found'); if (err) {
} logAction(`FAILED deletion of file: ${filename}`);
res.send('File deleted successfully'); return res.status(404).send('File not found');
}); }
logAction(`DELETED file: ${filename}`);
res.send('File deleted successfully');
});
}); });
app.listen(3000, () => { app.listen(3000, () => {
console.log('[*] Server is running on http://localhost:3000'); if (config.loggingEnabled) {
console.log('[*] Server is running on http://localhost:3000');
}
}); });