So, I have a java.io.BufferedInputStream which is returned from an S3 request for an image. I'd like to return that from my ring server. I tried something like returning {:body (:Body s3-res)} , which doesn't seem to work. The browser ends up returning an ASCII file with the contents #object[java.io.BufferedInputStream 0x65b906f0 ". What's the proper way of doing this?
I was looking at the docs and I found ring.core.protocols/StreamableResponseBody , If I do (satisfies? ring.core.protocols/StreamableResponseBody (:Body s3-res)) it returns true 🤔
(I'm always confused by streams 🙃)
This is more or less the code that I wrote
(let [s3-res (cogs-aws/invoke s3 {:op :GetObject
:request {:Bucket bucket
:Key s3-key}})]
(-> (ring.util.response/response (:Body s3-res))
(g/partial-content)
(rerus/header "Content-Range" (:ContentRange s3-res))
(rerus/header "Content-Type" (:ContentType s3-res))
(rerus/header "Content-Length" (:ContentLength s3-res))
(rerus/header "Accept-Ranges" (:AcceptRanges s3-res))
(rerus/header "Content-Disposition" "attachment")))What middlewares are you using?
My guess is you have a middlewares that is pr-str or similar (maybe json encoding?) the stream so it becomes that not useful string before it reaches the code that actually handles it as a StreamableResponseBody
Aaahh, that might be it
I'll give it a look, thanks!
Huh, the problem only happens when I try to use the range headers in the browser. I'm probably doing a bad implementation. No idea why it ends up downloading as an ASCII though.
Also I'm realizing range headers should only be returned if the client asks for a range