From e1b9a14fbc6b8d734504c7dbd1c79aad8f1d05d4 Mon Sep 17 00:00:00 2001 From: stringsplit <77242831+nbitzz@users.noreply.github.com> Date: Sun, 24 Sep 2023 11:58:46 -0700 Subject: [PATCH] i will not be finishing this what the hell was i smoking --- .vscode/tasks.json | 9 ++ README.md | 2 +- src/server/lib/files.ts | 152 +++++++++++++++------------- src/svelte/elem/UploadWindow.svelte | 4 +- 4 files changed, 93 insertions(+), 74 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index af8dfb2..04b6ae3 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -10,6 +10,15 @@ }, "label": "Build (Bot Server)" }, + { + "type": "shell", + "command":"sass src/style:out/style\nrollup -c", + "group": { + "kind": "build", + "isDefault": true + }, + "label": "Build (Frontend Only)" + }, { "type": "shell", "command":"tsc\nsass src/style:out/style\nrollup -c\nnode ./out/server/index.js\ndel ./out/* -Recurse", diff --git a/README.md b/README.md index cc8e371..afdd2d7 100644 --- a/README.md +++ b/README.md @@ -72,4 +72,4 @@ Although we believe monofile is not against Discord's developer terms of service Code written by monofile's contributors is currently licensed under [Unlicense](https://github.com/nbitzz/monofile/blob/main/LICENSE). -Icons under `/assets/icons` were created by Microsoft, and as such are licensed under [different terms](https://github.com/nbitzz/monofile/blob/1.3.0/assets/icons/README.md) (MIT). \ No newline at end of file +Icons under `/assets/icons` were created by Microsoft, and as such are licensed under [different terms](https://github.com/nbitzz/monofile/blob/main/assets/icons/README.md) (MIT). \ No newline at end of file diff --git a/src/server/lib/files.ts b/src/server/lib/files.ts index 34d5b67..8c6609b 100644 --- a/src/server/lib/files.ts +++ b/src/server/lib/files.ts @@ -25,7 +25,8 @@ export interface FileUploadSettings { name?: string, mime: string, uploadId?: string, - owner?:string + owner?:string, + sizeInBytes: number } export interface Configuration { @@ -92,8 +93,13 @@ export default class Files { } - uploadFile(settings:FileUploadSettings,fBuffer:Buffer):Promise { + uploadFileStream(_settings:FileUploadSettings,fStream:Readable):Promise { + let settings = JSON.parse(JSON.stringify(_settings)) // copy + return new Promise(async (resolve,reject) => { + + // guards + if (!this.uploadChannel) { reject({status:503,message:"server is not ready - please try again later"}) return @@ -141,102 +147,104 @@ export default class Files { let ogf = this.files[uploadId] this.files[uploadId] = { - filename:settings.name, - messageids:[], - mime:settings.mime, - sizeInBytes:0, + filename:settings.name, + messageids:[], + mime:settings.mime, + sizeInBytes:0, - owner:settings.owner, - visibility: settings.owner ? "private" : "public", - reserved: true, + owner:settings.owner, + visibility: settings.owner ? "private" : "public", + reserved: true, - chunkSize: this.config.maxDiscordFileSize - } + chunkSize: this.config.maxDiscordFileSize + } // save + + if (settings.sizeInBytes >= (this.config.maxDiscordFileSize*this.config.maxDiscordFiles)) { + reject({status:400,message:"file too large"}); + return + } if (settings.owner) { await files.index(settings.owner,uploadId) } - - // get buffer - if (fBuffer.byteLength >= (this.config.maxDiscordFileSize*this.config.maxDiscordFiles)) { - reject({status:400,message:"file too large"}); - return - } + + let msgIds: string[] = [] + let totalFileSize = 0 + let chunkData: {size: number, data: Buffer[]} = {size: 0, data: []} - // generate buffers to upload - let toUpload = [] - for (let i = 0; i < Math.ceil(fBuffer.byteLength/this.config.maxDiscordFileSize); i++) { - toUpload.push( - fBuffer.subarray( - i*this.config.maxDiscordFileSize, - Math.min( - fBuffer.byteLength, - (i+1)*this.config.maxDiscordFileSize + let uploadChunk = async (fBuffer: Buffer) => { + if (!this.uploadChannel) return + // generate buffers to upload + let toUpload = [] + for (let i = 0; i < Math.ceil(fBuffer.byteLength/this.config.maxDiscordFileSize); i++) { + toUpload.push( + fBuffer.subarray( + i*this.config.maxDiscordFileSize, + Math.min( + fBuffer.byteLength, + (i+1)*this.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++) { + } + if (toUpload.length > this.config.maxDiscordFiles) return + + // begin uploading + let uploadTmplt:Discord.AttachmentBuilder[] = toUpload.map((e) => { + return new Discord.AttachmentBuilder(e) + .setName(Math.random().toString().slice(2)) + }) + let ms = await this.uploadChannel.send({ - files:uploadGroups[i] + files:uploadTmplt }).catch((e) => {console.error(e)}) if (ms) { - msgIds.push(ms.id) + return ms.id } else { if (!ogf) delete this.files[uploadId] else this.files[uploadId] = ogf - reject({status:500,message:"please try again"}); return + return } } - // this code deletes the files from discord, btw - // if need be, replace with job queue system + let finish = async () => { + // this code deletes the files from discord, btw + // if need be, replace with job queue system - if (ogf&&this.uploadChannel) { - for (let x of ogf.messageids) { - this.uploadChannel.messages.delete(x).catch(err => console.error(err)) + if (ogf&&this.uploadChannel) { + for (let x of ogf.messageids) { + this.uploadChannel.messages.delete(x).catch(err => console.error(err)) + } } + + resolve(await this.writeFile( + uploadId, + { + filename: settings.name, + messageids: msgIds, + mime: settings.mime, + sizeInBytes: settings.sizeInBytes, + + owner: settings.owner, + visibility: ogf ? ogf.visibility + : ( + settings.owner + ? Accounts.getFromId(settings.owner)?.defaultFileVisibility + : undefined + ), + // so that json.stringify doesnt include tag:undefined + ...((ogf||{}).tag ? {tag:ogf.tag} : {}), + + chunkSize: this.config.maxDiscordFileSize + } + )) } - resolve(await this.writeFile( - uploadId, - { - filename:settings.name, - messageids:msgIds, - mime:settings.mime, - sizeInBytes:fBuffer.byteLength, - - owner:settings.owner, - visibility: ogf ? ogf.visibility - : ( - settings.owner - ? Accounts.getFromId(settings.owner)?.defaultFileVisibility - : undefined - ), - // so that json.stringify doesnt include tag:undefined - ...((ogf||{}).tag ? {tag:ogf.tag} : {}), - - chunkSize: this.config.maxDiscordFileSize - } - )) - + }) } diff --git a/src/svelte/elem/UploadWindow.svelte b/src/svelte/elem/UploadWindow.svelte index 0b2b136..f023ccf 100644 --- a/src/svelte/elem/UploadWindow.svelte +++ b/src/svelte/elem/UploadWindow.svelte @@ -165,7 +165,9 @@ {:else}
{#if !upload[1].uploadStatus.fileId} -

{upload[1].uploadStatus.error ?? "Uploading..."}

+
+

{upload[1].uploadStatus.error ?? `${upload[1].uploadStatus.percentage}%`}

+
{/if} {#if upload[1].uploadStatus.fileId}