clojurering

How do you serve dynamically created files in clojure ring server?


I have a web server that saves files uploaded by the user to the native filesystem in the resources folder. It works fine in development but in production, the files can't be served.

I've tried wrap-file and wrap-resource, neither works.


Solution

  • Neither wrap-file nor wrap-resource will work because the dynamic content is served outside of the packaged jar but both middleware expects the item to be in the jar, as seen in the source.

    Instead, you can actually pass in an input stream of file object directly to the :body of the response! Ring will handle it properly and serve it normally, as seen in the source here.

    Example code:

    (defn image-handler 
      [request]
      (as-> request r
            (:path-params r)
            (:filename r)
            {:status  200
             :body (io/input-stream (str <your-base-path-here> r))}))