私はClojureでCompojureを使用して静的ファイルサーバーを作成していますが、ファイルシステムから画像を読み取り、Compojureルートを介してその画像を表示することに固執しています。
残念ながら、slurpはバイナリデータをうまく処理できません。それ以来、これを100の異なる方法で試しましたが、これは私の最近の失敗した試みです。
(defn image-output [filepath]
(try
(let [contents (apply str (with-open [rdr (io/reader filepath)]
(into #{} (line-seq rdr))))]
{
:status 200
:headers
{
"Content-Type" "image/jpeg",
"Content-Length" "",
"Cache-Control" "",
"Expires" ""
}
:body contents
}))
(catch Exception e {:status 404})))
(defn endpoint_view [params]
(if (contains? params :bucket)
(image-output (join "/" [data_path (:bucket params) (:dir params) (:filename params)]))))
(defroutes main-routes
(GET "/view/:bucket/:dir/:filename" {params :params} (endpoint_view params))
(route/files "/")
(route/resources "/s" {:root "./public/s"})
(route/not-found "Page not found"))
この現在の試みは、コンテンツ文字列とそのエンコードされた文字列をエコーできるslurpを使用するのと同じ運命をたどっているようですが、content-typeをimage / jpegに変更すると、壊れた画像になります。
私は昨日Google検索を一日中過ごしましたが、同じ目標を達成した例はありませんでした。JavaIOについてもう少し理解するのに役立ちましたが、必要な場所にたどり着くのに十分なほど明確ではありませんでした。私がすでに得ていたのと同じ結果(例:ファイルの内容をClojureのセットに読み込む最良の方法)。
(ファイルパスからコンテンツタイプを取得する方法を教えていただければ、想像上のボーナスポイントです。それが私の次の質問です!)