TSDoc files.ts & errors.ts

This commit is contained in:
May 2023-10-03 13:37:37 -07:00 committed by GitHub
parent 937363c9ee
commit 3163ad649b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 4 deletions

View file

@ -3,6 +3,12 @@ import { readFile } from "fs/promises"
let errorPage:string 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( export default async function ServeError(
res:Response, res:Response,
code:number, code:number,
@ -29,7 +35,12 @@ export default async function ServeError(
.replace(/\$text/g,reason) .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) { export function Redirect(res:Response,url:string) {
res.status(302) res.status(302)
res.header("Location",url) res.header("Location",url)

View file

@ -14,6 +14,11 @@ export let alphanum = Array.from("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST
export type FileVisibility = "public" | "anonymous" | "private" 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) { export function generateFileId(length:number=5) {
let fid = "" let fid = ""
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
@ -40,7 +45,10 @@ export interface Configuration {
accounts: { accounts: {
registrationEnabled: boolean, registrationEnabled: boolean,
requiredForUpload: boolean requiredForUpload: boolean
} },
trustProxy: boolean,
forceSSL: boolean
} }
export interface FilePointer { 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<string|StatusCodeError> { uploadFile(settings:FileUploadSettings,fBuffer:Buffer):Promise<string|StatusCodeError> {
return new Promise<string>(async (resolve,reject) => { return new Promise<string>(async (resolve,reject) => {
if (!this.uploadChannel) { if (!this.uploadChannel) {
@ -244,6 +258,12 @@ export default class Files {
// fs // 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<string> { writeFile(uploadId: string, file: FilePointer):Promise<string> {
return new Promise((resolve, reject) => { 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<Readable> { readFileStream(uploadId: string, range?: {start:number, end:number}):Promise<Readable> {
return new Promise(async (resolve,reject) => { return new Promise(async (resolve,reject) => {
if (!this.uploadChannel) { 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<void> { unlink(uploadId:string, noWrite: boolean = false):Promise<void> {
return new Promise(async (resolve,reject) => { return new Promise(async (resolve,reject) => {
let tmp = this.files[uploadId]; 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 { getFilePointer(uploadId:string):FilePointer {
return this.files[uploadId] return this.files[uploadId]
} }