gpt4 book ai didi

clojure - 使用自定义 :store with wrap-multipart-params in ring middle ware

转载 作者:行者123 更新时间:2023-12-04 00:08:30 25 4
gpt4 key购买 nike

我正在尝试为 wrap-multipart-params 使用自定义 :store 选项,但显然我得到的是默认存储。我的自定义函数甚至没有被调用。

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

上传文件如下所示:

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

最佳答案

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

关于clojure - 使用自定义 :store with wrap-multipart-params in ring middle ware,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22792577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com