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