|
| 1 | +(ns nextjournal.cas-client.api |
| 2 | + (:require [clojure.java.io :as io] |
| 3 | + [clojure.string :as str] |
| 4 | + [nextjournal.cas-client.hashing :as h] |
| 5 | + [cheshire.core :as json] |
| 6 | + [babashka.fs :as fs] |
| 7 | + [org.httpkit.client :as http] |
| 8 | + [org.httpkit.sni-client :as sni-client] |
| 9 | + [ring.util.codec :as ring-codec])) |
| 10 | + |
| 11 | +;; Change default client for the whole application: |
| 12 | +;; Needed for TLS connections using SNI |
| 13 | +(alter-var-root #'org.httpkit.client/*default-client* (fn [_] sni-client/default-client)) |
| 14 | + |
| 15 | +(defonce ^:dynamic *cas-host* "https://cas.clerk.garden") |
| 16 | +(defonce ^:dynamic *tags-host* "https://storage.clerk.garden") |
| 17 | + |
| 18 | +(defn tag-put [{:keys [host auth-token namespace tag target] |
| 19 | + :or {host *tags-host*}}] |
| 20 | + (-> @(http/post (str host "/" namespace "/" tag) |
| 21 | + {:headers {"auth-token" auth-token |
| 22 | + "content-type" "plain/text"} |
| 23 | + :body target}))) |
| 24 | + |
| 25 | +(defn tag-url [{:keys [host namespace tag path] |
| 26 | + :or {host *tags-host*}}] |
| 27 | + (str host "/" namespace "/" tag (when path (str "/" path)))) |
| 28 | + |
| 29 | +(defn tag-get [{:as opts}] |
| 30 | + (-> @(http/get (tag-url opts)) |
| 31 | + :body)) |
| 32 | + |
| 33 | +(defn tag-exists? [opts] |
| 34 | + (-> @(http/head (tag-url opts)) |
| 35 | + :status |
| 36 | + (= 200))) |
| 37 | + |
| 38 | +(defn cas-url [{:as opts |
| 39 | + :keys [host key namespace tag] |
| 40 | + :or {host *cas-host*}}] |
| 41 | + (str host "/" key |
| 42 | + (let [query-params (ring-codec/form-encode (select-keys opts [:filename :content-type]))] |
| 43 | + (when (not (str/blank? query-params)) |
| 44 | + (str "?" query-params))))) |
| 45 | + |
| 46 | +(defn cas-exists? [opts] |
| 47 | + (-> @(http/head (cas-url opts)) |
| 48 | + :status |
| 49 | + (= 200))) |
| 50 | + |
| 51 | +(defn cas-get [opts] |
| 52 | + (-> @(http/get (cas-url opts)) |
| 53 | + :body |
| 54 | + slurp)) |
| 55 | + |
| 56 | +(defn cas-put [{:keys [path host auth-token namespace tag] |
| 57 | + :or {host *cas-host*}}] |
| 58 | + (let [f (io/file path) |
| 59 | + prefix (if (fs/directory? path) (str f "/") "") |
| 60 | + files (->> (file-seq f) |
| 61 | + (filter fs/regular-file?) |
| 62 | + (map (fn [f] (let [path (str (fs/path f)) |
| 63 | + hash (with-open [s (io/input-stream f)] |
| 64 | + (h/hash s))] |
| 65 | + {:path (str/replace path prefix "") |
| 66 | + :hash hash |
| 67 | + :filename (fs/file-name path) |
| 68 | + :content f})))) |
| 69 | + {files-to-upload false files-already-uploaded true} (group-by #(cas-exists? {:key (:hash %)}) files) |
| 70 | + multipart (concat (map (fn [{:keys [path filename content]}] |
| 71 | + {:name path |
| 72 | + :filename filename |
| 73 | + :content content}) files-to-upload) |
| 74 | + (map (fn [{:keys [path filename hash]}] |
| 75 | + {:name path |
| 76 | + :filename filename |
| 77 | + :content-type "application/clerk-cas-hash" |
| 78 | + :content hash}) files-already-uploaded)) |
| 79 | + {:as res :keys [status body]} @(http/post |
| 80 | + host |
| 81 | + (cond-> {:multipart multipart} |
| 82 | + tag (merge {:query-params {:tag (str namespace "/" tag)} |
| 83 | + :headers {"auth-token" auth-token}})))] |
| 84 | + (if (= 200 status) |
| 85 | + (-> body |
| 86 | + (json/parse-string)) |
| 87 | + res))) |
| 88 | + |
| 89 | +(defn put [{:as opts |
| 90 | + :keys [cas-host tags-host auth-token namespace tag target path] |
| 91 | + :or {cas-host *cas-host* |
| 92 | + tags-host *tags-host*}}] |
| 93 | + (assert (not (every? some? #{target path})) |
| 94 | + "Set either target or path") |
| 95 | + (cond (some? target) (tag-put opts) |
| 96 | + (some? path) (cas-put opts))) |
| 97 | + |
| 98 | +(defn get [{:as opts |
| 99 | + :keys [cas-host tags-host namespace tag path] |
| 100 | + :or {cas-host *cas-host* |
| 101 | + tags-host *tags-host*}}] |
| 102 | + (if (some? tag) |
| 103 | + (tag-get opts) |
| 104 | + (cas-get opts))) |
| 105 | + |
| 106 | +(comment |
| 107 | + (def r (cas-put {:path "test/resources/foo"})) |
| 108 | + (def r (cas-put {:path "test/resources/foo.bak" |
| 109 | + :namespace "Sohalt" |
| 110 | + :tag "test-tag" |
| 111 | + :auth-token (System/getenv "GITHUB_TOKEN")})) |
| 112 | + (def m-path (get r "manifest-id")) |
| 113 | + (def m-path (str "/tree/" (get r "manifest-id"))) |
| 114 | + (def m (get r "manifest")) |
| 115 | + (def k (get m "bar/baz.txt"))) |
0 commit comments