Skip to content

Commit

Permalink
refactor: add validation to file upload methods
Browse files Browse the repository at this point in the history
  • Loading branch information
depthbomb committed Apr 27, 2022
1 parent 0915d2d commit f37d83e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
.yarn/install-state.gz
dist/
node_modules/
src/test.ts
21 changes: 15 additions & 6 deletions src/Catbox.ts
Original file line number Diff line number Diff line change
@@ -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 = {
/**
Expand Down Expand Up @@ -120,11 +123,17 @@ export class Catbox {
* @returns The uploaded file URL
*/
public async uploadFile(options: UploadFileOptions): Promise<string> {
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);
}
Expand Down
20 changes: 15 additions & 5 deletions src/Litterbox.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -26,11 +29,18 @@ export class Litterbox {
* @returns The uploaded file URL
*/
public async upload(options: UploadOptions): Promise<string> {
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',
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { Catbox } from './Catbox';
export { Catbox } from './Catbox';
export { Litterbox } from './Litterbox';
11 changes: 11 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { stat } from 'node:fs/promises';

export async function isValidFile(path: string): Promise<boolean> {
try {
const stats = await stat(path);

return stats.isFile();
} catch {
return false;
}
};

0 comments on commit f37d83e

Please sign in to comment.