nestjs-multer-extended icon indicating copy to clipboard operation
nestjs-multer-extended copied to clipboard

AmazonS3FileInterceptor only uses one MulterExtendedOptions property, even if two were passed

Open jonasschultheiss opened this issue 3 years ago • 9 comments

Hey, first of all, cool package. It's really easy to upload to a s3 with this.

One thing I noticed on my project was, that the second property of MulterExtendedOptions doesn't seem to make a difference in the AmazonS3FileInterceptor. What I'd like to have random file names and thumbnails, but it appears to ignore the second option.

  1. Example 1 -> thumbnail first: 1.1. Code

    // image.controller.ts
    
    @Controller()
    export class ImageController {
      constructor(private imageService: ImageService) {}
    
      @Post('/users/:id/avatar')
      @UseInterceptors(
        AmazonS3FileInterceptor('file', {
          thumbnail: { suffix: 'thumb', width: 500, height: 500 },
          randomFilename: true,
        }),
      )
      async setAvatar(
        @Param('id', ParseIntPipe) userId: number,
        @UploadedFile() uploadedImage: UploadImageDto,
      ): Promise<Image> {
        console.log(
          'ImageController -> constructor -> uploadedImage',
          uploadedImage,
        );
        return this.imageService.setAvatar(userId, uploadedImage);
      }
    }
    

1.2 Console out

image -> Image {
  originalname: '5.png',
  mimetype: 'image/png',
  original: ImageData {
    ACL: 'public-read',
    Location: 'https://yxcvbnm.ams3.digitaloceanspaces.com/dev/5.png-original',
    key: 'dev/5.png-original',
    Key: 'dev/5.png-original',
    Bucket: 'yxcvbnm',
    format: 'png',
    width: 200,
    height: 200,
    channels: 4,
    premultiplied: false,
    size: 1593,
    ContentType: 'png',
    mimetype: 'image/png',
    id: 9
  },
  thumb: ImageData {
    ACL: 'public-read',
    Location: 'https://yxcvbnm.ams3.digitaloceanspaces.com/dev/5.png-thumb',
    key: 'dev/5.png-thumb',
    Key: 'dev/5.png-thumb',
    Bucket: 'yxcvbnm',
    format: 'png',
    width: 500,
    height: 500,
    channels: 4,
    premultiplied: true,
    size: 26997,
    ContentType: 'png',
    mimetype: 'image/png',
    id: 10
  }
}
  1. Example 2 -> randomFileName first 2.1. Code

    // image.controller.ts
    
    @Controller()
    export class ImageController {
      constructor(private imageService: ImageService) {}
    
      @Post('/users/:id/avatar')
      @UseInterceptors(
        AmazonS3FileInterceptor('file', {
    		  randomFilename: true,
          thumbnail: { suffix: 'thumb', width: 500, height: 500 },
        }),
      )
      async setAvatar(
        @Param('id', ParseIntPipe) userId: number,
        @UploadedFile() uploadedImage: UploadImageDto,
      ): Promise<Image> {
        console.log(
          'ImageController -> constructor -> uploadedImage',
          uploadedImage,
        );
        return this.imageService.setAvatar(userId, uploadedImage);
      }
    }
    

2.2 Console out

image -> Image {
	fieldname: 'file',
	originalname: '5.png',
	encoding: '7bit',
	mimetype: 'image/png',
	ACL: 'public-read',
	ETag: '"6eb01f522a374f04a3d790bab8433f7f"',
	Location: 'https://frozenpants.ams3.digitaloceanspaces.com/dev/77945170-51e8-4190-944f-26d26669518c.png',
  key: 'dev/77945170-51e8-4190-944f-26d26669518c.png',
  Key: 'dev/77945170-51e8-4190-944f-26d26669518c.png',
  Bucket: 'frozenpants',
  width: 200,
  height: 200,
  premultiplied: false,
  size: 1593,
  ContentType: 'png'
}

As you can see, the first example creates a thumbnail but doesn't upload the file with a random name, while the second example does the oposite.

If I made a mistake in my code, feel free to tell me.

jonasschultheiss avatar Sep 17 '20 07:09 jonasschultheiss

https://github.com/jeffminsungkim/nestjs-multer-extended/blob/25c824838edfa900548f671b9640aa9b4bdbfa72/lib/interceptors/amazon-s3-file.interceptor.ts#L63

let storageOptions: S3StorageOptions;
const extendedOptionProperty = Object.keys(this.localOptions)[0];

switch (extendedOptionProperty) {

Could it be, that this part only takes the first property into account and then switches through it?

jonasschultheiss avatar Sep 17 '20 07:09 jonasschultheiss

Thanks for opening an issue @jonasschultheiss First of all, sorry for the late response. Currently, we don't support the feature that you're trying to achieve. May I ask why you want this feature?

jeffminsungkim avatar Oct 08 '20 01:10 jeffminsungkim

I'd like to have this feature, so I have a thumbnail and both have a unique name.

  • no problems on the s3 because of naming stuff
  • thumbnail for react frontend
  • reduces loading time, if a gallery of images should be shown

jonasschultheiss avatar Oct 09 '20 07:10 jonasschultheiss

@jonasschultheiss

Sounds fair to me. Would you like to create a PR for this?

jeffminsungkim avatar Oct 11 '20 06:10 jeffminsungkim

sure, but make sure to review it, as I don't have that much experience with open source projects

jonasschultheiss avatar Oct 11 '20 21:10 jonasschultheiss

@jonasschultheiss No worries! I can help you if you need anything 😀

jeffminsungkim avatar Oct 12 '20 00:10 jeffminsungkim

@jonasschultheiss are you still working on a PR for this?

whather avatar Nov 13 '20 19:11 whather

No, the work and final phase at school are a bit stressful for me. I like to work on it at the beginning of the new year.

jonasschultheiss avatar Nov 15 '20 18:11 jonasschultheiss

Any, news on this?. I think we only need to add randomFilename: this.localOptions.randomFilename like dynamicPath: this.localOptions.dynamicPath, in https://github.com/jeffminsungkim/nestjs-multer-extended/blob/25c824838edfa900548f671b9640aa9b4bdbfa72/lib/interceptors/amazon-s3-file.interceptor.ts#L63 on every siwtch statement

denes16 avatar Sep 19 '21 22:09 denes16