Browse Source

enhance(rtc): limit asset-size

rcmerci 3 weeks ago
parent
commit
0af69a942a

+ 18 - 8
src/main/frontend/worker/rtc/asset.cljs

@@ -17,6 +17,8 @@
             [malli.core :as ma]
             [missionary.core :as m]))
 
+(defonce ^:private max-asset-size (* 100 1024 1024))
+
 (defn- create-local-updates-check-flow
   "Return a flow that emits value if need to push local-updates"
   [repo *auto-push? interval-ms]
@@ -131,10 +133,10 @@
 
 (defn- new-task--concurrent-upload-assets
   "Concurrently upload assets with limited max concurrent count"
-  [repo conn asset-uuid->url asset-uuid->asset-type+checksum]
+  [repo conn asset-uuid->url asset-uuid->asset-metadata]
   (->> (fn [[asset-uuid url]]
          (m/sp
-           (let [[asset-type checksum] (get asset-uuid->asset-type+checksum asset-uuid)
+           (let [[asset-type checksum] (get asset-uuid->asset-metadata asset-uuid)
                  r (c.m/<?
                     (worker-state/<invoke-main-thread :thread-api/rtc-upload-asset
                                                       repo (str asset-uuid) asset-type checksum url))]
@@ -165,17 +167,24 @@
                                   (when (contains? asset-op :remove-asset)
                                     (:block/uuid asset-op)))
                                 asset-ops)
-            asset-uuid->asset-type+checksum
+            asset-uuid->asset-metadata
             (into {}
                   (keep
                    (fn [asset-uuid]
                      (let [ent (d/entity @conn [:block/uuid asset-uuid])]
                        (when-let [tp (:logseq.property.asset/type ent)]
                          (when-let [checksum (:logseq.property.asset/checksum ent)]
-                           [asset-uuid [tp checksum]])))))
+                           (let [size (:logseq.property.asset/size ent 0)]
+                             (if (> size max-asset-size)
+                               (do (add-log-fn :rtc.asset.log/asset-too-large
+                                               {:asset-uuid asset-uuid
+                                                :asset-name (:block/title ent)
+                                                :size size})
+                                   nil)
+                               [asset-uuid [tp checksum]])))))))
                   upload-asset-uuids)
             asset-uuid->url
-            (when (seq asset-uuid->asset-type+checksum)
+            (when (seq asset-uuid->asset-metadata)
               (->> (m/? (ws-util/send&recv get-ws-create-task
                                            {:action "get-assets-upload-urls"
                                             :graph-uuid graph-uuid
@@ -183,11 +192,11 @@
                                             (into {}
                                                   (map (fn [[asset-uuid [asset-type checksum]]]
                                                          [asset-uuid {"checksum" checksum "type" asset-type}]))
-                                                  asset-uuid->asset-type+checksum)}))
+                                                  asset-uuid->asset-metadata)}))
                    :asset-uuid->url))]
         (when (seq asset-uuid->url)
           (add-log-fn :rtc.asset.log/upload-assets {:asset-uuids (keys asset-uuid->url)}))
-        (m/? (new-task--concurrent-upload-assets repo conn asset-uuid->url asset-uuid->asset-type+checksum))
+        (m/? (new-task--concurrent-upload-assets repo conn asset-uuid->url asset-uuid->asset-metadata))
         (when (seq remove-asset-uuids)
           (add-log-fn :rtc.asset.log/remove-assets {:asset-uuids remove-asset-uuids})
           (m/? (ws-util/send&recv get-ws-create-task
@@ -247,6 +256,7 @@
   [db]
   (d/q '[:find [(pull ?b [:block/uuid
                           :logseq.property.asset/type
+                          :logseq.property.asset/size
                           :logseq.property.asset/checksum])
                 ...]
          :where
@@ -271,7 +281,7 @@
 
 (defn create-assets-sync-loop
   [repo get-ws-create-task graph-uuid major-schema-version conn *auto-push?]
-  (let [started-dfv         (m/dfv)
+  (let [started-dfv (m/dfv)
         add-log-fn (fn [type message]
                      (assert (map? message) message)
                      (rtc-log-and-state/rtc-log type (assoc message :graph-uuid graph-uuid)))

+ 3 - 2
src/main/frontend/worker/rtc/log_and_state.cljs

@@ -29,6 +29,7 @@
        :rtc.asset.log/upload-assets {:doc "upload local assets to remote"}
        :rtc.asset.log/download-assets {:doc "download assets from remote"}
        :rtc.asset.log/remove-assets {:doc "remove remote assets"}
+       :rtc.asset.log/asset-too-large {:doc "asset is too large to upload"}
        :rtc.asset.log/initial-download-missing-assets {:doc "download assets if not exists in rtc-asset-sync initial phase"})))))
 
 (def ^:private rtc-log-type-validator (ma/validator rtc-log-type-schema))
@@ -57,9 +58,9 @@
 (defn- ensure-uuid
   [v]
   (cond
-    (uuid? v)   v
+    (uuid? v) v
     (string? v) (uuid v)
-    :else       (throw (ex-info "illegal value" {:data v}))))
+    :else (throw (ex-info "illegal value" {:data v}))))
 
 (defn- create-local-t-flow
   [graph-uuid]