mirror of
https://github.com/mollersuite/monofile.git
synced 2024-11-21 21:36:26 -08:00
wip
This commit is contained in:
parent
7cab14dd8d
commit
fbdc5f0edd
|
@ -19,7 +19,7 @@ TOKEN=KILL-YOURSELF.NOW
|
|||
- [X] 1.1.4 serve /assets as static files & make /server endpoint
|
||||
- [X] 1.2.0 add file parameters section + custom ids
|
||||
- [X] 1.2.1 add file counter to main page
|
||||
- [ ] 1.2.2 clean up this shitty code
|
||||
- [X] 1.2.2 clean up this shitty code
|
||||
- [ ] 1.2.3 add id locks, allowing you to set a key for a file that allows you to overwrite the file in the future
|
||||
- [ ] 1.2.4 prevent cloning of local/private ip addresses
|
||||
- [ ] 1.3.0 add simple moderation tools
|
||||
|
|
51
src/index.ts
51
src/index.ts
|
@ -21,7 +21,7 @@ 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[]}} = {}
|
||||
//let files:{[key:string]:{filename:string,mime:string,messageids:string[]}} = {}
|
||||
|
||||
// funcs
|
||||
|
||||
|
@ -37,10 +37,7 @@ function ThrowError(response:express.Response,code:number,errorMessage:string) {
|
|||
|
||||
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
|
||||
|
||||
|
@ -49,7 +46,7 @@ let client = new Client({intents:[
|
|||
IntentsBitField.Flags.MessageContent
|
||||
],rest:{timeout:config.requestTimeout}})
|
||||
|
||||
let uploadChannel:Discord.TextBasedChannel
|
||||
let files = new Files(client,config)
|
||||
|
||||
app.get("/", function(req,res) {
|
||||
fs.readFile(__dirname+"/../pages/base.html",(err,buf) => {
|
||||
|
@ -86,7 +83,7 @@ app.get("/clone", function(req,res) {
|
|||
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)
|
||||
files.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 {
|
||||
|
@ -134,35 +131,7 @@ app.get("/download/:fileId",(req,res) => {
|
|||
})
|
||||
|
||||
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) => {
|
||||
|
@ -173,18 +142,6 @@ 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!")
|
||||
})
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import axios from "axios";
|
||||
import Discord, { Client, TextBasedChannel } from "discord.js";
|
||||
import { readFile, writeFile } from "fs";
|
||||
|
||||
import { Readable } from "node:stream"
|
||||
|
||||
export let id_check_regex = /[A-Za-z0-9_\-\.]+/
|
||||
|
||||
|
@ -40,8 +41,27 @@ export default class Files {
|
|||
uploadChannel?: TextBasedChannel
|
||||
|
||||
constructor(client: Client, config: Configuration) {
|
||||
|
||||
this.config = config;
|
||||
this.client = client;
|
||||
|
||||
client.on("ready",() => {
|
||||
console.log("Discord OK!")
|
||||
|
||||
client.guilds.fetch(config.targetGuild).then((g) => {
|
||||
g.channels.fetch(config.targetChannel).then((a) => {
|
||||
if (a?.isTextBased()) {
|
||||
this.uploadChannel = a
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
readFile(__dirname+"/../.data/files.json",(err,buf) => {
|
||||
if (err) {console.log(err);return}
|
||||
this.files = JSON.parse(buf.toString() || "{}")
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
uploadFile(settings:FileUploadSettings,fBuffer:Buffer):Promise<string|StatusCodeError> {
|
||||
|
@ -159,12 +179,59 @@ export default class Files {
|
|||
|
||||
// todo: move read code here
|
||||
|
||||
readFile(uploadId: string):Promise<Buffer|StatusCodeError> {
|
||||
readFile(uploadId: string):Promise<{dataStream:Readable,contentType:string}> {
|
||||
return new Promise(async (resolve,reject) => {
|
||||
if (!this.uploadChannel) {
|
||||
reject({status:503,message:"server is not ready - please try again later"})
|
||||
return
|
||||
}
|
||||
|
||||
if (this.files[uploadId]) {
|
||||
let file = this.files[uploadId]
|
||||
|
||||
let dataStream = new Readable()
|
||||
|
||||
resolve({
|
||||
contentType: file.mime,
|
||||
dataStream: dataStream
|
||||
})
|
||||
|
||||
for (let i = 0; i < file.messageids.length; i++) {
|
||||
let msg = await this.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) {
|
||||
dataStream.push(d.data)
|
||||
} else {
|
||||
reject({status:500,message:"internal server error"})
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
reject({status:404,message:"not found"})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
unlink():Promise<void> {
|
||||
unlink(uploadId:string):Promise<void> {
|
||||
return new Promise((resolve,reject) => {
|
||||
let tmp = this.files[uploadId];
|
||||
delete this.files[uploadId];
|
||||
writeFile(process.cwd()+"/.data/files.json",JSON.stringify(this.files),(err) => {
|
||||
if (err) {
|
||||
this.files[uploadId] = tmp
|
||||
reject()
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue