Last active
January 4, 2016 18:49
-
-
Save dbushenko/8663692 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
(ns my.core | |
(:require ... | |
[ring.middleware [multipart-params :as mp]])) | |
(defn upload-picture [{:keys [tempfile content-type filename]}] | |
(let [ext (.substring content-type (inc (.lastIndexOf content-type "/"))) | |
id (:id (session/get :user))] | |
(if (or (nil? tempfile) | |
(nil? id)) | |
"<h3>Error while upload!</h3>" | |
(let [img-name (str "/userpics/" id "." ext) | |
fname (str "." img-name) | |
f (java.io.File. fname)] | |
(if (.exists f) | |
(.delete f)) | |
(io/copy tempfile (java.io.File. fname)) | |
(.delete tempfile) | |
(if (db/update-user {:id (:id (session/get :user)) | |
:image img-name}) | |
"<h3>File upload successful!</h3>" | |
"<h3>Error while updating user picture!</h3>"))))) | |
(defroutes app-routes | |
(mp/wrap-multipart-params | |
(POST "/user/picture" {params :params} (upload-picture (get params :file)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment