diff --git a/.clj-kondo/.cache/v1/clj/image-service-cli.main.transit.json b/.clj-kondo/.cache/v1/clj/image-service-cli.main.transit.json index 9b033c0..b3f869c 100644 --- a/.clj-kondo/.cache/v1/clj/image-service-cli.main.transit.json +++ b/.clj-kondo/.cache/v1/clj/image-service-cli.main.transit.json @@ -1 +1 @@ -["^ ","~$s3-list",["^ ","~:row",6,"~:col",1,"~:fixed-arities",["~#set",[1]],"~:name","^0","~:ns","~$image-service-cli.main","~:top-ns","^7","~:type","~:fn"],"~$prompt-for",["^ ","^1",9,"^2",1,"~:varargs-min-arity",1,"^5","^;","^6","^7","^8","^7","^9","^:"],"~$-main",["^ ","^1",15,"^2",1,"^<",0,"^5","^=","^6","^7","^8","^7","^9","^:"],"~:filename","/Users/she0001t/personal_projects/image-service-cli/src/image_service_cli/main.clj"] \ No newline at end of file +["^ ","~$base64-encode",["^ ","~:row",12,"~:col",1,"~:fixed-arities",["~#set",[1]],"~:name","^0","~:ns","~$image-service-cli.main","~:top-ns","^7","~:type","~:fn"],"~$get-image-url",["^ ","^1",17,"^2",1,"~:varargs-min-arity",1,"^5","^;","^6","^7","^8","^7","~:arities",["^ ","~:varargs",["^ ","~:ret","~:string","~:min-arity",1,"~:arglist-str","[s3-path & [max-dimension]]"]],"^9","^:"],"~$-main",["^ ","^1",27,"^2",1,"^<",0,"^5","^C","^6","^7","^8","^7","^9","^:"],"~:filename","/Users/she0001t/personal_projects/image_service_cli/src/image_service_cli/main.clj"] \ No newline at end of file diff --git a/.gitignore b/.gitignore index 7b5c564..cdf8cde 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .clj-kondo .lsp +config.edn diff --git a/bb.edn b/bb.edn index 7a6ffd1..2787f50 100644 --- a/bb.edn +++ b/bb.edn @@ -1,4 +1,5 @@ {:paths ["src"] + :pods {org.babashka/go-sqlite3 {:version "0.2.7"}} :deps {com.travisshears/gum-utils {:git/url "https://git.travisshears.com/travisshears/gum-utils" :git/tag "39K" :git/sha "748b21d358b62db0476bc3577cb5398acc533ba1"}}} diff --git a/config.edn b/config.edn deleted file mode 100644 index 2cd9100..0000000 --- a/config.edn +++ /dev/null @@ -1,2 +0,0 @@ -{:aws-profile "personal" - :s3-root "s3://travisshears.images/image-service/images/"} diff --git a/config.edn.sample b/config.edn.sample new file mode 100644 index 0000000..94ae556 --- /dev/null +++ b/config.edn.sample @@ -0,0 +1,5 @@ +{:aws-profile "xxxxxx" + :s3-bucket "xxxxxxxxxxxxxxxxxxx" + :s3-root "xxxxxxxxxxxxxxxxxxxxx" + :db-path "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + :final-host "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"} diff --git a/db.db b/db.db new file mode 100644 index 0000000..cce80bc Binary files /dev/null and b/db.db differ diff --git a/src/image_service_cli/index.clj b/src/image_service_cli/index.clj index 0a62c01..915bcea 100644 --- a/src/image_service_cli/index.clj +++ b/src/image_service_cli/index.clj @@ -4,6 +4,8 @@ (:import (java.time LocalDateTime) (java.time.format DateTimeFormatter)) (:require + [pod.babashka.go-sqlite3 :as sqlite] + [babashka.fs :as fs] [image-service-cli.config :refer [config]] [babashka.process :refer [shell]] [clojure.string :as str] @@ -16,7 +18,7 @@ (when match (second match)))) s3-output (-> - (shell {:out :string :extra-env {"AWS_PROFILE" (:aws-profile config)}} "aws s3 ls" (:s3-root config)) + (shell {:out :string :extra-env {"AWS_PROFILE" (:aws-profile config)}} "aws s3 ls" (str "s3://" (:s3-bucket config) "/" (:s3-root config))) :out)] (->> (str/split s3-output #"\n") @@ -27,7 +29,7 @@ (defn s3-list-dir [dir] "List the contents of a directory in S3. Skipping nested directories" (as-> - (shell {:out :string :extra-env {"AWS_PROFILE" (:aws-profile config)}} "aws s3 ls" (str (:s3-root config) dir "/")) x + (shell {:out :string :extra-env {"AWS_PROFILE" (:aws-profile config)}} "aws s3 ls" (str "s3://" (:s3-bucket config) "/" (:s3-root config) dir "/")) x (:out x) (str/split x #"\n") ;; split on newline (map #(re-find #"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\s+\d+\s+(.+$)" %) x) ;; convert line into date and file name @@ -36,16 +38,22 @@ (map #(list (LocalDateTime/parse (first %) s3-date-formatter) (second %)) x) (map #(hash-map :date (first %) :file-name (second %)) x))) -;; (defn fetch-snippets [] -;; (let [res (http/get (str (:backend-host config) "/api/snippets?limit=25"))] -;; (json/parse-string (:body res) true))) +(defn init-db [path] + (sqlite/execute! path + ["create table if not exists images (title TEXT, created TEXT);"]) + (sqlite/execute! path + ["delete from images;"])) -;; (defn send-patch [id patch] -;; (http/patch (str (:backend-host config) "/api/snippet") -;; {:query-params {:id id} -;; :headers {:content-type "application/json"} -;; :body (json/encode patch)})) +(defn save-to-db [db-path record] + (let [fmt DateTimeFormatter/ISO_LOCAL_DATE_TIME + formatted-date (-> record :date (.format fmt)) + file-name (record :file-name)] + (sqlite/execute! db-path + ["insert into images (title, created) values (?, ?);" file-name formatted-date]))) (defn run [] - ;; get file names and created dates from s3 - (flatten (map image-service-cli.index/s3-list-dir (image-service-cli.index/s3-root-dirs)))) + (let [db-path (:db-path config)] + (init-db db-path) + (->> + (flatten (map image-service-cli.index/s3-list-dir (image-service-cli.index/s3-root-dirs))) + (map #(save-to-db db-path %))))) diff --git a/src/image_service_cli/main.clj b/src/image_service_cli/main.clj index ae9a4b7..21abf8b 100644 --- a/src/image_service_cli/main.clj +++ b/src/image_service_cli/main.clj @@ -1,19 +1,32 @@ (ns image-service-cli.main + (:import [java.util Base64]) (:require - [babashka.process :refer [shell]] + [image-service-cli.index :as index] + [image-service-cli.config :refer [config]] + [cheshire.core :as json] + [com.travisshears.gum-utils :as utils])) - (shell (format "gum format \"%s\"" text))) +;; user=> (pprint (sql/query (image-service-cli.config/config :db-path) "select * from images i order by i.created desc limit 10;")) -(defn prompt-for [placeholder & {:keys [prefill]}] -(defn s3-list [dir] - (let [cmd (if prefill - (format "gum input --placeholder='%s' --value='%s'" placeholder prefill) - (format "gum input --placeholder='%s'" placeholder))] - (str/trim (-> (shell {:out :string} cmd) :out)))) +(defn base64-encode [data] + (let [bytes (.getBytes data) + encoder (Base64/getEncoder)] + (.encodeToString encoder bytes))) + +(defn get-image-url [s3-path & [max-dimension]] + (let [img-config (cond-> {:bucket (:s3-bucket config) + :key (str (:s3-root config) s3-path)} + max-dimension + (assoc :edits {:resize {:width max-dimension + :height max-dimension + :fit "inside"}})) + encoded-config (base64-encode (json/encode img-config))] + (str (:final-host config) encoded-config))) (defn -main [& args] (case (first args) - "upload" (create/run) - "ls" (delete/run) - (println "Missing command. Try upload or ls."))) + ;; "upload" (create/run) + ;; "ls" (delete/run) + "index" (index/run) + (println "Missing command. Try upload, ls, or index.")))