From e2278599615c15dc4824c8b7aac4c3f1c8d71dc4 Mon Sep 17 00:00:00 2001 From: Christian Egli Date: Mon, 28 Oct 2024 16:46:27 +0100 Subject: [PATCH] Add special almost static handlers that return generated assets using the title of the document --- src/clj/daisyproducer2/routes/services.clj | 10 ++-- src/clj/daisyproducer2/routes/static.clj | 56 ++++++++++++++++++++-- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/clj/daisyproducer2/routes/services.clj b/src/clj/daisyproducer2/routes/services.clj index 0ae3f01..11d9b5e 100644 --- a/src/clj/daisyproducer2/routes/services.clj +++ b/src/clj/daisyproducer2/routes/services.clj @@ -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)) @@ -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)) @@ -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)) @@ -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)) diff --git a/src/clj/daisyproducer2/routes/static.clj b/src/clj/daisyproducer2/routes/static.clj index cf12ba6..6262cdd 100644 --- a/src/clj/daisyproducer2/routes/static.clj +++ b/src/clj/daisyproducer2/routes/static.clj @@ -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." @@ -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)))}]]) +