0

I'm getting image via line api. when user send image to my bot, I will get it, and upload to aws s3 bucket: GET https://api.line.me/v2/bot/message/{messageId}/content

I got success image. I'm using file = fs.createWriteStream("file.jpg") and pipe(file). However, it will be fixed at file extension "jpg". For example, the file I get has the extension "gif", it will still save in the format "jpg".

So, how to get image format, if I know image format, i can use fs.createWriteStream to create file have the same format.

Nam Lee
  • 891
  • 1
  • 9
  • 20

1 Answers1

0

You can simply first read headers of image and you will get image format in it, according to image format can save it to s3 Bucket.

Here is the sample code.

request.head('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png', function (err, res, body) {
    console.log('content-type:', res.headers['content-type']);
    console.log('content-length:', res.headers['content-length']);
    let filename = 'image.png' // according to content type
    request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});

filename can be decided according to content-type

Aabid
  • 953
  • 5
  • 23