Skip to content

Instantly share code, notes, and snippets.

@stijnopheide
Created August 30, 2017 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stijnopheide/7cf154f0fc7a7dd7ad7487a9ea0473e5 to your computer and use it in GitHub Desktop.
Save stijnopheide/7cf154f0fc7a7dd7ad7487a9ea0473e5 to your computer and use it in GitHub Desktop.
(defn create-tempfile
[suffix]
(let [f (File/createTempFile "yada-" suffix)]
(.deleteOnExit f)
f))
(defn save-body-to-file
[ctx body-stream media-type & args]
(let [temp-file (create-tempfile (str "." (second (str/split media-type #"/"))))]
(md/chain
(ms/reduce
(fn [^OutputStream acc ^bytes buf]
(do (.write acc buf)
acc))
(io/output-stream temp-file)
body-stream)
(fn [^OutputStream acc]
(.close acc)
(assoc-in ctx [:parameters :body] temp-file)))))
(defmethod process-request-body "application/jpg"
[ctx body-stream media-type & args]
(apply save-body-to-file ctx body-stream media-type args))
(defn upload-file
[]
(yada/resource
{:produces #{"application/json"}
:methods {:post
{:consumes #{"application/jpg" "application/jpeg" "application/png"}
:parameters {:body java.io.File}
:response (fn [ctx]
(do
(log/info (get-in ctx [:parameters :body]))
{:message "ok, yeah"}))}}}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment