Implement the upload form
This commit is contained in:
parent
5b2d7538ad
commit
4434700343
4 changed files with 51 additions and 21 deletions
6
resources/templates/upload.html
Normal file
6
resources/templates/upload.html
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<h2>Upload image</h2>
|
||||||
|
<form action="/upload" enctype="multipart/form-data" method="POST">
|
||||||
|
{% csrf-field %}
|
||||||
|
<input id="file" name="file" type="file" />
|
||||||
|
<input type="submit" value="upload" />
|
||||||
|
</form>
|
|
@ -1,7 +1,7 @@
|
||||||
(ns yenu.handler
|
(ns yenu.handler
|
||||||
(:require [compojure.core :refer [routes wrap-routes]]
|
(:require [compojure.core :refer [routes wrap-routes]]
|
||||||
[yenu.layout :refer [error-page]]
|
[yenu.layout :refer [error-page]]
|
||||||
[yenu.routes.home :refer [home-routes]]
|
[yenu.routes.home :refer [home-routes upload-routes]]
|
||||||
[compojure.route :as route]
|
[compojure.route :as route]
|
||||||
[yenu.env :refer [defaults]]
|
[yenu.env :refer [defaults]]
|
||||||
[mount.core :as mount]
|
[mount.core :as mount]
|
||||||
|
@ -13,13 +13,16 @@
|
||||||
|
|
||||||
(def app-routes
|
(def app-routes
|
||||||
(routes
|
(routes
|
||||||
(-> #'home-routes
|
(-> #'home-routes
|
||||||
(wrap-routes middleware/wrap-csrf)
|
(wrap-routes middleware/wrap-csrf)
|
||||||
(wrap-routes middleware/wrap-formats))
|
(wrap-routes middleware/wrap-formats))
|
||||||
(route/not-found
|
(-> #'upload-routes
|
||||||
(:body
|
(wrap-routes middleware/wrap-csrf)
|
||||||
(error-page {:status 404
|
(wrap-routes middleware/wrap-formats))
|
||||||
:title "page not found"})))))
|
(route/not-found
|
||||||
|
(:body
|
||||||
|
(error-page {:status 404
|
||||||
|
:title "page not found"})))))
|
||||||
|
|
||||||
|
|
||||||
(defn app [] (middleware/wrap-base #'app-routes))
|
(defn app [] (middleware/wrap-base #'app-routes))
|
||||||
|
|
|
@ -9,13 +9,6 @@
|
||||||
[clj-exif-orientation.core :as exif]
|
[clj-exif-orientation.core :as exif]
|
||||||
[yenu.config :refer [env]]))
|
[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
|
;;; HINT: To use the env function, the webserver need to be started with
|
||||||
;;; (start) in the 'user namespace.
|
;;; (start) in the 'user namespace.
|
||||||
(defn data-path
|
(defn data-path
|
||||||
|
@ -24,6 +17,13 @@
|
||||||
(let [postfix (apply io/file args)]
|
(let [postfix (apply io/file args)]
|
||||||
(.getPath (io/file (env :user-dir) "data" postfix)))))
|
(.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]
|
(defn target-image-filename [image-file-path]
|
||||||
(let [file (io/file image-file-path)]
|
(let [file (io/file image-file-path)]
|
||||||
(str (.lastModified file) "-" (digest/md5 file))))
|
(str (.lastModified file) "-" (digest/md5 file))))
|
||||||
|
|
|
@ -1,16 +1,37 @@
|
||||||
(ns yenu.routes.home
|
(ns yenu.routes.home
|
||||||
(:require [yenu.layout :as layout]
|
(:require [yenu.layout :as layout]
|
||||||
[compojure.core :refer [defroutes GET]]
|
[compojure.core :refer [defroutes GET POST]]
|
||||||
[ring.util.http-response :as response]
|
[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 []
|
(defn decoded-url-file [filepath]
|
||||||
(layout/render "home.html"))
|
(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
|
(defroutes home-routes
|
||||||
(GET "/" []
|
(GET "/" []
|
||||||
(home-page))
|
(layout/render "home.html"))
|
||||||
(GET "/docs" []
|
(GET "/docs" []
|
||||||
(-> (response/ok (-> "docs/docs.md" io/resource slurp))
|
(-> (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"))))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue