2020-10-03 09:40:05 -04:00
|
|
|
const express = require("express");
|
|
|
|
const helmet = require("helmet");
|
2020-08-12 17:08:37 -04:00
|
|
|
const mime = require("mime");
|
|
|
|
const url = require("url");
|
|
|
|
const fs = require("fs");
|
|
|
|
const qs = require("querystring");
|
|
|
|
const moment = require("moment");
|
|
|
|
const config = require("../cfg");
|
|
|
|
const threads = require("../data/threads");
|
|
|
|
const attachments = require("../data/attachments");
|
2020-08-13 17:42:32 -04:00
|
|
|
const { formatters } = require("../formatters");
|
2018-02-11 14:54:30 -05:00
|
|
|
|
|
|
|
function notfound(res) {
|
2020-10-03 09:40:05 -04:00
|
|
|
res.status(404).send("Page Not Found");
|
2018-02-11 14:54:30 -05:00
|
|
|
}
|
|
|
|
|
2020-10-03 09:40:05 -04:00
|
|
|
/**
|
|
|
|
* @param {express.Request} req
|
|
|
|
* @param {express.Response} res
|
|
|
|
*/
|
|
|
|
async function serveLogs(req, res) {
|
|
|
|
const thread = await threads.findById(req.params.threadId);
|
2018-02-11 14:54:30 -05:00
|
|
|
if (! thread) return notfound(res);
|
|
|
|
|
2020-05-24 18:54:26 -04:00
|
|
|
let threadMessages = await thread.getThreadMessages();
|
|
|
|
|
2020-08-13 17:42:32 -04:00
|
|
|
const formatLogResult = await formatters.formatLog(thread, threadMessages, {
|
2020-10-03 09:40:05 -04:00
|
|
|
simple: Boolean(req.query.simple),
|
|
|
|
verbose: Boolean(req.query.verbose),
|
2018-02-11 14:54:30 -05:00
|
|
|
});
|
|
|
|
|
2020-08-13 17:42:32 -04:00
|
|
|
const contentType = formatLogResult.extra && formatLogResult.extra.contentType || "text/plain; charset=UTF-8";
|
2020-05-24 18:54:26 -04:00
|
|
|
|
2020-10-03 09:40:05 -04:00
|
|
|
res.set("Content-Type", contentType);
|
|
|
|
res.send(formatLogResult.content);
|
2018-02-11 14:54:30 -05:00
|
|
|
}
|
|
|
|
|
2020-10-03 09:40:05 -04:00
|
|
|
function serveAttachments(req, res) {
|
|
|
|
if (req.params.attachmentId.match(/^[0-9]+$/) === null) return notfound(res);
|
|
|
|
if (req.params.filename.match(/^[0-9a-z._-]+$/i) === null) return notfound(res);
|
2018-02-11 14:54:30 -05:00
|
|
|
|
2020-10-03 09:40:05 -04:00
|
|
|
const attachmentPath = attachments.getLocalAttachmentPath(req.params.attachmentId);
|
2018-02-11 14:54:30 -05:00
|
|
|
fs.access(attachmentPath, (err) => {
|
|
|
|
if (err) return notfound(res);
|
|
|
|
|
2020-10-03 09:40:05 -04:00
|
|
|
const filenameParts = req.params.filename.split(".");
|
2020-08-12 17:08:37 -04:00
|
|
|
const ext = (filenameParts.length > 1 ? filenameParts[filenameParts.length - 1] : "bin");
|
2018-08-07 18:10:38 -04:00
|
|
|
const fileMime = mime.getType(ext);
|
2018-02-11 14:54:30 -05:00
|
|
|
|
2020-10-03 09:40:05 -04:00
|
|
|
res.set("Content-Type", fileMime);
|
2018-02-11 14:54:30 -05:00
|
|
|
|
|
|
|
const read = fs.createReadStream(attachmentPath);
|
|
|
|
read.pipe(res);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:13 -04:00
|
|
|
const server = express();
|
|
|
|
server.use(helmet());
|
2018-02-11 14:54:30 -05:00
|
|
|
|
2020-10-03 19:10:13 -04:00
|
|
|
server.get("/logs/:threadId", serveLogs);
|
|
|
|
server.get("/logs/:attachmentId/:filename", serveAttachments);
|
2018-02-11 14:54:30 -05:00
|
|
|
|
2020-10-03 19:10:13 -04:00
|
|
|
server.on("error", err => {
|
|
|
|
console.log("[WARN] Web server error:", err.message);
|
|
|
|
});
|
2018-08-07 17:54:01 -04:00
|
|
|
|
2020-10-04 09:46:43 -04:00
|
|
|
module.exports = server;
|