Skip to content

Commit

Permalink
Folder upload support
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlueBurger committed Oct 5, 2024
1 parent c7f3866 commit c1bdd25
Show file tree
Hide file tree
Showing 5 changed files with 560 additions and 473 deletions.
46 changes: 19 additions & 27 deletions Server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,27 @@ app.post("/api/request/:name", async (req, res, next) => {
d: req.body
});
});
let maxUploadSize = 100_000_000;
let maxUploadSize = 250_000_000;
app.post("/api/uploadfile/:id", (req, res) => {
let cb = httpUploadCallbacks.get(req.params.id);
if(!cb) return res.sendStatus(401);
if(typeof cb != "function") return res.sendStatus(500); // in case
let uploadPath = httpUploadMap.get(req.params.id);
if(!uploadPath) return res.sendStatus(401);
if(typeof uploadPath != "string") return res.sendStatus(500); // in case
if(isNaN(parseInt(req.headers["content-length"] ?? "a")) || parseInt(req.headers["content-length"] ?? "a") > maxUploadSize) {
return res.status(400).send("Too big file!");
}
let data = Buffer.from([]);
req.on("data", chunk => {
data = Buffer.concat([data, chunk]);
if(data.byteLength > maxUploadSize) {
let writeStream = fs.createWriteStream(uploadPath);
let uploadedBytes = 0;
req.on("data", (chunk: Buffer) => {
if(uploadedBytes > maxUploadSize) {
logger.log(`Client lied about size. ID: ${req.params.id}, destroying connection.`, "error", LogLevel.ERROR);
req.socket.destroy();
}
uploadedBytes += chunk.byteLength;
writeStream.write(chunk);
});
req.on("end", () => {
if(data.byteLength > maxUploadSize) return;
writeStream.end();
res.sendStatus(200);
if(cb) cb(data);
});
});
app.options("/api/uploadfile/:id", (_,r) => r.sendStatus(200));
Expand Down Expand Up @@ -299,7 +300,7 @@ wss.on('connection', (_client) => {
clients.splice(clients.indexOf(client), 1);
});
});
let httpUploadCallbacks: Map<string, (b: Buffer) => void> = new Map();
let httpUploadMap: Map<string, string> = new Map();
let httpDownloadRequests: Map<string, string> = new Map();
export function requestDownload(fullPath: string, timeout: number = 60_000) {
let id = makeToken();
Expand All @@ -312,25 +313,16 @@ export function requestDownload(fullPath: string, timeout: number = 60_000) {
}, timeout);
return id;
}
export function requestUpload(timeout: number = 600_000): (string | Promise<Buffer>)[] {
export function requestUpload(filePath: string, timeout: number = 600_000): string {
let id = makeToken();
while(typeof httpUploadCallbacks.get(id) != "undefined") {
while(typeof httpUploadMap.get(id) != "undefined") {
id = makeToken();
}
return [id, new Promise((res, rej) => {
let cancelled = false;
httpUploadCallbacks.set(id,(buf: Buffer) => {
if(cancelled) return;
httpUploadCallbacks.delete(id);
clearTimeout(timeoutID);
res(buf);
});
let timeoutID = setTimeout(() => {
httpUploadCallbacks.delete(id);
cancelled = true;
rej("Timed out while waiting for upload id " + id);
}, timeout);
})];
httpUploadMap.set(id, filePath);
setTimeout(() => {
httpUploadMap.delete(id);
}, timeout);
return id;
}
let exiting = false;
export async function exit(signal?: string) {
Expand Down
7 changes: 1 addition & 6 deletions Server/src/packets/serverFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,8 @@ export default class ServerFiles extends Packet {
}
case "upload":
if(!hasServerPermission(client.data.auth.user, server.toJSON(), "serverfiles.upload")) return "no permission to upload";
let [id, promise] = await requestUpload();
let id = requestUpload(pathToCheck);
logger.log(`${client.data.auth.user?.username} is uploading ${data.path} to ${server.name} with upload ID '${id}'`, "server.file.upload");
if(!(promise instanceof Promise)) return;
if(typeof id != "string") return;
promise.then(async buf => {
await fs.writeFile(pathToCheck, buf);
});
return {
type: "uploadConfirm",
id
Expand Down
2 changes: 1 addition & 1 deletion Share/Packets.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Do not touch this file, it is automatically changed.
export default ["attachToServer","auth","createServer","createUser","deleteServer","deleteUser","detachFromServer","editUser","getAllServers","getAllSettings","getServer","getSetting","getUserData","getUserToken","getUsers","importServer","integrator","killServer","listSessions","logging","logout","ping","plugins","serverFiles","serverLogs","setServerOption","setSetting","startServer","stopServer","systemInformation","writeToConsole"] as const
export default ["attachToServer","auth","createServer","createUser","deleteServer","deleteUser","detachFromServer","editUser","getAllServers","getAllSettings","getServer","getSetting","getUserData","getUsers","getUserToken","importServer","integrator","killServer","listSessions","logging","logout","ping","plugins","serverFiles","serverLogs","setServerOption","setSetting","startServer","stopServer","systemInformation","writeToConsole"] as const
Loading

0 comments on commit c1bdd25

Please sign in to comment.