mirror of
https://github.com/mollersuite/monofile.git
synced 2024-11-21 21:36:26 -08:00
start work on writeFileStream
This commit is contained in:
parent
b8d46bb193
commit
c57f4ed692
|
@ -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<boolean, any>) {
|
||||
conditions.forEach((err, cond) => {
|
||||
if (cond) throw err
|
||||
})
|
||||
}
|
||||
|
||||
export type FileUploadSettings = Partial<Pick<FilePointer, "mime" | "owner">> &
|
||||
Pick<FilePointer, "mime" | "filename"> & { 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
|
||||
|
|
Loading…
Reference in a new issue