使用自定义:在环形中间件中使用wrap-multipart-params存储

时间:2014-04-01 17:17:21

标签: clojure compojure ring

我正在尝试为:store使用自定义wrap-multipart-params选项,而我显然正在获取默认商店。我的自定义函数甚至没有被调用。

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

我的日志存储功能看起来像这样(现在它只是一个假人 - 最终我想以自定义方式处理流)这些IO都不会发生。

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

upload-file如下所示:

(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})))

请求和参数的打印清楚地显示了那里的多部分参数,并且它们正由临时文件存储处理:

 :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"}},

编辑:应用程序定义(根据要求)

(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)

1 个答案:

答案 0 :(得分:7)

compojure.handler/site函数包含wrap-multipart-params中间件,因此您不知不觉地将多部分中间件应用两次:首先使用默认值,然后使用自定义选项。因为首先应用具有默认选项的多部分中间件,所以优先考虑。