Skip to content

Commit

Permalink
[JS Sample 15] Fix for image attachments (#1323)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdrichardson authored and stevengum committed Mar 14, 2019
1 parent bf99a66 commit 7b7942e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion samples/javascript_nodejs/15.handling-attachments/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,16 @@ class AttachmentsBot {
const localFileName = path.join(__dirname, attachment.name);

try {
const response = await axios.get(url);
// arraybuffer is necessary for images
const response = await axios.get(url, { responseType: 'arraybuffer' });
// If user uploads JSON file, this prevents it from being written as "{"type":"Buffer","data":[123,13,10,32,32,34,108..."
if (response.headers['content-type'] === 'application/json') {
response.data = JSON.parse(response.data, (key, value) => {
return value && value.type === 'Buffer' ?
Buffer.from(value.data) :
value;
});
}
fs.writeFile(localFileName, response.data, (fsError) => {
if (fsError) {
throw fsError;
Expand Down

0 comments on commit 7b7942e

Please sign in to comment.