jeffminsungkim / nestjs-multer-extended

💪 Extended MulterModule for NestJS with flexible S3 upload and helpful features
MIT License
202 stars 47 forks source link

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

Open jonasschultheiss opened 4 years ago

jonasschultheiss commented 4 years ago

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
    }
    }
  2. 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 commented 4 years ago

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?

jeffminsungkim commented 3 years ago

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?

jonasschultheiss commented 3 years ago

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

jeffminsungkim commented 3 years ago

@jonasschultheiss

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

jonasschultheiss commented 3 years ago

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

jeffminsungkim commented 3 years ago

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

whather commented 3 years ago

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

jonasschultheiss commented 3 years ago

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.

denes16 commented 3 years ago

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