From 3163ad649b3c3cd81001d4e50161bc6d7802853e Mon Sep 17 00:00:00 2001 From: stringsplit <77242831+nbitzz@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:37:37 -0700 Subject: [PATCH] TSDoc files.ts & errors.ts --- src/server/lib/errors.ts | 13 ++++++++++++- src/server/lib/files.ts | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/server/lib/errors.ts b/src/server/lib/errors.ts index 6e67f33..84c1cc5 100644 --- a/src/server/lib/errors.ts +++ b/src/server/lib/errors.ts @@ -3,6 +3,12 @@ import { readFile } from "fs/promises" let errorPage:string +/** + * @description Serves an error as a response to a request with an error page attached + * @param res Express response object + * @param code Error code + * @param reason Error reason + */ export default async function ServeError( res:Response, code:number, @@ -29,7 +35,12 @@ export default async function ServeError( .replace(/\$text/g,reason) ) } - +/** + * @description Redirects a user to another page. + * @param res Express response object + * @param url Target URL + * @deprecated Use `res.redirect` instead. + */ export function Redirect(res:Response,url:string) { res.status(302) res.header("Location",url) diff --git a/src/server/lib/files.ts b/src/server/lib/files.ts index d0be358..259761e 100644 --- a/src/server/lib/files.ts +++ b/src/server/lib/files.ts @@ -14,6 +14,11 @@ export let alphanum = Array.from("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST export type FileVisibility = "public" | "anonymous" | "private" +/** + * @description Generates an alphanumeric string, used for files + * @param length Length of the ID + * @returns a random alphanumeric string + */ export function generateFileId(length:number=5) { let fid = "" for (let i = 0; i < length; i++) { @@ -40,7 +45,10 @@ export interface Configuration { accounts: { registrationEnabled: boolean, requiredForUpload: boolean - } + }, + + trustProxy: boolean, + forceSSL: boolean } export interface FilePointer { @@ -93,6 +101,12 @@ export default class Files { } + /** + * @description Uploads a new file + * @param settings Settings for your new upload + * @param fBuffer Buffer containing file content + * @returns Promise which resolves to the ID of the new file + */ uploadFile(settings:FileUploadSettings,fBuffer:Buffer):Promise { return new Promise(async (resolve,reject) => { if (!this.uploadChannel) { @@ -244,6 +258,12 @@ export default class Files { // fs + /** + * @description Writes a file to disk + * @param uploadId New file's ID + * @param file FilePointer representing the new file + * @returns Promise which resolves to the file's ID + */ writeFile(uploadId: string, file: FilePointer):Promise { return new Promise((resolve, reject) => { @@ -264,8 +284,12 @@ export default class Files { }) } - // todo: move read code here - + /** + * @description Read a file + * @param uploadId Target file's ID + * @param range Byte range to get + * @returns A `Readable` containing the file's contents + */ readFileStream(uploadId: string, range?: {start:number, end:number}):Promise { return new Promise(async (resolve,reject) => { if (!this.uploadChannel) { @@ -401,6 +425,11 @@ export default class Files { }) } + /** + * @description Deletes a file + * @param uploadId Target file's ID + * @param noWrite Whether or not the change should be written to disk. Enable for bulk deletes + */ unlink(uploadId:string, noWrite: boolean = false):Promise { return new Promise(async (resolve,reject) => { let tmp = this.files[uploadId]; @@ -431,6 +460,11 @@ export default class Files { }) } + /** + * @description Get a file's FilePointer + * @param uploadId Target file's ID + * @returns FilePointer for the file + */ getFilePointer(uploadId:string):FilePointer { return this.files[uploadId] }