Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

clojure

ring

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.

like image 491
danieltan95 Avatar asked Oct 22 '25 18:10

danieltan95


1 Answers

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))}))
like image 71
danieltan95 Avatar answered Oct 26 '25 05:10

danieltan95