mirror of
https://git.sr.ht/~coasteen/webui
synced 2025-11-04 11:37:34 +01:00
logging update
This commit is contained in:
parent
a74b0624ca
commit
13feee7b17
1 changed files with 90 additions and 56 deletions
146
server.js
146
server.js
|
|
@ -5,14 +5,35 @@ const fs = require('fs');
|
|||
|
||||
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({
|
||||
destination: (req, file, cb) => {
|
||||
cb(null, 'uploads/');
|
||||
},
|
||||
filename: (req, file, cb) => {
|
||||
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
|
||||
cb(null, uniqueSuffix + '-' + file.originalname);
|
||||
}
|
||||
destination: (req, file, cb) => {
|
||||
cb(null, 'uploads/');
|
||||
},
|
||||
filename: (req, file, cb) => {
|
||||
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
|
||||
cb(null, uniqueSuffix + '-' + file.originalname);
|
||||
}
|
||||
});
|
||||
|
||||
const upload = multer({ storage: storage });
|
||||
|
|
@ -20,69 +41,82 @@ const upload = multer({ storage: storage });
|
|||
app.use(express.static(__dirname));
|
||||
|
||||
app.post('/upload', upload.array('files'), (req, res) => {
|
||||
if (!req.files || req.files.length === 0) {
|
||||
return res.status(400).send('No files were uploaded.');
|
||||
}
|
||||
res.send(`Successfully uploaded ${req.files.length} file(s)!`);
|
||||
if (!req.files || req.files.length === 0) {
|
||||
logAction(`FAILED upload attempt: No files received.`);
|
||||
return res.status(400).send('No files were uploaded.');
|
||||
}
|
||||
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) => {
|
||||
const directoryPath = path.join(__dirname, 'uploads');
|
||||
fs.readdir(directoryPath, (err, files) => {
|
||||
if (err) {
|
||||
return res.status(500).send('Unable to scan directory: ' + err);
|
||||
}
|
||||
const fileListPromises = files.map(file => {
|
||||
return new Promise((resolve) => {
|
||||
const filePath = path.join(directoryPath, file);
|
||||
fs.stat(filePath, (err, stats) => {
|
||||
if (err) {
|
||||
return resolve(null);
|
||||
}
|
||||
resolve({
|
||||
name: file,
|
||||
path: `/uploads/${file}`,
|
||||
size: stats.size,
|
||||
date: stats.mtime
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
logAction(`LIST files requested.`);
|
||||
const directoryPath = path.join(__dirname, 'uploads');
|
||||
fs.readdir(directoryPath, (err, files) => {
|
||||
if (err) {
|
||||
return res.status(500).send('Unable to scan directory: ' + err);
|
||||
}
|
||||
const fileListPromises = files.map(file => {
|
||||
return new Promise((resolve) => {
|
||||
const filePath = path.join(directoryPath, file);
|
||||
fs.stat(filePath, (err, stats) => {
|
||||
if (err) {
|
||||
return resolve(null);
|
||||
}
|
||||
resolve({
|
||||
name: file,
|
||||
path: `/uploads/${file}`,
|
||||
size: stats.size,
|
||||
date: stats.mtime
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Promise.all(fileListPromises).then(fileList => {
|
||||
res.json(fileList.filter(file => file !== null));
|
||||
});
|
||||
});
|
||||
Promise.all(fileListPromises).then(fileList => {
|
||||
res.json(fileList.filter(file => file !== null));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/uploads/:filename', (req, res) => {
|
||||
const filePath = path.join(__dirname, 'uploads', req.params.filename);
|
||||
res.sendFile(filePath, err => {
|
||||
if (err) {
|
||||
res.status(404).send('File not found');
|
||||
}
|
||||
});
|
||||
const filename = req.params.filename;
|
||||
logAction(`PREVIEW/ACCESS file: ${filename}`);
|
||||
const filePath = path.join(__dirname, 'uploads', filename);
|
||||
res.sendFile(filePath, err => {
|
||||
if (err) {
|
||||
res.status(404).send(`File not found: ${filename}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/download/:filename', (req, res) => {
|
||||
const filePath = path.join(__dirname, 'uploads', req.params.filename);
|
||||
res.download(filePath, err => {
|
||||
if (err) {
|
||||
res.status(404).send('File not found');
|
||||
}
|
||||
});
|
||||
const filename = req.params.filename;
|
||||
logAction(`DOWNLOAD file: ${filename}`);
|
||||
const filePath = path.join(__dirname, 'uploads', filename);
|
||||
res.download(filePath, err => {
|
||||
if (err) {
|
||||
res.status(404).send(`File not found for download: ${filename}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.delete('/delete/:filename', (req, res) => {
|
||||
const filePath = path.join(__dirname, 'uploads', req.params.filename);
|
||||
fs.unlink(filePath, err => {
|
||||
if (err) {
|
||||
return res.status(404).send('File not found');
|
||||
}
|
||||
res.send('File deleted successfully');
|
||||
});
|
||||
const filename = req.params.filename;
|
||||
const filePath = path.join(__dirname, 'uploads', filename);
|
||||
fs.unlink(filePath, err => {
|
||||
if (err) {
|
||||
logAction(`FAILED deletion of file: ${filename}`);
|
||||
return res.status(404).send('File not found');
|
||||
}
|
||||
logAction(`DELETED file: ${filename}`);
|
||||
res.send('File deleted successfully');
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(3000, () => {
|
||||
console.log('[*] Server is running on http://localhost:3000');
|
||||
if (config.loggingEnabled) {
|
||||
console.log('[*] Server is running on http://localhost:3000');
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue