Question

I'm trying to use a custom :store option for wrap-multipart-params and instead I'm clearly getting the default store. My custom function isn't even being called.

(mp/wrap-multipart-params 
   (POST "/upload-x" request (upload/upload-file request))
   {:store upload/logging-store})

My logging store function looks like this (it's just a dummy for now - eventually I want to handle the stream in a custom way) None of that IO happens.

(defn logging-store [{filename     :filename
                      content-type :content-type
                      stream       :stream
                      :as params}]
  (println "in logging store")
  (pprint filename)
  (pprint params)
  filename)

upload-file looks like this:

(defn upload-file [{params :params
                    session :session :as request}]
  (let [user-id (:user-id session)
        files (get params "files")]
    (pprint request)
    (pprint params)
    (response/response
     {:status    :success})))

The printing for the request and the params clearly show the multipart params in there and that they are being handled by the temp file store:

 :multipart-params
 {"files"
  {:size 1674,
   :tempfile
   #<File /var/folders/rx/9ntjyyvs35qbmcbp6rhfmj200000gn/T/ring-multipart-3853352501927893381.tmp>,
   :content-type "application/octet-stream",
   :filename "blog-test.clj"}},

EDIT: app definition (as requested)

(defroutes file-list-routes
  (GET "/simple-upload" request (upload/simple-upload-file request))
  (mp/wrap-multipart-params 
   (POST "/upload-x"        request (upload/upload-file    request))
   {:store upload/logging-store})
  )


(defroutes scratch-app
  (context "/files" request file-list-routes)
  (route/resources "/")
  (route/not-found "Page not found"))

(def my-app
  (handler/site
   (ring.middleware.json/wrap-json-response
    (ring.middleware.json/wrap-json-params
     (ring.middleware.stacktrace/wrap-stacktrace
      (ring.middleware.session/wrap-session 
       scratch-app
       {:store (ring.middleware.session.memory/memory-store all-the-sessions)
        :cookie-attrs {:max-age 3600 ;in s 3600s = 1h
                       }}))))))

(ring.util.servlet/defservice my-app)
Was it helpful?

Solution

The compojure.handler/site function contains the wrap-multipart-params middleware, so you're unknowingly applying the multipart middleware twice: first with the defaults, then with your custom options. Because the multipart middleware with the default options is applied first, that take priority.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top