ramirez/src/webserver.js

72 lines
2.0 KiB
JavaScript
Raw Normal View History

2017-02-09 21:56:36 -05:00
const http = require('http');
const mime = require('mime');
2017-02-09 23:36:47 -05:00
const url = require('url');
const fs = require('fs');
2017-02-09 21:56:36 -05:00
const config = require('../config');
2017-02-09 23:36:47 -05:00
const logs = require('./logs');
const attachments = require('./attachments');
2017-02-09 21:56:36 -05:00
const port = config.port || 8890;
function serveLogs(res, pathParts) {
const token = pathParts[pathParts.length - 1];
if (token.match(/^[0-9a-f]+$/) === null) return res.end();
2017-02-09 23:36:47 -05:00
logs.findLogFile(token).then(logFilename => {
if (logFilename === null) return res.end();
2017-02-09 21:56:36 -05:00
2017-02-09 23:36:47 -05:00
fs.readFile(logs.getLogFilePath(logFilename), {encoding: 'utf8'}, (err, data) => {
2017-02-09 21:56:36 -05:00
if (err) {
res.statusCode = 404;
res.end('Log not found');
return;
}
res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
res.end(data);
});
});
}
function serveAttachments(res, pathParts) {
const desiredFilename = pathParts[pathParts.length - 1];
const id = pathParts[pathParts.length - 2];
if (id.match(/^[0-9]+$/) === null) return res.end();
if (desiredFilename.match(/^[0-9a-z\._-]+$/i) === null) return res.end();
2017-02-09 23:36:47 -05:00
const attachmentPath = attachments.getPath(id);
2017-02-09 21:56:36 -05:00
fs.access(attachmentPath, (err) => {
if (err) {
res.statusCode = 404;
res.end('Attachment not found');
return;
}
const filenameParts = desiredFilename.split('.');
const ext = (filenameParts.length > 1 ? filenameParts[filenameParts.length - 1] : 'bin');
const fileMime = mime.lookup(ext);
res.setHeader('Content-Type', fileMime);
const read = fs.createReadStream(attachmentPath);
read.pipe(res);
})
}
function run() {
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(`http://${req.url}`);
const pathParts = parsedUrl.path.split('/').filter(v => v !== '');
if (parsedUrl.path.startsWith('/logs/')) serveLogs(res, pathParts);
if (parsedUrl.path.startsWith('/attachments/')) serveAttachments(res, pathParts);
});
2017-02-09 23:36:47 -05:00
server.listen(port);
2017-02-09 21:56:36 -05:00
}
module.exports = {
run,
};