Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

Passing a readable stream into uploadStylesheetImage method #122

Closed
emyarod opened this issue Jan 4, 2018 · 4 comments
Closed

Passing a readable stream into uploadStylesheetImage method #122

emyarod opened this issue Jan 4, 2018 · 4 comments

Comments

@emyarod
Copy link

emyarod commented Jan 4, 2018

I am trying to upload images in an archive as readable streams but keep receiving error 500s back in reddit's response. Am I incorrectly using the uploadStylesheetImage method? As an alternative, I am currently uncompressing the images into a temp directory then removing that directory after the file uploads are complete. How would I stream the images from the archive to the uploadStylesheetImage method?

const restoreStylesheetImage = (r, subreddit, stylesheetImage) => {
  const [name, imageType] = stylesheetImage.name.split('.');
  const strim = new stream.PassThrough();
  strim.end(stylesheetImage.data); // stylesheetImage.data is an image as type Node.js Buffer

  r
    .getSubreddit(subreddit)
    .uploadStylesheetImage({
      name,
      imageType,
      file: strim,
    })
    .catch(console.error);
};
@not-an-aardvark
Copy link
Owner

Thanks for the report. It looks like this is caused by request/request#2499.

You could try the workaround described in form-data/form-data#356 (comment), although it doesn't seem like an ideal solution.

@emyarod
Copy link
Author

emyarod commented Jan 4, 2018

if I'm not mistaken, it appears that the workaround will not apply to my use case since I am uploading from a gzip stream

@not-an-aardvark aside from swapping out the request dependency for bhttp in snoowrap, I'll have to continue using my initial workaround, right?

@not-an-aardvark
Copy link
Owner

Well, you could probably accumulate the stream into a buffer, and then use the workaround to send the buffer (which would have known length at that point). That does lose some of the advantages of using a stream, but it will probably be faster than writing to the filesystem, and it won't leave files around if something crashes.

@Venefilyn
Copy link
Collaborator

Since the solution proposed by not-an-aardvark seems to work I'm going to go ahead and close this due to inactivity. If you want to reopen the ticket just make a comment and I'll open it.

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

No branches or pull requests

3 participants