Skip to content

Commit

Permalink
Add special almost static handlers that return generated assets
Browse files Browse the repository at this point in the history
using the title of the document
  • Loading branch information
egli committed Oct 28, 2024
1 parent 743095e commit e227859
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/clj/daisyproducer2/routes/services.clj
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
(let [product-id (str/replace name #".epub$" "")]
(when (s/valid? ::product-number product-id)
(fs/copy path spool-dir {:replace-existing true}))))
(let [url (str "/download/" name)]
(let [url (format "/download/%s/epub/%s" id name)]
(created url {:location url})))
(catch clojure.lang.ExceptionInfo e
(log/error (ex-message e))
Expand Down Expand Up @@ -363,7 +363,8 @@
(if-let [doc (documents/get-document id)]
(try
(let [[name _] (preview/sbsform doc opts)
url (str "/download/" name)]
contraction (:contraction opts)
url (format "/download/%s/braille/%s/%s" id contraction name)]
(created url {:location url}))
(catch clojure.lang.ExceptionInfo e
(log/error (ex-message e))
Expand All @@ -390,7 +391,8 @@
(if-let [doc (documents/get-document id)]
(try
(let [[name _] (preview/large-print id opts)
url (str "/download/" name)]
font-size (or (:font-size opts) 17)
url (format "/download/%s/large-print/%s/%s" id font-size name)]
(created url {:location url}))
(catch clojure.lang.ExceptionInfo e
(log/error (ex-message e))
Expand All @@ -415,7 +417,7 @@
(if-let [doc (documents/get-document id)]
(try
(let [[name _] (preview/open-document id opts)
url (str "/download/" name)]
url (format "/download/%s/open-document/%s" id name)]
(created url {:location url}))
(catch clojure.lang.ExceptionInfo e
(log/error (ex-message e))
Expand Down
56 changes: 51 additions & 5 deletions src/clj/daisyproducer2/routes/static.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
(ns daisyproducer2.routes.static
(:require
[daisyproducer2.config :refer [env]]
[reitit.ring :as ring]))
[daisyproducer2.documents.documents :as documents]
[reitit.coercion.spec :as spec-coercion]
[reitit.ring :as ring]
[reitit.ring.coercion :as coercion]
[reitit.ring.middleware.parameters :as parameters]
[ring.util.http-response :as response]))

(defn archive-routes
"Route to serve static assets such as DTBook XML of versions and images directly from the file system."
Expand All @@ -10,9 +15,50 @@
(ring/create-file-handler
{:root (env :document-root)})])

(defn- content-response [file name]
(let [content-disposition (format "attachment; filename=\"%s\"" name)]
(-> file
(response/file-response {:root (env :spool-dir)})
(response/header "Content-Disposition" content-disposition))))

(defn- created-assets-handler [name-fn]
(fn [{{{:keys [id file] :as params} :path} :parameters}]
(if-let [doc (documents/get-document id)]
(let [title (:title doc)
name (name-fn title params)]
(content-response file name))
(response/not-found))))

(defn created-assets-routes
"Route to serve generated assets such as EPUBs, SBSform, PDFs or ODT files directly from the file system."
"Route to serve generated assets such as EPUBs, SBSform, PDFs or ODT files."
[]
["/download/*"
(ring/create-file-handler
{:root (env :spool-dir)})])
["/download/:id"
{:coercion spec-coercion/coercion
:middleware [;; query-params & form-params
parameters/parameters-middleware
;; coercing request parameters
coercion/coerce-request-middleware]}
["/epub/:file" {:parameters {:path {:id int?
:file string?}}
:handler (created-assets-handler
(fn [title _] (format "%s.epub" title)))}]

["/braille/:contraction/:file" {:parameters {:path {:id int?
:contraction int?
:file string?}}
:handler (created-assets-handler
(fn [title {:keys [contraction]}]
(format "%s.%s" title (if (= contraction 2) "bk" "bv"))))}]

["/large-print/:font-size/:file" {:parameters {:path {:id int?
:font-size int?
:file string?}}
:handler (created-assets-handler
(fn [title {:keys [font-size]}]
(format "%s %spt.pdf" title font-size)))}]

["/open-document/:file" {:parameters {:path {:id int?
:file string?}}
:handler (created-assets-handler
(fn [title _] (format "%s.odt" title)))}]])

0 comments on commit e227859

Please sign in to comment.