ring

stvnmllr2 2018-04-17T00:13:00.000035Z

anyone know a simple example posted online for downloading file stream? Like, generating a csv file for example. Or serving any file really.

alice 2018-04-17T00:15:50.000260Z

@stvnmllr2 You want to serve a filestream, do I understand correctly?

stvnmllr2 2018-04-17T00:16:08.000323Z

well.. any stream. Or just a file if I have to.

stvnmllr2 2018-04-17T00:16:18.000076Z

so people can download their data from an app

stvnmllr2 2018-04-17T00:16:21.000161Z

web app

alice 2018-04-17T00:18:24.000010Z

I'm serving images from a GridFS db in clojure like this, gimme a sec

alice 2018-04-17T00:19:23.000064Z

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


(defn download-file-by-id
  "Downloads the requested file with `md5`"
  [md5]
  (let [mongo-file   (first (db/get-file-md5 md5))
        file-map     (db/find-map-by-md5 md5)
        content-type (-> file-map :contentType)
        file-name    (-> file-map :filename)]
    (res/content-type {:status 200 :body (make-file-stream mongo-file)} content-type)))

;; then in my routes
  (GET "/i/:md5" [md5] (download-file-by-id (first (string/split md5 #"\."))))

stvnmllr2 2018-04-17T00:22:21.000336Z

@alice. Closer than anything else I've found. Thanks! Will give it a go.

alice 2018-04-17T00:22:46.000106Z

Keep in mind I only have to pipe the stream because I originate with an input stream