问题描述
我有以下路线定义:
(require '[compojure.core :as ccore]
'[ring.util.response :as response])
(def *main-routes*
(ccore/defroutes avalanche-routes
(ccore/GET "/" [] "Hello World 2")
(ccore/GET "/images/:id" [id] (get-image-response id))))
在此示例中,请求 /
工作方式类似于charm并返回预期的 Hello World 2
In this example, requesting
/
works like a charm and returns the expected Hello World 2
.
get-images-response方法定义如下:
The get-images-response method is defined like this:
(defn get-image-response
[id]
(let [record (db/get-image id false)]
(-> (response/response (:data record))
(response/content-type (:content-type record))
(response/header "Content-Length" (:size record)))))
我得到一个404,所以二进制文件的服务还不工作。任何想法为什么?
I get a 404 though, so the serving of binary files doesn't quite work yet. Any ideas why?
编辑:
确定,问题与在 / images / name上请求图像的事实有关。 jpg
。一旦我删除 .jpg
,调用处理程序。所以,问题是我如何匹配除了扩展之外的任何东西?
Ok, the issue relates to the fact that images are being requested on
/images/name.jpg
. As soon as I remove the .jpg
the handler gets called. So the question becomes how do I match on anything but the extension?
推荐答案
这种情况下的真正的答案是, clojure-couchdb库中的错误。该修补程序可从获取。
The real answer in this case was that there was a bug in the clojure-couchdb library. The patch is available on github here.
它通过添加{:as:byte-array}映射参数和值到通过clj-http发送到couch的api的请求。
It boils down to adding the {:as :byte-array} map parameter and value to the request sent via clj-http to couch's api.
我的代码中的另一个问题是
ring
真的不知道在渲染它们时如何处理字节数组。而不是修补环,我只是将字节数组包装到 java.io.ByteArrayInputStream
。这是处理下载的完整代码:
The other issue in my code was that
ring
doesn't really know what to do with byte-arrays when it's rendering them. Rather than patching ring, I just wrapped the byte-array into a java.io.ByteArrayInputStream
. Here is the complete code for handling the download:
(defn get-image-response
[id]
(let [record (db/get-image id false)]
(-> (response/response (new java.io.ByteArrayInputStream (:data record)))
(response/content-type (:content-type (:content-type record)))
(response/header "Content-Length" (:size record)))))
这篇关于使用compojure从数据库提供二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!