1

I need to first download the image from a public url, convert the downloaded image file to a multer file so that I can call an existing api which then uploads the multer file. I've tried axios responseType: "blob" and responseType: "arraybuffer" but I'm having trouble converting both responses into an Express.Multer.File. I kind of need it to be a multer file because we are also generating hashes from multer file buffer before we upload to our s3 storage. Using typescript I was coming up with something like this:

let imageResponse = await axios.get(imageURL, { responseType: "arraybuffer" });
let file: Express.Multer.File = {
             buffer: Buffer.from(imageResponse.data, "binary"),
             fieldname: "fieldname",
             originalname: "imageName.jpg",
             encoding: "binary",
             mimetype: "image/jpg",
             destination: __dirname,
           }

I know this looks horribly wrong but yea I'm not sure how to tackle this.

Ariel Frischer
  • 1,406
  • 2
  • 12
  • 20
  • in that case, you don't need to use multer, download the file and save directly to the location you prefer. Example code here https://stackoverflow.com/questions/11944932/how-to-download-a-file-with-node-js-without-using-third-party-libraries – Malkit Singh Mar 13 '20 at 04:47
  • I'm sure I can do this, but if I upload without first converting to a multer file, then I'm not able to generate a hash which we need from multer file.buffer – Ariel Frischer Mar 13 '20 at 06:05

1 Answers1

0

Kind of solved this without converting to a multer file, instead just make a file-like object with buffer property.

Ariel Frischer
  • 1,406
  • 2
  • 12
  • 20