Skip to content

Commit

Permalink
feat: Add random filename, ability to use random filenames on upload (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
volbrene committed Mar 5, 2020
1 parent 32f443d commit 8c4110d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/interfaces/multer-extended-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface MultipleSizeOptions {

export interface MulterExtendedOptions extends Pick<MulterOptions, 'fileFilter' | 'limits'> {
dynamicPath?: string;
randomFilename?: boolean;
resize?: ResizeOptions;
resizeMultiple?: MultipleSizeOptions[];
thumbnail?: MultipleSizeOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface AmazonS3UploadOptions extends Partial<S3.Types.PutObjectRequest
s3: S3;
Key?: any;
dynamicPath?: string;
randomFilename?: boolean;
}
7 changes: 6 additions & 1 deletion lib/multer-sharp/multer-sharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { isFunction, isString } from '@nestjs/common/utils/shared.utils';
import { Request } from 'express';
import { from, Observable } from 'rxjs';
import { map, mergeMap, toArray, first } from 'rxjs/operators';
import { lookup } from 'mime-types';
import { lookup, extension } from 'mime-types';
import { S3StorageOptions, S3Storage } from './interfaces/s3-storage.interface';
import { SharpOptions, Size, ExtendSize } from './interfaces/sharp-options.interface';
import {
Expand All @@ -15,6 +15,7 @@ import {
transformImage,
isOriginalSuffix,
} from './multer-sharp.utils';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';

export interface EventStream {
stream: NodeJS.ReadableStream & Sharp;
Expand Down Expand Up @@ -76,6 +77,10 @@ export class MulterSharp implements StorageEngine, S3Storage {
return;
}

if (storageOpts.randomFilename) {
file.originalname = `${randomStringGenerator()}.${extension(mimetype)}`;
}

const { originalname } = file;

params.Key = storageOpts.dynamicPath
Expand Down
9 changes: 9 additions & 0 deletions tests/src/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ describe('AppModule', () => {
expect(res.body.key).toEqual(`${dynamicPath}/smile.jpg`);
});

it(`should upload an image with random filename`, async () => {
const res = await request(app.getHttpServer())
.post(`/image-upload/with-random-filename`)
.set('Content-Type', 'multipart/form-data')
.attach('file', path.resolve(__dirname, 'data/smile.jpg'));

expect(res.status).toEqual(201);
});

it(`should not upload non image format`, async () => {
const res = await request(app.getHttpServer())
.post(`/image-upload/non-image-file`)
Expand Down
6 changes: 6 additions & 0 deletions tests/src/image-upload/image-upload.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export class ImageUploadController {
return file;
}

@Post('with-random-filename')
@UseInterceptors(AmazonS3FileInterceptor('file', { randomFilename: true }))
async uploadImageWithRandomFilenameKeyOption(@UploadedFile() file: any): Promise<any> {
return file;
}

@Post('non-image-file')
@UseInterceptors(AmazonS3FileInterceptor('file'))
async uploadNonImageFile(@UploadedFile() file: any): Promise<void> {}
Expand Down

0 comments on commit 8c4110d

Please sign in to comment.