Skip to content

Instantly share code, notes, and snippets.

@Kah0ona
Created March 8, 2016 15:32
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Kah0ona/9c52f4932cc4de4219ae to your computer and use it in GitHub Desktop.
this fn donwloads the file
(defn make-file-stream
"Takes an input stream and streams it"
[file]
(io/piped-input-stream
(fn [output-stream]
(.writeTo file output-stream))))
(defn download-file-by-path
"Downloads the requested file, ie. streams it to the client"
[file-path file-name content-type]
(let [content-length (.length (cio/file file-path))
resp (-> file-path
make-file-stream
r/response
(#(r/header % "Content-Length" content-length))
(#(r/header % "Content-Disposition" ; to get the right default file-name
(str "attachment; filename=\"" file-name "\"")))
(#(r/header % "Content-Type" content-type)))]
resp))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment