/* i really should split this up into different modules */ import bodyParser from "body-parser" import multer, {memoryStorage} from "multer" import Discord, { IntentsBitField, Client } from "discord.js" import express from "express" import fs from "fs" import axios, { AxiosResponse } from "axios" import Files from "./lib/files" require('dotenv').config() let pkg = require(`${process.cwd()}/package.json`) let app = express() const multerSetup = multer({storage:memoryStorage()}) let config = require(`${process.cwd()}/config.json`) app.use("/static",express.static("assets")) app.use(bodyParser.text({limit:(config.maxDiscordFileSize*config.maxDiscordFiles)+1048576,type:["application/json","text/plain"]})) let files:{[key:string]:{filename:string,mime:string,messageids:string[]}} = {} // funcs function ThrowError(response:express.Response,code:number,errorMessage:string) { fs.readFile(__dirname+"/../pages/error.html",(err,buf) => { if (err) {response.sendStatus(500);console.log(err);return} response.status(code) response.send(buf.toString().replace(/\$ErrorCode/g,code.toString()).replace(/\$ErrorMessage/g,errorMessage).replace(/\$Version/g,pkg.version)) }) } // init data if (!fs.existsSync(__dirname+"/../.data/")) fs.mkdirSync(__dirname+"/../.data/") fs.readFile(__dirname+"/../.data/files.json",(err,buf) => { if (err) {console.log(err);return} files = JSON.parse(buf.toString() || "{}") }) // discord let client = new Client({intents:[ IntentsBitField.Flags.GuildMessages, IntentsBitField.Flags.MessageContent ],rest:{timeout:config.requestTimeout}}) let uploadChannel:Discord.TextBasedChannel app.get("/", function(req,res) { fs.readFile(__dirname+"/../pages/base.html",(err,buf) => { if (err) {res.sendStatus(500);console.log(err);return} res.send( buf.toString() .replace("$MaxInstanceFilesize",`${(config.maxDiscordFileSize*config.maxDiscordFiles)/1048576}MB`) .replace(/\$Version/g,pkg.version) .replace(/\$Handler/g,"upload_file") .replace(/\$UploadButtonText/g,"Upload file") .replace(/\$otherPath/g,"/clone") .replace(/\$otherText/g,"clone from url...") .replace(/\$FileNum/g,Object.keys(files).length.toString()) ) }) }) app.get("/clone", function(req,res) { fs.readFile(__dirname+"/../pages/base.html",(err,buf) => { if (err) {res.sendStatus(500);console.log(err);return} res.send( buf.toString() .replace("$MaxInstanceFilesize",`${(config.maxDiscordFileSize*config.maxDiscordFiles)/1048576}MB`) .replace(/\$Version/g,pkg.version) .replace(/\$Handler/g,"clone_file") .replace(/\$UploadButtonText/g,"Input a URL") .replace(/\$otherPath/g,"/") .replace(/\$otherText/g,"upload file...") .replace(/\$FileNum/g,Object.keys(files).length.toString()) ) }) }) app.post("/upload",multerSetup.single('file'),async (req,res) => { if (req.file) { try { uploadFile({name:req.file.originalname,mime:req.file.mimetype,uploadId:req.header("monofile-upload-id")},req.file.buffer) .then((uID) => res.send(uID)) .catch((stat) => {res.status(stat.status);res.send(`[err] ${stat.message}`)}) } catch { res.status(400) res.send("[err] bad request") } } else { res.status(400) res.send("[err] bad request") } }) app.post("/clone",(req,res) => { try { let j = JSON.parse(req.body) if (!j.url) { res.status(400) res.send("[err] invalid url") } axios.get(j.url,{responseType:"arraybuffer"}).then((data:AxiosResponse) => { uploadFile({name:req.body.split("/")[req.body.split("/").length-1] || "generic",mime:data.headers["content-type"],uploadId:j.uploadId},Buffer.from(data.data)) .then((uID) => res.send(uID)) .catch((stat) => {res.status(stat.status);res.send(`[err] ${stat.message}`)}) }).catch((err) => { res.status(400) res.send(`[err] failed to fetch data`) }) } catch { res.status(500) res.send("[err] an error occured") } }) app.get("/download/:fileId",(req,res) => { if (files[req.params.fileId]) { let file = files[req.params.fileId] fs.readFile(__dirname+"/../pages/download.html",(err,buf) => { if (err) {res.sendStatus(500);console.log(err);return} res.send(buf.toString().replace(/\$FileName/g,file.filename).replace(/\$FileId/g,req.params.fileId).replace(/\$Version/g,pkg.version)) }) } else { ThrowError(res,404,"File not found.") } }) app.get("/file/:fileId",async (req,res) => { if (files[req.params.fileId]) { let file = files[req.params.fileId] let bufToCombine = [] for (let i = 0; i < file.messageids.length; i++) { let msg = await uploadChannel.messages.fetch(file.messageids[i]).catch(() => {return null}) if (msg?.attachments) { let attach = Array.from(msg.attachments.values()) for (let i = 0; i < attach.length; i++) { let d = await axios.get(attach[i].url,{responseType:"arraybuffer"}).catch((e:Error) => {console.error(e)}) if (d) { bufToCombine.push(d.data) } else { res.sendStatus(500);return } } } } let nb:Buffer|null = Buffer.concat(bufToCombine) res.setHeader('Content-Type',file.mime) res.send(nb) nb = null } else { res.sendStatus(404) } }) app.get("/server",(req,res) => { res.send(JSON.stringify({...config,version:pkg.version})) }) app.get("*",(req,res) => { ThrowError(res,404,"Page not found.") }) client.on("ready",() => { console.log("Discord OK!") client.guilds.fetch(config.targetGuild).then((g) => { g.channels.fetch(config.targetChannel).then((a) => { if (a?.isTextBased()) { uploadChannel = a } }) }) }) app.listen(3000,function() { console.log("Web OK!") }) client.login(process.env.TOKEN)