From c57f4ed69254aabc9038b9b28caa68cf576752b0 Mon Sep 17 00:00:00 2001 From: stringsplit <77242831+nbitzz@users.noreply.github.com> Date: Wed, 6 Dec 2023 20:57:31 +0000 Subject: [PATCH] start work on writeFileStream --- src/server/lib/files.ts | 48 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/server/lib/files.ts b/src/server/lib/files.ts index 760a475..e28b03e 100644 --- a/src/server/lib/files.ts +++ b/src/server/lib/files.ts @@ -1,5 +1,5 @@ import { readFile, writeFile } from "node:fs/promises" -import { Readable } from "node:stream" +import { Readable, Writable } from "node:stream" import crypto from "node:crypto" import { files } from "./accounts" import { Client as API } from "./DiscordAPI" @@ -31,6 +31,17 @@ export function generateFileId(length: number = 5) { return fid } +/** + * @description Assert multiple conditions... this exists out of pure laziness + * @param conditions + */ + +function multiAssert(conditions: Map) { + conditions.forEach((err, cond) => { + if (cond) throw err + }) +} + export type FileUploadSettings = Partial> & Pick & { uploadId?: string } @@ -100,6 +111,41 @@ export default class Files { .catch(console.error) } + async writeFileStream(metadata: FileUploadSettings) { + + let uploadId = (metadata.uploadId || generateFileId()).toString() + + multiAssert( + new Map() + .set(!metadata.filename, {status: 400, message: "missing filename"}) + .set(metadata.filename.length > 128, {status: 400, message: "filename too long"}) + .set(!metadata.mime, {status: 400, message: "missing mime type"}) + .set(metadata.mime.length > 128, {status: 400, message: "mime type too long"}) + .set( + uploadId.match(id_check_regex)?.[0] != uploadId + || uploadId.length > this.config.maxUploadIdLength, + { status: 400, message: "invalid file ID" } + ) + .set( + this.files[uploadId] && + (metadata.owner + ? this.files[uploadId].owner != metadata.owner + : true), + { status: 403, message: "you don't own this file" } + ) + .set( + this.files[uploadId]?.reserved, + { + status: 400, + message: "already uploading this file. if your file is stuck in this state, contact an administrator" + } + ) + ) + + + + } + /** * @description Uploads a new file * @param metadata Settings for your new upload