get image url gen working

This commit is contained in:
Travis Shears 2025-07-12 14:37:15 +02:00
parent ae86e83716
commit a11ab3f3d5
8 changed files with 52 additions and 26 deletions

View file

@ -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"]
["^ ","~$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"]

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
.clj-kondo
.lsp
config.edn

1
bb.edn
View file

@ -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"}}}

View file

@ -1,2 +0,0 @@
{:aws-profile "personal"
:s3-root "s3://travisshears.images/image-service/images/"}

5
config.edn.sample Normal file
View file

@ -0,0 +1,5 @@
{:aws-profile "xxxxxx"
:s3-bucket "xxxxxxxxxxxxxxxxxxx"
:s3-root "xxxxxxxxxxxxxxxxxxxxx"
:db-path "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
:final-host "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}

BIN
db.db Normal file

Binary file not shown.

View file

@ -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 %)))))

View file

@ -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.")))