Skip to content

Commit

Permalink
feat: extended onComplete (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
kukhariev authored Feb 6, 2020
1 parent da10a1d commit 719c642
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
9 changes: 7 additions & 2 deletions examples/express-gcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import { uploadx, GCStorage } from '../src';
const app = express();

const storage = new GCStorage();
storage.onComplete = file => console.log('File upload complete: ', file);
app.use('/upload/files', uploadx({ storage }));

storage.onComplete = file => {
(file as any)['custom'] = 'Hi!';
console.log('File upload complete: ', file);
};

app.use('/files', uploadx({ storage }));

app.listen(3003, error => {
if (error) throw error;
Expand Down
4 changes: 2 additions & 2 deletions src/storages/disk-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export interface DiskListObject {
name: string;
updated: Date;
}
export interface DiskStorageOptions extends BaseStorageOptions {
export type DiskStorageOptions = BaseStorageOptions<DiskFile> & {
/**
* Uploads directory
* @defaultValue './files'
*/
directory?: string;
}
};

/**
* Local Disk Storage
Expand Down
2 changes: 1 addition & 1 deletion src/storages/gcs-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function buildContentRange(part: FilePart & GCSFile): string {
const validateStatus: (code: number) => boolean = (code: number) =>
(code >= 200 && code < 300) || code === 308 || code === 499;

export type GCStorageOptions = BaseStorageOptions &
export type GCStorageOptions = BaseStorageOptions<GCSFile> &
GoogleAuthOptions & {
/**
* Google Cloud Storage bucket
Expand Down
2 changes: 1 addition & 1 deletion src/storages/s3-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class S3File extends File {
uri?: string;
}

export type S3StorageOptions = BaseStorageOptions &
export type S3StorageOptions = BaseStorageOptions<S3File> &
S3.ClientConfiguration & {
/**
* AWS S3 bucket
Expand Down
14 changes: 8 additions & 6 deletions src/storages/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { File, FileInit, FilePart } from './file';

export const METAFILE_EXTNAME = '.META';

export interface BaseStorageOptions {
export type OnComplete<T = File> = (file: Readonly<T>) => any;

export interface BaseStorageOptions<T> {
/** Allowed file types */
allowMIME?: string[];
/** File size limit */
Expand All @@ -14,12 +16,12 @@ export interface BaseStorageOptions {
filename?: (file: Partial<File>) => string;
useRelativeLocation?: boolean;
/** Completed callback */
onComplete?: (file: File) => void;
onComplete?: OnComplete<T>;
/** Node http base path */
path?: string;
}

const defaultOptions: Required<BaseStorageOptions> = {
const defaultOptions: Required<BaseStorageOptions<any>> = {
allowMIME: ['*/*'],
maxUploadSize: '50GB',
filename: ({ userId, id }: Partial<File>): string => [userId, id].filter(Boolean).join('-'),
Expand All @@ -32,13 +34,13 @@ export type Validator = (file: File) => string | false;

export abstract class BaseStorage<TFile, TList> {
validators: Set<Validator> = new Set();
onComplete: (file: File) => void;
onComplete: (file: Readonly<TFile>) => void;
path: string;
isReady = false;
protected log = Logger.get(`store:${this.constructor.name}`);
protected namingFunction: (file: Partial<File>) => string;
constructor(public config: BaseStorageOptions) {
const opts: Required<BaseStorageOptions> = { ...defaultOptions, ...config };
constructor(public config: BaseStorageOptions<TFile>) {
const opts: Required<BaseStorageOptions<TFile>> = { ...defaultOptions, ...config };
this.path = opts.path;
this.onComplete = opts.onComplete;
this.namingFunction = opts.filename;
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BaseStorageOptions } from '../../src';
import { BaseStorageOptions, File } from '../../src';
export const userPrefix = 'userId';

export const root = 'files';

export const storageOptions: BaseStorageOptions = {
export const storageOptions: BaseStorageOptions<File> = {
filename: file => `${file.userId}/${file.originalName}`,
maxUploadSize: '6GB',
allowMIME: ['video/*', 'image/*', 'application/octet-stream'],
Expand Down

0 comments on commit 719c642

Please sign in to comment.