Add file queue
This commit is contained in:
parent
d8dbc351bb
commit
8412aeb1af
4 changed files with 34 additions and 5 deletions
|
@ -20,6 +20,7 @@
|
||||||
"discord.js": "^14.14.1",
|
"discord.js": "^14.14.1",
|
||||||
"fastify": "^4.24.3",
|
"fastify": "^4.24.3",
|
||||||
"md5": "^2.3.0",
|
"md5": "^2.3.0",
|
||||||
|
"mnemonist": "^0.39.6",
|
||||||
"msgpack-lite": "^0.1.26"
|
"msgpack-lite": "^0.1.26"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
4
src/File.ts
Normal file
4
src/File.ts
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
export default interface File {
|
||||||
|
Filename : string;
|
||||||
|
FileData : Buffer;
|
||||||
|
}
|
27
src/VM.ts
27
src/VM.ts
|
@ -3,11 +3,15 @@ import * as protocol from './Protocol.js'
|
||||||
import * as msgpack from 'msgpack-lite';
|
import * as msgpack from 'msgpack-lite';
|
||||||
import {Mutex} from 'async-mutex';
|
import {Mutex} from 'async-mutex';
|
||||||
import log from './log.js';
|
import log from './log.js';
|
||||||
|
import Queue from 'mnemonist/queue.js';
|
||||||
|
import File from "./File.js";
|
||||||
|
|
||||||
export default class VM {
|
export default class VM {
|
||||||
#socketpath : string;
|
#socketpath : string;
|
||||||
#socket : Socket;
|
#socket : Socket;
|
||||||
#writeLock : Mutex = new Mutex();
|
#writeLock : Mutex = new Mutex();
|
||||||
|
#fileQueue : Queue<File> = new Queue<File>();
|
||||||
|
#isFileQueueRunning : boolean = false;
|
||||||
connected : boolean = false;
|
connected : boolean = false;
|
||||||
constructor(socketpath : string) {
|
constructor(socketpath : string) {
|
||||||
this.#socketpath = socketpath;
|
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) => {
|
return new Promise(async (res, rej) => {
|
||||||
const msg : protocol.ProtocolMessage = {
|
const msg : protocol.ProtocolMessage = {
|
||||||
Operation: protocol.ProtocolOperation.UploadFile,
|
Operation: protocol.ProtocolOperation.UploadFile,
|
||||||
Filename: filename,
|
Filename: file.Filename,
|
||||||
FileData: data
|
FileData: file.FileData
|
||||||
};
|
};
|
||||||
var payload = msgpack.encode(msg);
|
var payload = msgpack.encode(msg);
|
||||||
await this.#sendMessage(payload);
|
await this.#sendMessage(payload);
|
||||||
|
|
|
@ -95,7 +95,10 @@ app.put("/:vm/:filename", async (req, res) => {
|
||||||
log("INFO", `${vm}: ${req.ip} tried to upload "${filename}" with blocked MD5 ${hash}`);
|
log("INFO", `${vm}: ${req.ip} tried to upload "${filename}" with blocked MD5 ${hash}`);
|
||||||
return { success: false, result: "That file is not allowed" };
|
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}`);
|
log("INFO", `${vm}: ${req.ip} uploaded "${filename}" with MD5 ${hash}`);
|
||||||
if (config.LogDir)
|
if (config.LogDir)
|
||||||
await appendFile(`${config.LogDir}/${vm}.log`, `${req.ip} uploaded "${filename}" with MD5 ${hash}\n`);
|
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");
|
.setTitle("File Uploaded");
|
||||||
discord!.send({ embeds: [embed] });
|
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);
|
log("INFO", "Starting HTTP server on port " + config.ListenPort);
|
||||||
|
|
Loading…
Reference in a new issue