Fork me on GitHub
#ring
<
2024-07-15
>
frankitox23:07:36

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 ".BufferedInputStream@65b906f0"]. What's the proper way of doing this?

frankitox23:07:54

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 :thinking_face:

frankitox23:07:16

(I'm always confused by streams 🙃)

frankitox23:07:56

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")))

hiredman23:07:04

What middlewares are you using?

hiredman23:07:14

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

frankitox23:07:08

Aaahh, that might be it

frankitox23:07:27

I'll give it a look, thanks!

frankitox00:07:56

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.

frankitox00:07:32

Also I'm realizing range headers should only be returned if the client asks for a range