Add file queue

This commit is contained in:
Elijah 2023-12-08 21:27:48 -05:00
parent d8dbc351bb
commit 8412aeb1af
4 changed files with 34 additions and 5 deletions

View file

@ -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"
}
}

4
src/File.ts Normal file
View file

@ -0,0 +1,4 @@
export default interface File {
Filename : string;
FileData : Buffer;
}

View file

@ -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<File> = new Queue<File>();
#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<void> {
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<void> {
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);

View file

@ -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);