Fork me on GitHub
#ring
<
2018-04-17
>
stvnmllr200:04:00

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

alice00:04:50

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

stvnmllr200:04:08

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

stvnmllr200:04:18

so people can download their data from an app

alice00:04:24

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

alice00:04:23

(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 #"\."))))

stvnmllr200:04:21

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

alice00:04:46

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