start work on 1.2.2

This commit is contained in:
stringsplit 2023-01-20 21:23:45 +00:00
parent da42b6566e
commit 79da014ec5
5 changed files with 91 additions and 64 deletions

6
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "monofile", "name": "monofile",
"version": "1.1.4", "version": "1.2.1",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "monofile", "name": "monofile",
"version": "1.1.4", "version": "1.2.1",
"license": "Unlicense", "license": "Unlicense",
"dependencies": { "dependencies": {
"@types/body-parser": "^1.19.2", "@types/body-parser": "^1.19.2",
@ -21,7 +21,7 @@
"typescript": "^4.8.3" "typescript": "^4.8.3"
}, },
"engines": { "engines": {
"node": ">=v16" "node": ">=v16.9.0"
} }
}, },
"node_modules/@discordjs/builders": { "node_modules/@discordjs/builders": {

View file

@ -1,6 +1,6 @@
{ {
"name": "monofile", "name": "monofile",
"version": "1.2.1", "version": "1.2.2",
"description": "Discord-based file sharing", "description": "Discord-based file sharing",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View file

@ -9,7 +9,9 @@ import express from "express"
import fs from "fs" import fs from "fs"
import axios, { AxiosResponse } from "axios" import axios, { AxiosResponse } from "axios"
import Files from "./lib/files"
require('dotenv').config() require('dotenv').config()
let pkg = require(`${process.cwd()}/package.json`) let pkg = require(`${process.cwd()}/package.json`)
let app = express() let app = express()
@ -49,66 +51,6 @@ let client = new Client({intents:[
let uploadChannel:Discord.TextBasedChannel let uploadChannel:Discord.TextBasedChannel
interface FileUploadSettings {
name?: string,
mime: string,
uploadId?: string
}
let uploadFile = (settings:FileUploadSettings,fBuffer:Buffer) => {
return new Promise<string>(async (resolve,reject) => {
if (!settings.name || !settings.mime) {reject({status:400,message:"missing name/mime"});return}
let uploadId = (settings.uploadId || Math.random().toString().slice(2)).toString();
if ((uploadId.match(/[A-Za-z0-9_\-\.]+/)||[])[0] != uploadId || uploadId.length > 30) {reject({status:400,message:"invalid id"});return}
if (files[uploadId]) {reject({status:400,message:"a file with this id already exists"});return}
if (settings.name.length > 128) {reject({status:400,message:"name too long"}); return}
if (settings.name.length > 128) {reject({status:400,message:"mime too long"}); return}
// get buffer
if (fBuffer.byteLength >= (config.maxDiscordFileSize*config.maxDiscordFiles)) {reject({status:400,message:"file too large"}); return}
// generate buffers to upload
let toUpload = []
for (let i = 0; i < Math.ceil(fBuffer.byteLength/config.maxDiscordFileSize); i++) {
toUpload.push(fBuffer.subarray(i*config.maxDiscordFileSize,Math.min(fBuffer.byteLength,(i+1)*config.maxDiscordFileSize)))
}
// begin uploading
let uploadTmplt:Discord.AttachmentBuilder[] = toUpload.map((e) => {return new Discord.AttachmentBuilder(e).setName(Math.random().toString().slice(2))})
let uploadGroups = []
for (let i = 0; i < Math.ceil(uploadTmplt.length/10); i++) {
uploadGroups.push(uploadTmplt.slice(i*10,((i+1)*10)))
}
let msgIds = []
for (let i = 0; i < uploadGroups.length; i++) {
let ms = await uploadChannel.send({files:uploadGroups[i]}).catch((e) => {console.error(e)})
if (ms) {
msgIds.push(ms.id)
} else {
reject({status:500,message:"please try again"}); return
}
}
// save
files[uploadId] = {
filename:settings.name,
messageids:msgIds,
mime:settings.mime
}
fs.writeFile(__dirname+"/../.data/files.json",JSON.stringify(files),(err) => {
if (err) {reject({status:500,message:"please try again"}); delete files[uploadId];return}
resolve(uploadId)
})
})
}
app.get("/", function(req,res) { app.get("/", function(req,res) {
fs.readFile(__dirname+"/../pages/base.html",(err,buf) => { fs.readFile(__dirname+"/../pages/base.html",(err,buf) => {
if (err) {res.sendStatus(500);console.log(err);return} if (err) {res.sendStatus(500);console.log(err);return}

85
src/lib/files.ts Normal file
View file

@ -0,0 +1,85 @@
import { Client } from "discord.js";
export interface FileUploadSettings {
name?: string,
mime: string,
uploadId?: string
}
export interface Configuration {
maxDiscordFiles: number,
maxDiscordFileSize: number,
targetGuild: string,
targetChannel: string,
requestTimeout: number
}
export default class Files {
config: Configuration
client: Client
constructor(client: Client, config: Configuration) {
this.config = config;
this.client = client;
}
uploadFile(settings:FileUploadSettings,fBuffer:Buffer) {
return new Promise<string>(async (resolve,reject) => {
if (!settings.name || !settings.mime) {reject({status:400,message:"missing name/mime"});return}
let uploadId = (settings.uploadId || Math.random().toString().slice(2)).toString();
if ((uploadId.match(/[A-Za-z0-9_\-\.]+/)||[])[0] != uploadId || uploadId.length > 30) {reject({status:400,message:"invalid id"});return}
if (files[uploadId]) {reject({status:400,message:"a file with this id already exists"});return}
if (settings.name.length > 128) {reject({status:400,message:"name too long"}); return}
if (settings.name.length > 128) {reject({status:400,message:"mime too long"}); return}
// get buffer
if (fBuffer.byteLength >= (this.config.maxDiscordFileSize*this.config.maxDiscordFiles)) {
reject({status:400,message:"file too large"});
return
}
// generate buffers to upload
let toUpload = []
for (let i = 0; i < Math.ceil(fBuffer.byteLength/config.maxDiscordFileSize); i++) {
toUpload.push(fBuffer.subarray(i*config.maxDiscordFileSize,Math.min(fBuffer.byteLength,(i+1)*config.maxDiscordFileSize)))
}
// begin uploading
let uploadTmplt:Discord.AttachmentBuilder[] = toUpload.map((e) => {return new Discord.AttachmentBuilder(e).setName(Math.random().toString().slice(2))})
let uploadGroups = []
for (let i = 0; i < Math.ceil(uploadTmplt.length/10); i++) {
uploadGroups.push(uploadTmplt.slice(i*10,((i+1)*10)))
}
let msgIds = []
for (let i = 0; i < uploadGroups.length; i++) {
let ms = await uploadChannel.send({files:uploadGroups[i]}).catch((e) => {console.error(e)})
if (ms) {
msgIds.push(ms.id)
} else {
reject({status:500,message:"please try again"}); return
}
}
// save
files[uploadId] = {
filename:settings.name,
messageids:msgIds,
mime:settings.mime
}
fs.writeFile(__dirname+"/../.data/files.json",JSON.stringify(files),(err) => {
if (err) {reject({status:500,message:"please try again"}); delete files[uploadId];return}
resolve(uploadId)
})
})
}
}