From b135dc79eae98d4238d5676b59a32a37d4f2f1c4 Mon Sep 17 00:00:00 2001 From: "Jack W." Date: Tue, 24 Oct 2023 16:33:15 -0400 Subject: [PATCH] refactor: :recycle: Use .replaceAll instead of /g --- src/server/index.ts | 189 +++++++++++++++++++++++---------------- src/server/lib/errors.ts | 6 +- src/server/lib/mail.ts | 41 +++++---- tsconfig.json | 2 +- 4 files changed, 137 insertions(+), 101 deletions(-) diff --git a/src/server/index.ts b/src/server/index.ts index 6baf6c6..f023eaa 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -1,14 +1,14 @@ -import cookieParser from "cookie-parser"; +import cookieParser from "cookie-parser" import { IntentsBitField, Client } from "discord.js" import express from "express" import fs from "fs" -import bytes from "bytes"; +import bytes from "bytes" import ServeError from "./lib/errors" import Files from "./lib/files" import * as auth from "./lib/auth" import * as Accounts from "./lib/accounts" -import { getAccount } from "./lib/middleware"; +import { getAccount } from "./lib/middleware" import APIRouter from "./routes/api" @@ -18,9 +18,9 @@ let pkg = require(`${process.cwd()}/package.json`) let app = express() let config = require(`${process.cwd()}/config.json`) -app.use("/static/assets",express.static("assets")) -app.use("/static/style",express.static("out/style")) -app.use("/static/js",express.static("out/client")) +app.use("/static/assets", express.static("assets")) +app.use("/static/style", express.static("out/style")) +app.use("/static/js", express.static("out/client")) //app.use(bodyParser.text({limit:(config.maxDiscordFileSize*config.maxDiscordFiles)+1048576,type:["application/json","text/plain"]})) @@ -29,34 +29,41 @@ app.use(cookieParser()) // check for ssl, if not redirect if (config.trustProxy) app.enable("trust proxy") if (config.forceSSL) { - app.use((req,res,next) => { - if (req.protocol == "http") res.redirect(`https://${req.get("host")}${req.originalUrl}`) + app.use((req, res, next) => { + if (req.protocol == "http") + res.redirect(`https://${req.get("host")}${req.originalUrl}`) else next() }) } -app.get("/server",(req,res) => { - res.send(JSON.stringify({ - ...config, - version:pkg.version, - files:Object.keys(files.files).length - })) +app.get("/server", (req, res) => { + res.send( + JSON.stringify({ + ...config, + version: pkg.version, + files: Object.keys(files.files).length, + }) + ) }) // funcs // init data -if (!fs.existsSync(__dirname+"/../.data/")) fs.mkdirSync(__dirname+"/../.data/") +if (!fs.existsSync(__dirname + "/../.data/")) + fs.mkdirSync(__dirname + "/../.data/") // discord -let client = new Client({intents:[ - IntentsBitField.Flags.GuildMessages, - IntentsBitField.Flags.MessageContent -],rest:{timeout:config.requestTimeout}}) +let client = new Client({ + intents: [ + IntentsBitField.Flags.GuildMessages, + IntentsBitField.Flags.MessageContent, + ], + rest: { timeout: config.requestTimeout }, +}) -let files = new Files(client,config) +let files = new Files(client, config) let apiRouter = new APIRouter(files) apiRouter.loadAPIMethods().then(() => { @@ -66,88 +73,118 @@ apiRouter.loadAPIMethods().then(() => { // index, clone -app.get("/", function(req,res) { - res.sendFile(process.cwd()+"/pages/index.html") +app.get("/", function (req, res) { + res.sendFile(process.cwd() + "/pages/index.html") }) // serve download page -app.get("/download/:fileId", getAccount, (req,res) => { - +app.get("/download/:fileId", getAccount, (req, res) => { let acc = res.locals.acc as Accounts.Account if (files.getFilePointer(req.params.fileId)) { let file = files.getFilePointer(req.params.fileId) if (file.visibility == "private" && acc?.id != file.owner) { - ServeError(res,403,"you do not own this file") + ServeError(res, 403, "you do not own this file") return } - fs.readFile(process.cwd()+"/pages/download.html",(err,buf) => { - let fileOwner = file.owner ? Accounts.getFromId(file.owner) : undefined; - if (err) {res.sendStatus(500);console.log(err);return} + fs.readFile(process.cwd() + "/pages/download.html", (err, buf) => { + let fileOwner = file.owner + ? Accounts.getFromId(file.owner) + : undefined + if (err) { + res.sendStatus(500) + console.log(err) + return + } res.send( - buf.toString() - .replace(/\$FileId/g,req.params.fileId) - .replace(/\$Version/g,pkg.version) - .replace(/\$FileSize/g,file.sizeInBytes ? bytes(file.sizeInBytes) : "[File size unknown]") - .replace(/\$FileName/g, - file.filename - .replace(/\&/g,"&") - .replace(/\/g,">") - ) - .replace(/\<\!\-\-metaTags\-\-\>/g, - ( - file.mime.startsWith("image/") - ? `` - : ( - file.mime.startsWith("video/") - ? ( - ` - + buf + .toString() + .replaceAll("$FileId", req.params.fileId) + .replaceAll("$Version", pkg.version) + .replaceAll( + "$FileSize", + file.sizeInBytes + ? bytes(file.sizeInBytes) + : "[File size unknown]" + ) + .replaceAll( + "$FileName", + file.filename + .replaceAll("&", "&") + .replaceAll("<", "<") + .replaceAll(">", ">") + ) + .replace( + "", + (file.mime.startsWith("image/") + ? `` + : file.mime.startsWith("video/") + ? ` + - ` - // quick lazy fix as a fallback - // maybe i'll improve this later, but probably not. - + ((file.sizeInBytes||0) >= 26214400 ? ` + ` + + // quick lazy fix as a fallback + // maybe i'll improve this later, but probably not. + ((file.sizeInBytes || 0) >= 26214400 + ? ` - ` : "") - ) - : "" - ) + ` + : "") + : "") + + (fileOwner?.embed?.largeImage && + file.visibility != "anonymous" && + file.mime.startsWith("image/") + ? `` + : "") + + `\n` ) - + ( - fileOwner?.embed?.largeImage && file.visibility!="anonymous" && file.mime.startsWith("image/") - ? `` - : "" - ) - + `\n` - ) - .replace(/\<\!\-\-preview\-\-\>/g, - file.mime.startsWith("image/") - ? `
` - : ( - file.mime.startsWith("video/") - ? `
` - : ( - file.mime.startsWith("audio/") + .replace( + "", + file.mime.startsWith("image/") + ? `
` + : file.mime.startsWith("video/") + ? `
` + : file.mime.startsWith("audio/") ? `
` : "" - ) ) - ) - .replace(/\$Uploader/g,!file.owner||file.visibility=="anonymous" ? "Anonymous" : `@${fileOwner?.username || "Deleted User"}`) + .replaceAll( + "$Uploader", + !file.owner || file.visibility == "anonymous" + ? "Anonymous" + : `@${fileOwner?.username || "Deleted User"}` + ) ) }) } else { - ServeError(res,404,"file not found") + ServeError(res, 404, "file not found") } }) - /* routes should be in this order: @@ -159,7 +196,7 @@ app.get("/download/:fileId", getAccount, (req,res) => { // listen on 3000 or MONOFILE_PORT -app.listen(process.env.MONOFILE_PORT || 3000,function() { +app.listen(process.env.MONOFILE_PORT || 3000, function () { console.log("Web OK!") }) diff --git a/src/server/lib/errors.ts b/src/server/lib/errors.ts index 84c1cc5..cb53535 100644 --- a/src/server/lib/errors.ts +++ b/src/server/lib/errors.ts @@ -31,8 +31,8 @@ export default async function ServeError( res.header("x-backup-status-message", reason) // glitch default nginx configuration res.send( errorPage - .replace(/\$code/g,code.toString()) - .replace(/\$text/g,reason) + .replaceAll("$code",code.toString()) + .replaceAll("$text",reason) ) } /** @@ -45,4 +45,4 @@ export function Redirect(res:Response,url:string) { res.status(302) res.header("Location",url) res.send() -} \ No newline at end of file +} diff --git a/src/server/lib/mail.ts b/src/server/lib/mail.ts index 3ee1b38..7d7bf29 100644 --- a/src/server/lib/mail.ts +++ b/src/server/lib/mail.ts @@ -1,21 +1,16 @@ -import { createTransport } from "nodemailer"; +import { createTransport } from "nodemailer" // required i guess require("dotenv").config() -let -mailConfig = - require( process.cwd() + "/config.json" ).mail, -transport = - createTransport( - { - ...mailConfig.transport, - auth: { - user: process.env.MAIL_USER, - pass: process.env.MAIL_PASS - } - } - ) +let mailConfig = require(process.cwd() + "/config.json").mail, + transport = createTransport({ + ...mailConfig.transport, + auth: { + user: process.env.MAIL_USER, + pass: process.env.MAIL_PASS, + }, + }) // lazy but @@ -30,11 +25,15 @@ export function sendMail(to: string, subject: string, content: string) { return transport.sendMail({ to, subject, - "from": mailConfig.send.from, - "html": `monofile accounts
Gain control of your uploads.

${ - content - .replace(/\/g, `@`) - .replace(/\/g,``) - }

If you do not believe that you are the intended recipient of this email, please disregard this message.` + from: mailConfig.send.from, + html: `monofile accounts
Gain control of your uploads.

${content + .replaceAll( + "", + `@` + ) + .replaceAll( + "", + `` + )}

If you do not believe that you are the intended recipient of this email, please disregard this message.`, }) -} \ No newline at end of file +} diff --git a/tsconfig.json b/tsconfig.json index 1867d13..e7dbb82 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,7 @@ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ /* Language and Environment */ - "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "target": "es2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */