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

53 lines
1.9 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]
2017-03-04 00:43:17 +01:00
[yenu.helpers.images :as images]
[yenu.db.core :as db]
[clojure.string :as str])
2017-02-27 23:31:53 +01:00
(: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))
2017-03-04 00:43:17 +01:00
(defn add-tag [image-id tag]
(db/create-tag! {:tagname tag})
(let [{tag-id :id} (db/get-tag {:tagname tag})]
(db/add-tag-to-image! {:imageid image-id :tagid tag-id})))
(defn add-to-database [filehash title description tags]
(let [img (db/create-image! {:hash filehash :title title :description description})
image-id ((keyword "last_insert_rowid()") img)
all-tags (str/split tags, #"(\s*,\s*|\s+)")]
(map #(add-tag image-id %) all-tags)))
2017-02-27 23:31:53 +01:00
(defroutes upload-routes
(GET "/upload" []
(layout/render "upload.html"))
2017-03-04 00:43:17 +01:00
(POST "/upload" [file title description tags]
(-> (upload-file file)
(images/process-image)
(add-to-database title description tags))
2017-02-27 23:31:53 +01:00
(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"))))