2017-02-16 22:31:16 +01:00
|
|
|
(ns yenu.routes.home
|
|
|
|
(:require [yenu.layout :as layout]
|
2017-02-27 23:31:53 +01:00
|
|
|
[compojure.core :refer [defroutes GET POST]]
|
2017-02-16 22:31:16 +01:00
|
|
|
[ring.util.http-response :as response]
|
2017-02-27 23:31:53 +01:00
|
|
|
[ring.util.response :refer [redirect]]
|
|
|
|
[clojure.java.io :as io]
|
|
|
|
[yenu.helpers.images :as images])
|
|
|
|
(:import [java.io File FileInputStream FileOutputStream]))
|
2017-02-16 22:31:16 +01:00
|
|
|
|
2017-02-27 23:31:53 +01:00
|
|
|
(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 "/")))
|
2017-02-16 22:31:16 +01:00
|
|
|
|
|
|
|
(defroutes home-routes
|
|
|
|
(GET "/" []
|
2017-02-27 23:31:53 +01:00
|
|
|
(layout/render "home.html"))
|
2017-02-16 22:31:16 +01:00
|
|
|
(GET "/docs" []
|
|
|
|
(-> (response/ok (-> "docs/docs.md" io/resource slurp))
|
2017-02-27 23:31:53 +01:00
|
|
|
(response/header "Content-Type" "text/plain; charset=utf-8"))))
|
2017-02-16 22:31:16 +01:00
|
|
|
|