I have the following code, in which I want to send an InputStream of a file in the function fetch-items
, which handles the route /fetch-items.
(defn id->image [image-id]
(let [image (.getInputStream (gfs/find-by-id fs image-id))] image))
(defn item-resp [item]
(assoc item :_id (str (:_id item))
:images (into [] (map id->image (:image-ids item))))
)
(defn fetch-items [req]
(res/response
(map item-resp (find fs "items" {}))))
Here's my request in the client side, using cljs-ajax:
(ajax-request
{:uri "http://localhost:5000/fetch-items"
:method :get
:handler #(prn (into [] %))
:format (json-request-format)
:response-format (raw-response-format)
}
)
But the response I get on the client is this:
[:failure :parse] [:response nil] [:status-text "No reader function for tag object. Format should have been EDN"]
:original-text "{:_id \"5e63f5c591585c30985793cd\", :images [#object[com.mongodb.gridfs.GridFSDBFile$GridFSInputStream 0x22556652 \"com.mongodb.gridfs.GridFSDBFile$GridFSInputStream@22556652\"]]}{:_id \"5e63f5d891585c30985793d0\", :images [#object[com.mongodb.gridfs.GridFSDBFile$GridFSInputStream 0x266ae6c0 \"com.mongodb.gridfs.GridFSDBFile$GridFSInputStream@266ae6c0\"]]}{:_id \"5e63f5e891585c30985793d3\", ...
Why would the response say that the format should have been edn? How do I extract this file/image out in the client side?
--- EDIT ----
Doing the following:
(IOUtils/toString image "utf-8")
returns a string of size 1594 bytes, which is much smaller than the expected image size.
I think this is because it's converting the file object to base64 and not the actual chunk of data associated with it.
How do I make it convert the actual GridFS chunk to base64 string and not the file object?