0

I have a web app using Clojure, Clojurescript, and Monger. Documents are uploaded and stored in Mongo via GridFS. The same files can be requested for download; at the moment this is accomplished by writing the file to (the server's) disk and serving it as a static file, but this is somewhat awkward; how can I serve the file represented by the GridFS object directly in Clojure/Java? Routing is handled by Ring/Compojure.

4

1 回答 1

2

事实证明,Luminus 中使用的 Ring/Compojure 基础设施能够返回输出流,从而无需接触驱动器即可轻松传输文件。

(ns my-app.routes.updown
  "File uploading and insertion into database"
  (:use compojure.core)
  (:require [my-app.db.core :as db] ;; all the db functions you see here are just wrappers for the Monger functions you'd expect
            [ring.util.response :as r]
            [ring.util.io :as io]))

(defn make-file-stream
  "Takes an input stream--such as a MongoDBObject stream--and streams it"
  [file]  
  (io/piped-input-stream
   (fn [output-stream]
       (.writeTo file output-stream))))

(defn download-file-by-id "Downloads the requested file, if privileges are allowed"
  [id-string]
  (let [mongo-file (db/find-file-by-id id-string)
        file-map (db/map-from-mongo-obj mongo-file)
        content-type (-> file-map :metadata :contentType)
        file-name (-> file-map :filename)]
        (-> mongo-file
            make-file-stream
            r/response
            (#(r/header % "Content-Disposition" ; to get the right default file-name
                        (str "attachment; filename=\"" file-name "\"")))
        (#(r/header % "Content-Type" content-type))))) ; final wrapper: offer the right "open with" dialogue


;; this is called by my main routes def like so:
;; (notice no extra wrappers needed, as wrapping took place in download-file-by-id)
;; (defroutes home-routes
;;     (GET "/files/:id-string" [id-string]
;;        (up/download-file-by-id id-string)))
于 2015-11-04T00:02:50.003 回答