yenu/src/clj/yenu/routes/home.clj

38 lines
1.3 KiB
Clojure
Raw Normal View History

(ns yenu.routes.home
(:require [yenu.layout :as layout]
2017-02-27 23:31:53 +01:00
[compojure.core :refer [defroutes GET POST]]
[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-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 "/")))
(defroutes home-routes
(GET "/" []
2017-02-27 23:31:53 +01:00
(layout/render "home.html"))
(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"))))