enhance(asset-sync): add upload/download-asset fns in main thread

This commit is contained in:
rcmerci
2024-11-10 14:44:52 +08:00
committed by Tienson Qin
parent d0837dc088
commit dd4800a3bd
2 changed files with 35 additions and 1 deletions

View File

@@ -1,5 +1,7 @@
(ns ^:no-doc frontend.handler.assets
(:require [clojure.string :as string]
(:require [cljs-http.client :as http]
[clojure.string :as string]
[frontend.common.missionary-util :as c.m]
[frontend.config :as config]
[frontend.fs :as fs]
[frontend.fs.nfs :as nfs]
@@ -10,6 +12,7 @@
[logseq.common.path :as path]
[logseq.common.util :as common-util]
[medley.core :as medley]
[missionary.core :as m]
[promesa.core :as p]))
(defn alias-enabled?
@@ -248,6 +251,25 @@
(str asset-block-id-str "." asset-type))]
(fs/write-file! repo repo-dir file-path data {})))
(defn new-task--rtc-upload-asset
[repo asset-block-uuid-str asset-type put-url]
(m/sp
(let [asset-file (c.m/<? (<read-asset repo asset-block-uuid-str asset-type))
{:keys [status] :as r}
(c.m/<? (http/put put-url {:headers {"x-amz-meta-checksum" "TODO-CHECKSUM-HERE"}
:body asset-file
:with-credentials? false}))]
(when-not (http/unexceptional-status? status)
(throw (ex-info "upload asset failed" {:type :rtc.exception/upload-asset-failed :data r}))))))
(defn new-task--rtc-download-asset
[repo asset-block-uuid-str asset-type get-url]
(m/sp
(let [{:keys [status body] :as r} (c.m/<? (http/get get-url {:with-credentials? false}))]
(when-not (http/unexceptional-status? status)
(throw (ex-info "download asset failed" {:type :rtc.exception/download-asset-failed :data r})))
(c.m/<? (<write-asset repo asset-block-uuid-str asset-type body)))))
(comment
;; read asset
(p/let [repo "logseq_db_demo"