diff --git a/.gitignore b/.gitignore index 291b87b..0e26fc0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ .yarn/install-state.gz dist/ node_modules/ +src/test.ts diff --git a/src/Catbox.ts b/src/Catbox.ts index cf38d41..af0e110 100644 --- a/src/Catbox.ts +++ b/src/Catbox.ts @@ -1,7 +1,10 @@ -import fetch from 'node-fetch'; -import FormData from 'form-data'; -import { createReadStream } from 'fs'; -import { USER_AGENT, CATBOX_BASE_URL } from './constants'; +import fetch from 'node-fetch'; +import FormData from 'form-data'; +import { resolve } from 'node:path'; +import { createReadStream } from 'node:fs'; + +import { isValidFile } from './utils'; +import { USER_AGENT, CATBOX_BASE_URL } from './constants'; type UploadURLOptions = { /** @@ -120,11 +123,17 @@ export class Catbox { * @returns The uploaded file URL */ public async uploadFile(options: UploadFileOptions): Promise { - const { path } = options; - const data = new FormData(); + let { path } = options; + path = resolve(path); + + if (!await isValidFile(path)) { + throw new Error(`Invalid file path ${path}`); + } + const data = new FormData(); data.append('reqtype', 'fileupload'); data.append('fileToUpload', createReadStream(path)); + if (this._userHash) { data.append('userhash', this._userHash); } diff --git a/src/Litterbox.ts b/src/Litterbox.ts index d17fc6c..45aa74a 100644 --- a/src/Litterbox.ts +++ b/src/Litterbox.ts @@ -1,6 +1,9 @@ -import fetch from 'node-fetch'; -import FormData from 'form-data'; -import { createReadStream } from 'fs'; +import fetch from 'node-fetch'; +import FormData from 'form-data'; +import { resolve } from 'node:path'; +import { createReadStream } from 'node:fs'; + +import { isValidFile } from './utils'; import { USER_AGENT, LITTERBOX_BASE_URL } from './constants'; type UploadOptions = { @@ -26,11 +29,18 @@ export class Litterbox { * @returns The uploaded file URL */ public async upload(options: UploadOptions): Promise { - const { path, duration } = options; + let { path, duration } = options; + path = resolve(path); + duration = duration ?? '1h'; + + if (!await isValidFile(path)) { + throw new Error(`Invalid file path ${path}`); + } + const data = new FormData(); data.append('reqtype', 'fileupload'); data.append('fileToUpload', createReadStream(path)); - data.append('time', duration ?? '1h'); + data.append('time', duration); const res = await fetch(LITTERBOX_BASE_URL, { method: 'POST', diff --git a/src/index.ts b/src/index.ts index 2098b39..5436794 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ -export { Catbox } from './Catbox'; +export { Catbox } from './Catbox'; export { Litterbox } from './Litterbox'; diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..5c1507c --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,11 @@ +import { stat } from 'node:fs/promises'; + +export async function isValidFile(path: string): Promise { + try { + const stats = await stat(path); + + return stats.isFile(); + } catch { + return false; + } +};