Created
August 30, 2017 18:54
-
-
Save stijnopheide/7cf154f0fc7a7dd7ad7487a9ea0473e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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