aboutsummaryrefslogtreecommitdiff
path: root/src/pics/handler/view.clj
blob: 2fd7710aa48c02346ec2e6d9d9b40ffe6dcb4636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
(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 [id (get-in req [:reitit.core/match :path-params :id])
        file (->> (.listFiles (File. "./img/"))
                  (filter #(cstr/starts-with? (.getName ^File %)
                                              (str id ".")))
                  first)]
    (if (some? file)
      (-> (let [last-modified (Date. (.lastModified ^File file))
                date-format (SimpleDateFormat. "dd.MM.yyyy")
                date-string (.format date-format last-modified)
                server-url (str "http://" (:server-name req) (when (not= (:server-port req) 80)
                                                               (str ":" (:server-port req))))
                src-url (str server-url "/src/" (.getName ^File file))]
            [:html
             [:head
              [:title (str "pics - upload from " date-string)]
              [:meta {:name "robots" :content "noindex,nofollow"}]

              [:meta {:property "og:title" :content "You received a Screenshot!"}]
              [:meta {:property "og:description" :content (str "Uploaded on " date-string)}]
              [:meta {:property "og:url" :content src-url}]
              [:meta {:property "og:type" :content "website"}]
              [:meta {:property "og:image" :content src-url}]]
             [: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}}]
              [: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 " date-string)]]]])
          hiccup/html
          str
          ruresp/response)
      (ruresp/not-found "Not found."))))