diff --git a/resources/templates/upload.html b/resources/templates/upload.html new file mode 100644 index 0000000..53e2e8e --- /dev/null +++ b/resources/templates/upload.html @@ -0,0 +1,6 @@ +

Upload image

+
+ {% csrf-field %} + + +
diff --git a/src/clj/yenu/handler.clj b/src/clj/yenu/handler.clj index 613677f..3bcd99b 100644 --- a/src/clj/yenu/handler.clj +++ b/src/clj/yenu/handler.clj @@ -1,7 +1,7 @@ (ns yenu.handler (:require [compojure.core :refer [routes wrap-routes]] [yenu.layout :refer [error-page]] - [yenu.routes.home :refer [home-routes]] + [yenu.routes.home :refer [home-routes upload-routes]] [compojure.route :as route] [yenu.env :refer [defaults]] [mount.core :as mount] @@ -13,13 +13,16 @@ (def app-routes (routes - (-> #'home-routes - (wrap-routes middleware/wrap-csrf) - (wrap-routes middleware/wrap-formats)) - (route/not-found - (:body - (error-page {:status 404 - :title "page not found"}))))) + (-> #'home-routes + (wrap-routes middleware/wrap-csrf) + (wrap-routes middleware/wrap-formats)) + (-> #'upload-routes + (wrap-routes middleware/wrap-csrf) + (wrap-routes middleware/wrap-formats)) + (route/not-found + (:body + (error-page {:status 404 + :title "page not found"}))))) (defn app [] (middleware/wrap-base #'app-routes)) diff --git a/src/clj/yenu/helpers/images.clj b/src/clj/yenu/helpers/images.clj index 88bebb9..59b02bb 100644 --- a/src/clj/yenu/helpers/images.clj +++ b/src/clj/yenu/helpers/images.clj @@ -9,13 +9,6 @@ [clj-exif-orientation.core :as exif] [yenu.config :refer [env]])) -(defn create-needed-folders [] - (if (not (fs/exists? (data-path "to-process"))) - (fs/mkdirs (data-path "to-process"))) - (if (not (fs/exists? (data-path "gallery"))) - (map #(fs/mkdirs (data-path "gallery" %)) - ["normal" "raw" "thumbnails"]))) - ;;; HINT: To use the env function, the webserver need to be started with ;;; (start) in the 'user namespace. (defn data-path @@ -24,6 +17,13 @@ (let [postfix (apply io/file args)] (.getPath (io/file (env :user-dir) "data" postfix))))) +(defn create-needed-folders [] + (if (not (fs/exists? (data-path "to-process"))) + (fs/mkdirs (data-path "to-process"))) + (if (not (fs/exists? (data-path "gallery"))) + (map #(fs/mkdirs (data-path "gallery" %)) + ["normal" "raw" "thumbnails"]))) + (defn target-image-filename [image-file-path] (let [file (io/file image-file-path)] (str (.lastModified file) "-" (digest/md5 file)))) diff --git a/src/clj/yenu/routes/home.clj b/src/clj/yenu/routes/home.clj index e44c21a..ab9453d 100644 --- a/src/clj/yenu/routes/home.clj +++ b/src/clj/yenu/routes/home.clj @@ -1,16 +1,37 @@ (ns yenu.routes.home (:require [yenu.layout :as layout] - [compojure.core :refer [defroutes GET]] + [compojure.core :refer [defroutes GET POST]] [ring.util.http-response :as response] - [clojure.java.io :as io])) + [ring.util.response :refer [redirect]] + [clojure.java.io :as io] + [yenu.helpers.images :as images]) + (:import [java.io File FileInputStream FileOutputStream])) -(defn home-page [] - (layout/render "home.html")) +(defn decoded-url-file [filepath] + (java.net.URLDecoder/decode filepath "utf-8")) + +(defn upload-file [{:keys [tempfile size filename]}] + (let [target-filepath (decoded-url-file (images/data-path "to-process" filename))] + (try + (with-open [in (new FileInputStream tempfile) + out (new FileOutputStream target-filepath)] + (let [src (.getChannel in) + dst (.getChannel out)] + (.transferFrom dst src 0 (.size src)) + (.flush out)))) + target-filepath)) + +(defroutes upload-routes + (GET "/upload" [] + (layout/render "upload.html")) + (POST "/upload" [file] + (-> (upload-file file) (images/process-image)) + (redirect "/"))) (defroutes home-routes (GET "/" [] - (home-page)) + (layout/render "home.html")) (GET "/docs" [] (-> (response/ok (-> "docs/docs.md" io/resource slurp)) - (response/header "Content-Type" "text/plain; charset=utf-8")))) + (response/header "Content-Type" "text/plain; charset=utf-8"))))