Use wrapper for yada/resource

next
Marcin Kulik 8 years ago
parent 3424505587
commit bb4c602a4c

@ -7,6 +7,7 @@
[user-database :as udb]] [user-database :as udb]]
[asciinema.model.asciicast :as asciicast] [asciinema.model.asciicast :as asciicast]
[asciinema.util.io :refer [with-tmp-dir]] [asciinema.util.io :refer [with-tmp-dir]]
[asciinema.yada :refer [resource]]
[clj-time.core :as t] [clj-time.core :as t]
[clojure.java [clojure.java
[io :as io] [io :as io]
@ -53,49 +54,51 @@
(service-unavailable-response ctx))) (service-unavailable-response ctx)))
(defn asciicast-json-resource [db file-store] (defn asciicast-json-resource [db file-store]
(yada/resource {:produces "application/json" (resource
:parameters {:path {:token String} {:produces "application/json"
:query {(s/optional-key :dl) s/Bool}} :parameters {:path {:token String}
:properties (fn [ctx] :query {(s/optional-key :dl) s/Bool}}
(if-let [asciicast (adb/get-asciicast-by-token db (-> ctx :parameters :path :token))] :properties (fn [ctx]
{:exists? true (if-let [asciicast (adb/get-asciicast-by-token db (-> ctx :parameters :path :token))]
::asciicast asciicast} {:exists? true
{:exists? false})) ::asciicast asciicast}
:response (fn [ctx] {:exists? false}))
(let [asciicast (-> ctx :properties ::asciicast) :response (fn [ctx]
dl (-> ctx :parameters :query :dl) (let [asciicast (-> ctx :properties ::asciicast)
path (asciicast/json-store-path asciicast) dl (-> ctx :parameters :query :dl)
filename (str "asciicast-" (:id asciicast) ".json")] path (asciicast/json-store-path asciicast)
(fstore/serve-file file-store ctx path (when dl {:filename filename}))))})) filename (str "asciicast-" (:id asciicast) ".json")]
(fstore/serve-file file-store ctx path (when dl {:filename filename}))))}))
(defn asciicast-png-resource [db file-store exp-set executor] (defn asciicast-png-resource [db file-store exp-set executor]
(yada/resource {:produces "image/png" (resource
:parameters {:path {:token String} {:produces "image/png"
:query {(s/optional-key :time) s/Num :parameters {:path {:token String}
(s/optional-key :theme) Theme :query {(s/optional-key :time) s/Num
(s/optional-key :scale) (s/enum "1" "2")}} (s/optional-key :theme) Theme
:properties (fn [ctx] (s/optional-key :scale) (s/enum "1" "2")}}
(if-let [asciicast (adb/get-asciicast-by-token db (-> ctx :parameters :path :token))] :properties (fn [ctx]
(let [user (udb/get-user-by-id db (:user_id asciicast)) (if-let [asciicast (adb/get-asciicast-by-token db (-> ctx :parameters :path :token))]
{:keys [time theme scale]} (-> ctx :parameters :query) (let [user (udb/get-user-by-id db (:user_id asciicast))
png-params (cond-> (asciicast/png-params asciicast user) {:keys [time theme scale]} (-> ctx :parameters :query)
time (assoc :snapshot-at time) png-params (cond-> (asciicast/png-params asciicast user)
theme (assoc :theme theme) time (assoc :snapshot-at time)
scale (assoc :scale (Integer/parseInt scale)))] theme (assoc :theme theme)
{:exists? true scale (assoc :scale (Integer/parseInt scale)))]
:version (asciicast/png-version asciicast png-params) {:exists? true
::asciicast asciicast :version (asciicast/png-version asciicast png-params)
::png-params png-params}) ::asciicast asciicast
{:exists? false})) ::png-params png-params})
:response (fn [ctx] {:exists? false}))
(let [asciicast (-> ctx :properties ::asciicast) :response (fn [ctx]
png-params (-> ctx :properties ::png-params) (let [asciicast (-> ctx :properties ::asciicast)
png-store-path (asciicast/png-store-path asciicast png-params)] png-params (-> ctx :properties ::png-params)
(if (exp-set/contains? exp-set png-store-path) png-store-path (asciicast/png-store-path asciicast png-params)]
(fstore/serve-file file-store ctx png-store-path {}) (if (exp-set/contains? exp-set png-store-path)
(async-response ctx executor (fn [] (fstore/serve-file file-store ctx png-store-path {})
(generate-png file-store exp-set asciicast png-params png-store-path) (async-response ctx executor (fn []
(fstore/serve-file file-store ctx png-store-path {}))))))})) (generate-png file-store exp-set asciicast png-params png-store-path)
(fstore/serve-file file-store ctx png-store-path {}))))))}))
(defn asciicasts-endpoint [{:keys [db file-store exp-set executor]}] (defn asciicasts-endpoint [{:keys [db file-store exp-set executor]}]
["" [["/a/" [[[:token ".json"] (asciicast-json-resource db file-store)] ["" [["/a/" [[[:token ".json"] (asciicast-json-resource db file-store)]

@ -0,0 +1,5 @@
(ns asciinema.yada
(:require [yada.yada :as yada]))
(defn resource [model]
(yada/resource model))
Loading…
Cancel
Save