From 8412aeb1af6c5796aa51423d0abbfd9c1d0b16d5 Mon Sep 17 00:00:00 2001 From: Elijah Date: Fri, 8 Dec 2023 21:27:48 -0500 Subject: [PATCH] Add file queue --- package.json | 1 + src/File.ts | 4 ++++ src/VM.ts | 27 ++++++++++++++++++++++++--- src/index.ts | 7 +++++-- 4 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 src/File.ts diff --git a/package.json b/package.json index 2fea0fa..3d2b8f5 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "discord.js": "^14.14.1", "fastify": "^4.24.3", "md5": "^2.3.0", + "mnemonist": "^0.39.6", "msgpack-lite": "^0.1.26" } } diff --git a/src/File.ts b/src/File.ts new file mode 100644 index 0000000..19fe634 --- /dev/null +++ b/src/File.ts @@ -0,0 +1,4 @@ +export default interface File { + Filename : string; + FileData : Buffer; +} \ No newline at end of file diff --git a/src/VM.ts b/src/VM.ts index 74b46b0..a4039a4 100644 --- a/src/VM.ts +++ b/src/VM.ts @@ -3,11 +3,15 @@ import * as protocol from './Protocol.js' import * as msgpack from 'msgpack-lite'; import {Mutex} from 'async-mutex'; import log from './log.js'; +import Queue from 'mnemonist/queue.js'; +import File from "./File.js"; export default class VM { #socketpath : string; #socket : Socket; #writeLock : Mutex = new Mutex(); + #fileQueue : Queue = new Queue(); + #isFileQueueRunning : boolean = false; connected : boolean = false; constructor(socketpath : string) { this.#socketpath = socketpath; @@ -19,12 +23,29 @@ export default class VM { }); } - UploadFile(filename : string, data : Buffer) : Promise { + UploadFile(file : File) { + this.#fileQueue.enqueue(file); + if (!this.#isFileQueueRunning) + this.fileQueueLoop(); + } + + fileQueueLoop() { + return new Promise(async (res, rej) => { + this.#isFileQueueRunning = true; + while (this.#fileQueue.size > 0) { + var file = this.#fileQueue.dequeue(); + await this.PushFile(file!); + } + this.#isFileQueueRunning = false; + }) + } + + PushFile(file : File) : Promise { return new Promise(async (res, rej) => { const msg : protocol.ProtocolMessage = { Operation: protocol.ProtocolOperation.UploadFile, - Filename: filename, - FileData: data + Filename: file.Filename, + FileData: file.FileData }; var payload = msgpack.encode(msg); await this.#sendMessage(payload); diff --git a/src/index.ts b/src/index.ts index f1f8a0d..8ae9618 100644 --- a/src/index.ts +++ b/src/index.ts @@ -95,7 +95,10 @@ app.put("/:vm/:filename", async (req, res) => { log("INFO", `${vm}: ${req.ip} tried to upload "${filename}" with blocked MD5 ${hash}`); return { success: false, result: "That file is not allowed" }; } - await VMs.get(vm)!.UploadFile(filename, filedata); + VMs.get(vm)!.UploadFile({ + Filename: filename, + FileData: filedata + }); log("INFO", `${vm}: ${req.ip} uploaded "${filename}" with MD5 ${hash}`); if (config.LogDir) await appendFile(`${config.LogDir}/${vm}.log`, `${req.ip} uploaded "${filename}" with MD5 ${hash}\n`); @@ -105,7 +108,7 @@ app.put("/:vm/:filename", async (req, res) => { .setTitle("File Uploaded"); discord!.send({ embeds: [embed] }); } - return { success: true, result: "File uploaded" }; + return { success: true, result: "File queued!" }; }); log("INFO", "Starting HTTP server on port " + config.ListenPort);