Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JS Sample 15] Fix for image attachments #1323

Merged
merged 1 commit into from
Mar 13, 2019
Merged

[JS Sample 15] Fix for image attachments #1323

merged 1 commit into from
Mar 13, 2019

Conversation

mdrichardson
Copy link
Contributor

Fixes #1293

Per issue, without this change, an attached/uploaded image or PDF (likely some other file formats, too) would corrupt while saving locally. @Zerryth and I found that this fixes it.

Proposed Changes

When calling downloadAttachmentAndWrite(attachment):

  • Gets the attachment response as an 'arraybuffer' (instead of default ?blob?)
  • If content-type is application/json, converts before saving to prevent .json file content being something like: "{"type":"Buffer","data":[123,13,10,32,32,34,108..."

Testing

Tested with the following filetypes:

  • jpg
  • msi
  • txt
  • json
  • js
  • ts
  • png
  • mp4
  • pdf

They all work.

@mdrichardson mdrichardson requested a review from stevengum March 13, 2019 17:45
Copy link
Member

@stevengum stevengum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

// 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') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

download Attachment And Write failed nodejs
2 participants