I've got the following function in webnoir that serves up an image from disk.
(defpage [:get "/GetPhoto"] {:keys [photoName]}
(def file (io/input-stream (str photoName ".jpg")))
(resp/set-headers {"Content-Type" "image/jpeg"} file))
I assume I should close the file, but when I add (.close file)
to the end of the function, I get an error java.io.IOException: Stream closed
when accessing the URL. What's the right way to handle this?
Obviously you can't close the file at the end of the function, because all you do with noir/ring is to return a data structure describing the response. Ring then uses this data structure to actually respond to the client. In this case it sees a stream and tries to read from it. When you close it (explicit or with with-open
) this will fail.
I would expect you don't have to do anything and ring will close the stream after exhausting it.
You don't want nested def
. You don't want nested def
. You don't want nested def
. You don't want...