From 0ae14e83a1ecd2d99b9933ecd10a2232dcf97f7a Mon Sep 17 00:00:00 2001 From: Drew Lemmy Date: Sun, 12 Jan 2020 14:06:52 +0000 Subject: [PATCH] Redirect to raw paste for certain useragents --- README.md | 4 +++- config.example.json | 3 ++- index.js | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0cb638a..8a0ea3c 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,8 @@ Make a file called `config.json`, with the following properties: "videoFiles": ["mp4","webm"], "languagePackages": [ "language-lua" - ] + ], + "rawPasteAgents": "^(?:computercraft|curl|wget)" } ``` - `logo` - Filenames of your logos images. Make sure to put them in /public. @@ -59,6 +60,7 @@ Make a file called `config.json`, with the following properties: - `audioFiles` Array of file extentions that shitty will treat as audio. - `videoFiles` Array of file extentions that shitty will treat as video. - `languagePackages` Array of npm package names containing atom language grammars. +- `rawPasteAgents` Regular expression (case-insensitive) matching agents to return raw pastes for (on `/paste` and `/edit`). Use `null` to disable. ## Custom names diff --git a/config.example.json b/config.example.json index b025d23..2acf1ef 100644 --- a/config.example.json +++ b/config.example.json @@ -27,5 +27,6 @@ "videoFiles": ["mp4","webm"], "languagePackages": [ "language-lua" - ] + ], + "rawPasteAgents": "^(?:computercraft|curl|wget)" } \ No newline at end of file diff --git a/index.js b/index.js index a149850..495d5ed 100644 --- a/index.js +++ b/index.js @@ -541,6 +541,10 @@ router.post("/rename", (req, res) => { }); }); +function shouldReturnRaw(req) { + return config.rawPasteAgents && new RegExp(config.rawPasteAgents, "i").test(req.get("User-Agent")); +} + router.post("/edit", (req, res) => { if (typeof req.body.nonce === "undefined" || typeof req.body.file === "undefined" || typeof noncesLookup[req.body.nonce] === "undefined") return error(req, res, "No file specified."); @@ -566,6 +570,8 @@ router.post("/edit", (req, res) => { }); router.get("/paste/:file", (req, res) => { + if (shouldReturnRaw(req)) return res.redirect("/" + req.params.file); + const filename = sanitizeFilename(req.params.file); const filePath = path.join(config.imagePath, filename); @@ -597,6 +603,8 @@ router.get("/paste/:file", (req, res) => { }); router.get("/edit/:file", (req, res) => { + if (shouldReturnRaw(req)) return res.redirect("/" + req.params.file); + let editor = true; if (!req.session || !req.session.authed) { if (!req.body.password) editor = undefined;