yenu/src/clj/yenu/routes/core.clj

40 lines
1.3 KiB
Clojure
Raw Normal View History

2017-03-15 00:28:27 +01:00
(ns yenu.routes.core
(:require [yenu.layout :as layout]
[compojure.core :refer [defroutes GET POST]]
[ring.util.http-response :as response]
[ring.util.response :refer [redirect file-response]]
[yenu.helpers.images :as images]
[yenu.db.core :as db]))
2017-03-17 00:23:13 +01:00
(defn number-of-pages [images-on-page]
(int (Math/ceil
(/ (:count (db/get-image-count)) images-on-page))))
(defn index-page [current-page]
(let [image-count 5
offset (* (dec current-page) image-count)
pages (number-of-pages image-count)
images (db/get-all-images {:offset offset :count image-count})]
(layout/render "index.html"
{:images images
:current-page current-page
:pages (range 1 (inc pages))})))
2017-03-15 00:28:27 +01:00
(defn image-file [type hash ext]
(let [filename (str hash "." ext)]
(file-response (images/data-path "gallery" type filename))))
(defroutes core-routes
(GET "/" []
2017-03-17 00:23:13 +01:00
(redirect "/page/1"))
(GET "/page/:page-number" [page-number]
(index-page (Integer. page-number)))
2017-03-15 00:28:27 +01:00
(GET ["/images/:type/:hash.:ext"
;;:type #"(normal|raw|thumbnails)"
;;:hash #"[0-9]+-[^.]+"
;;:ext #"(png|jpg)"
]
[type hash ext]
(image-file type hash ext)))