Browse Source

feat(asset-sync): add progress when upload&download

rcmerci 1 năm trước cách đây
mục cha
commit
3ea357d7c2
3 tập tin đã thay đổi với 45 bổ sung19 xóa
  1. 3 1
      deps.edn
  2. 41 18
      src/main/frontend/handler/assets.cljs
  3. 1 0
      src/main/frontend/state.cljs

+ 3 - 1
deps.edn

@@ -47,7 +47,9 @@
                                               ;; when PR merged
                                               ;; https://github.com/open-spaced-repetition/cljc-fsrs/pull/5
                                               :git/url "https://github.com/rcmerci/cljc-fsrs"}
-  tick/tick                             {:mvn/version "0.7.5"}}
+  tick/tick                              {:mvn/version "0.7.5"}
+  io.github.rcmerci/cljs-http-missionary {:git/url "https://github.com/RCmerci/cljs-http-missionary"
+                                          :git/sha "d61ce7e29186de021a2a453a8cee68efb5a88440"}}
 
  :aliases {:cljs {:extra-paths ["src/dev-cljs/" "src/test/" "src/electron/"]
                   :extra-deps  {org.clojure/clojurescript        {:mvn/version "1.11.132"}

+ 41 - 18
src/main/frontend/handler/assets.cljs

@@ -1,5 +1,5 @@
 (ns ^:no-doc frontend.handler.assets
-  (:require [cljs-http.client :as http]
+  (:require [cljs-http-missionary.client :as http]
             [clojure.string :as string]
             [frontend.common.missionary-util :as c.m]
             [frontend.config :as config]
@@ -276,28 +276,51 @@
   [repo asset-block-uuid-str asset-type checksum put-url]
   (assert (and asset-type checksum))
   (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" checksum
-                                              "x-amz-meta-type" asset-type}
-                                    :body asset-file
-                                    :with-credentials? false}))]
-     (when-not (http/unexceptional-status? status)
-       {:ex-data {:type :rtc.exception/upload-asset-failed :data r}}))))
+    (let [asset-file (c.m/<? (<read-asset repo asset-block-uuid-str asset-type))
+          *progress-flow (atom nil)
+          http-task (http/put put-url {:headers {"x-amz-meta-checksum" checksum
+                                                 "x-amz-meta-type" asset-type}
+                                       :body asset-file
+                                       :with-credentials? false
+                                       :*progress-flow *progress-flow})]
+      (c.m/run-task
+       (m/reduce (fn [_ v]
+                   (prn :debug-upload v)
+                   (state/update-state!
+                    :rtc/asset-upload-download-progress
+                    (fn [m] (assoc m asset-block-uuid-str v))))
+                 @*progress-flow)
+       :upload-asset-progress
+       :succ (constantly nil))
+      (let [{:keys [status] :as r} (m/? http-task)]
+        (when-not (http/unexceptional-status? status)
+          {:ex-data {:type :rtc.exception/upload-asset-failed :data r}})))))
 
 (defn new-task--rtc-download-asset
   [repo asset-block-uuid-str asset-type get-url]
   (state/update-state! :rtc/asset-downloading? (fn [m] (assoc m asset-block-uuid-str true)))
   (m/sp
-   (let [{:keys [status body] :as r} (c.m/<? (http/get get-url {:with-credentials? false
-                                                                :response-type :array-buffer}))]
-     (if-not (http/unexceptional-status? status)
-       (do
-         (state/update-state! :rtc/asset-downloading? (fn [m] (assoc m asset-block-uuid-str false))) ; TODO: :failed
-         {:ex-data {:type :rtc.exception/download-asset-failed :data r}})
-       (do (c.m/<? (<write-asset repo asset-block-uuid-str asset-type body))
-           (state/update-state! :rtc/asset-downloading? (fn [m] (assoc m asset-block-uuid-str false)))
-           nil)))))
+    (let [*progress-flow (atom nil)
+          http-task (http/get get-url {:with-credentials? false
+                                       :response-type :array-buffer
+                                       :*progress-flow *progress-flow})]
+      (c.m/run-task
+       (m/reduce (fn [_ v]
+                   (prn :debug-download v)
+                   (state/update-state!
+                    :rtc/asset-upload-download-progress
+                    (fn [m] (assoc m asset-block-uuid-str v))))
+                 @*progress-flow)
+       :download-asset-progress
+       :succ (constantly nil))
+      (let [{:keys [status body] :as r} (m/? http-task)]
+        (if-not (http/unexceptional-status? status)
+          (do
+            (state/update-state! :rtc/asset-downloading? (fn [m] (assoc m asset-block-uuid-str false))) ; TODO: :failed
+            {:ex-data {:type :rtc.exception/download-asset-failed :data r}})
+          (do (c.m/<? (<write-asset repo asset-block-uuid-str asset-type body))
+              (state/update-state! :rtc/asset-downloading? (fn [m] (assoc m asset-block-uuid-str false)))
+              nil))))))
 
 (comment
   ;; read asset

+ 1 - 0
src/main/frontend/state.cljs

@@ -294,6 +294,7 @@
       :rtc/graphs                            []
       :rtc/online-info                       (atom {})
       :rtc/asset-downloading?                (atom {})
+      :rtc/asset-upload-download-progress    (atom {})
 
       :user/info                             {:UserGroups (storage/get :user-groups)}
       :encryption/graph-parsing?             false