DEV Community

Nestjs(fastify, multer). Uploading & cropping image.

Sergey Telpuk on August 10, 2019

Hello friends! At this article, I wanna show you how we can download and crop image with the help of Multer and in the same time to use different...
Collapse
 
ruslangonzalez profile image
Ruslan Gonzalez

I was looking something about uploading to S3 AWS... thanks... I'll try following a little about your setup and hope will works. If you have recommendations would be much appreciated. My challenge is to just host files to AWS with some Multer Validations (I think it already do very good)...

Collapse
 
sergey_telpuk profile image
Sergey Telpuk • Edited

Hello, I can put forward using localstack/localstack:latest for trying it out.

Collapse
 
ruslangonzalez profile image
Ruslan Gonzalez

Actually I used part of your code and got AWS I integrated with my stack. I am appreciated.

Collapse
 
devzepto profile image
Nutchapong

Thank you so much for your guide. I have tested it out and it works great so far. The response from the controller is replied with the original image detail uploaded to S3 AWS. Is it possible to get the detail of both the cropped image and the original image as a response after finish uploading?

Collapse
 
aayani profile image
Wahaj Aayani

Couldn't get it running. How to export the UploadImageFactory to a module?

Collapse
 
sergey_telpuk profile image
Sergey Telpuk

Hi, You can do it like below:

import { Module } from '@nestjs/common';

export const UploadImageFactory: FactoryProvider = {
    provide: 'IUploadImage',
    useFactory: () => {
        return StorageFactory.createStorageFromType(TYPE_STORAGE);
    },
};

@Module({
  controllers: [],
  providers: [UploadImageFactory],
})
export class CatsModule {}
Collapse
 
aayani profile image
Wahaj Aayani • Edited

Thanks for your response. Figured that out earlier. I was somewhat trying to make it a dynamic module to be exported and used elsewhere in the code. I can see that you have hard-coded AWS config in it's adapter itself which is ofc not the best of practices, and now if you want to take it out of there and pass in from the module itself. How would you recommend doing that?!

Thread Thread
 
sergey_telpuk profile image
Sergey Telpuk

To be honest, I made this solution in a hurry. You know, I start thinking about making a library MulterCropper.
You are right about hard-coded config, This solution sucks, As for me the best way is to take it out and pass via a constructor.

Thread Thread
 
aayani profile image
Wahaj Aayani

It really helped me to say the least. Thank you. That's a nice idea for a library though.

Collapse
 
rainercedric23 profile image
Cedric Mariano

great implementation indeed, couldn't think any better on how to implement this, as I created one with the traditional, controller service approach with providers. one question though, have you tried handling multiple images upload on single request for this one? if yes, how many files do you recommend to be uploaded once or how big is the size for example on the s3 storage?

Collapse
 
sergey_telpuk profile image
Sergey Telpuk • Edited

Hello, by default this solution doesn't support multiple files, about size and how big it depends on your aims