diff options
author | Tim <contact@bytim.eu> | 2025-04-21 18:08:13 +0200 |
---|---|---|
committer | Tim <contact@bytim.eu> | 2025-04-21 18:08:13 +0200 |
commit | 9cbd9b9edbbdaf7835bcf3e501bc9c0fbe90504d (patch) | |
tree | c0de0b297a1dc1609a7b19e66a46a90b40d94ebe | |
parent | b40c0325abd6a0952948dd266d7409af557fc0e7 (diff) | |
download | pics-9cbd9b9edbbdaf7835bcf3e501bc9c0fbe90504d.tar.xz pics-9cbd9b9edbbdaf7835bcf3e501bc9c0fbe90504d.zip |
Add image view
-rw-r--r-- | TODO.md | 2 | ||||
-rw-r--r-- | deps.edn | 3 | ||||
-rw-r--r-- | src/pics/handler/view.clj | 46 | ||||
-rw-r--r-- | src/pics/routes.clj | 7 |
4 files changed, 54 insertions, 4 deletions
@@ -1,5 +1,5 @@ # ToDo - [x] upload image -- [ ] view image +- [x] view image - [ ] delete image @@ -1,7 +1,8 @@ {:paths ["src"] :deps {http-kit/http-kit {:mvn/version "2.8.0"} metosin/reitit {:mvn/version "0.8.0"} - lynxeyes/dotenv {:mvn/version "1.1.0"}} + lynxeyes/dotenv {:mvn/version "1.1.0"} + hiccup/hiccup {:mvn/version "2.0.0-RC5"}} :aliases {:neil {:project {:name pics/pics :version "0.0.1"}} diff --git a/src/pics/handler/view.clj b/src/pics/handler/view.clj new file mode 100644 index 0000000..34ee712 --- /dev/null +++ b/src/pics/handler/view.clj @@ -0,0 +1,46 @@ +(ns pics.handler.view + (:require [clojure.string :as cstr] + [ring.util.response :as ruresp] + [hiccup2.core :as hiccup]) + (:import java.io.File + java.util.Date + (java.text SimpleDateFormat))) + +(defn handle-src [req] + (->> (get-in req [:reitit.core/match :path-params :file-name]) + (str "./img/") + ruresp/file-response)) + +(defn handle [req] + (let [file (->> (.listFiles (File. "./img/")) + (filter #(cstr/starts-with? (.getName ^File %) + (str (get-in req [:reitit.core/match :path-params :id]) "."))) + first)] + (if (some? file) + (-> [:html + [:head + [:title "pics"] + [:meta {:name "robots" :content "noindex,nofollow"}]] + [:body {:style {:background-color :aquamarine}} + [:img {:src (str "/src/" (.getName ^File file)) + :style {:max-height "90%" + :max-width "90%" + :border-radius "1em" + :display :block + :margin :auto}}] + (let [last-modified (Date. (.lastModified ^File file)) + date-format (SimpleDateFormat. "dd.MM.yyyy")] + [:div {:style {:position :fixed + :bottom 0 + :text-align :center + :width "100%"}} + [:p {:style {:font-family "Arial, sans-serif" + :background-color :white + :border-radius "1em" + :display :inline-block + :padding "0.3em"}} + (str "Uploaded on " (.format date-format last-modified))]])]] + hiccup/html + str + ruresp/response) + (ruresp/not-found "Not found.")))) diff --git a/src/pics/routes.clj b/src/pics/routes.clj index a10ab95..31edd63 100644 --- a/src/pics/routes.clj +++ b/src/pics/routes.clj @@ -3,11 +3,14 @@ [ring.middleware.multipart-params :as rmmultiparams] [pics.handler.home :as phhome] - [pics.handler.upload :as phupload])) + [pics.handler.upload :as phupload] + [pics.handler.view :as phview])) (def ^:private router (rring/router [["/" {:get {:handler phhome/handle}}] - ["/upload" {:post {:handler phupload/handle}}]])) + ["/api/upload" {:post {:handler phupload/handle}}] + ["/src/:file-name" {:get {:handler phview/handle-src}}] + ["/:id" {:get {:handler phview/handle}}]])) (def ring-handler (-> router |