move upload to separate function

This commit is contained in:
May 2022-12-26 20:28:33 -08:00
parent a0dfa7efb5
commit fc5d2114ed

View file

@ -43,26 +43,23 @@ let client = new Client({intents:[
let uploadChannel:Discord.TextBasedChannel let uploadChannel:Discord.TextBasedChannel
app.get("/", function(req,res) { interface FileUploadSettings {
fs.readFile(__dirname+"/../pages/upload.html",(err,buf) => { name?: string,
if (err) {res.sendStatus(500);console.log(err);return} mime: string,
res.send(buf.toString().replace("$MaxInstanceFilesize",`${(config.maxDiscordFileSize*config.maxDiscordFiles)/1048576}MB`)) }
})
})
app.post("/upload",multerSetup.single('file'),async (req,res) => { let uploadFile = (settings:FileUploadSettings,fBuffer:Buffer) => {
if (req.file) { return new Promise<string>(async (resolve,reject) => {
if (!req.file.originalname || !req.file.mimetype) {res.status(400); res.send("[err] missing name/mime");return} if (!settings.name || !settings.mime) {reject({status:400,message:"missing name/mime"});return}
let uploadId = Math.random().toString().slice(2) let uploadId = Math.random().toString().slice(2)
if (files[uploadId]) {res.status(500); res.send("[err] please try again"); return} if (files[uploadId]) {reject({status:500,message:"please try again"});return}
if (req.file.originalname.length > 64) {res.status(400); res.send("[err] name too long"); return} if (settings.name.length > 128) {reject({status:400,message:"name too long"}); return}
if (req.file.mimetype.length > 64) {res.status(400); res.send("[err] mime too long"); return} if (settings.name.length > 128) {reject({status:400,message:"mime too long"}); return}
// get buffer // get buffer
let fBuffer = req.file.buffer if (fBuffer.byteLength >= (config.maxDiscordFileSize*config.maxDiscordFiles)) {reject({status:400,message:"file too large"}); return}
if (fBuffer.byteLength >= (config.maxDiscordFileSize*config.maxDiscordFiles)) {res.status(400); res.send("[err] file too large"); return}
// generate buffers to upload // generate buffers to upload
let toUpload = [] let toUpload = []
@ -84,29 +81,47 @@ app.post("/upload",multerSetup.single('file'),async (req,res) => {
if (ms) { if (ms) {
msgIds.push(ms.id) msgIds.push(ms.id)
} else { } else {
res.status(500); res.send("[err] please try again"); return reject({status:500,message:"please try again"}); return
} }
} }
// save // save
files[uploadId] = { files[uploadId] = {
filename:req.file.originalname, filename:settings.name,
messageids:msgIds, messageids:msgIds,
mime:req.file.mimetype mime:settings.mime
} }
fs.writeFile(__dirname+"/../.data/files.json",JSON.stringify(files),(err) => { fs.writeFile(__dirname+"/../.data/files.json",JSON.stringify(files),(err) => {
if (err) {res.status(500); res.send("[err] please try again"); delete files[uploadId];return} if (err) {reject({status:500,message:"please try again"}); delete files[uploadId];return}
res.send(uploadId) resolve(uploadId)
}) })
})
}
app.get("/", function(req,res) {
fs.readFile(__dirname+"/../pages/upload.html",(err,buf) => {
if (err) {res.sendStatus(500);console.log(err);return}
res.send(buf.toString().replace("$MaxInstanceFilesize",`${(config.maxDiscordFileSize*config.maxDiscordFiles)/1048576}MB`))
})
})
app.post("/upload",multerSetup.single('file'),async (req,res) => {
if (req.file) {
uploadFile({name:req.file.originalname,mime:req.file.mimetype},req.file.buffer)
.then((uID) => res.send(uID))
.catch((stat) => {res.status(stat.status);res.send(stat.message)})
} else { } else {
res.status(400) res.status(400)
res.send("[err] bad request") res.send("[err] bad request")
} }
}) })
app.post("/clone",(req,res) => {
console.log(req.body)
})
app.get("/download/:fileId",(req,res) => { app.get("/download/:fileId",(req,res) => {
if (files[req.params.fileId]) { if (files[req.params.fileId]) {
let file = files[req.params.fileId] let file = files[req.params.fileId]