get image url gen working
This commit is contained in:
parent
ae86e83716
commit
a11ab3f3d5
8 changed files with 52 additions and 26 deletions
|
|
@ -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
1
.gitignore
vendored
|
|
@ -1,2 +1,3 @@
|
||||||
.clj-kondo
|
.clj-kondo
|
||||||
.lsp
|
.lsp
|
||||||
|
config.edn
|
||||||
|
|
|
||||||
1
bb.edn
1
bb.edn
|
|
@ -1,4 +1,5 @@
|
||||||
{:paths ["src"]
|
{: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"
|
:deps {com.travisshears/gum-utils {:git/url "https://git.travisshears.com/travisshears/gum-utils"
|
||||||
:git/tag "39K"
|
:git/tag "39K"
|
||||||
:git/sha "748b21d358b62db0476bc3577cb5398acc533ba1"}}}
|
:git/sha "748b21d358b62db0476bc3577cb5398acc533ba1"}}}
|
||||||
|
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
||||||
{:aws-profile "personal"
|
|
||||||
:s3-root "s3://travisshears.images/image-service/images/"}
|
|
||||||
5
config.edn.sample
Normal file
5
config.edn.sample
Normal 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
BIN
db.db
Normal file
Binary file not shown.
|
|
@ -4,6 +4,8 @@
|
||||||
(:import (java.time LocalDateTime)
|
(:import (java.time LocalDateTime)
|
||||||
(java.time.format DateTimeFormatter))
|
(java.time.format DateTimeFormatter))
|
||||||
(:require
|
(:require
|
||||||
|
[pod.babashka.go-sqlite3 :as sqlite]
|
||||||
|
[babashka.fs :as fs]
|
||||||
[image-service-cli.config :refer [config]]
|
[image-service-cli.config :refer [config]]
|
||||||
[babashka.process :refer [shell]]
|
[babashka.process :refer [shell]]
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
|
|
@ -16,7 +18,7 @@
|
||||||
(when match
|
(when match
|
||||||
(second match))))
|
(second match))))
|
||||||
s3-output (->
|
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)]
|
:out)]
|
||||||
(->>
|
(->>
|
||||||
(str/split s3-output #"\n")
|
(str/split s3-output #"\n")
|
||||||
|
|
@ -27,7 +29,7 @@
|
||||||
(defn s3-list-dir [dir]
|
(defn s3-list-dir [dir]
|
||||||
"List the contents of a directory in S3. Skipping nested directories"
|
"List the contents of a directory in S3. Skipping nested directories"
|
||||||
(as->
|
(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)
|
(:out x)
|
||||||
(str/split x #"\n") ;; split on newline
|
(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
|
(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 #(list (LocalDateTime/parse (first %) s3-date-formatter) (second %)) x)
|
||||||
(map #(hash-map :date (first %) :file-name (second %)) x)))
|
(map #(hash-map :date (first %) :file-name (second %)) x)))
|
||||||
|
|
||||||
;; (defn fetch-snippets []
|
(defn init-db [path]
|
||||||
;; (let [res (http/get (str (:backend-host config) "/api/snippets?limit=25"))]
|
(sqlite/execute! path
|
||||||
;; (json/parse-string (:body res) true)))
|
["create table if not exists images (title TEXT, created TEXT);"])
|
||||||
|
(sqlite/execute! path
|
||||||
|
["delete from images;"]))
|
||||||
|
|
||||||
;; (defn send-patch [id patch]
|
(defn save-to-db [db-path record]
|
||||||
;; (http/patch (str (:backend-host config) "/api/snippet")
|
(let [fmt DateTimeFormatter/ISO_LOCAL_DATE_TIME
|
||||||
;; {:query-params {:id id}
|
formatted-date (-> record :date (.format fmt))
|
||||||
;; :headers {:content-type "application/json"}
|
file-name (record :file-name)]
|
||||||
;; :body (json/encode patch)}))
|
(sqlite/execute! db-path
|
||||||
|
["insert into images (title, created) values (?, ?);" file-name formatted-date])))
|
||||||
|
|
||||||
(defn run []
|
(defn run []
|
||||||
;; get file names and created dates from s3
|
(let [db-path (:db-path config)]
|
||||||
(flatten (map image-service-cli.index/s3-list-dir (image-service-cli.index/s3-root-dirs))))
|
(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 %)))))
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,32 @@
|
||||||
(ns image-service-cli.main
|
(ns image-service-cli.main
|
||||||
|
(:import [java.util Base64])
|
||||||
(:require
|
(: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]))
|
[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 base64-encode [data]
|
||||||
(defn s3-list [dir]
|
(let [bytes (.getBytes data)
|
||||||
(let [cmd (if prefill
|
encoder (Base64/getEncoder)]
|
||||||
(format "gum input --placeholder='%s' --value='%s'" placeholder prefill)
|
(.encodeToString encoder bytes)))
|
||||||
(format "gum input --placeholder='%s'" placeholder))]
|
|
||||||
(str/trim (-> (shell {:out :string} cmd) :out))))
|
(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]
|
(defn -main [& args]
|
||||||
(case (first args)
|
(case (first args)
|
||||||
"upload" (create/run)
|
;; "upload" (create/run)
|
||||||
"ls" (delete/run)
|
;; "ls" (delete/run)
|
||||||
(println "Missing command. Try upload or ls.")))
|
"index" (index/run)
|
||||||
|
(println "Missing command. Try upload, ls, or index.")))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue