Pārlūkot izejas kodu

enhance(asset-sync): compare local asset-file metadata when generating ops

rcmerci 1 gadu atpakaļ
vecāks
revīzija
6d9da5713d

+ 3 - 3
src/main/frontend/handler/assets.cljs

@@ -217,7 +217,7 @@
   []
   (when-let [path (config/get-current-repo-assets-root)]
     (p/let [result (p/catch (fs/readdir path {:path-only? true})
-                            (fn [_e] nil))]
+                       (constantly nil))]
       (p/all (map (fn [path]
                     (p/let [data (fs/read-file path "" {})]
                       (let [path' (util/node-path.join "assets" (util/node-path.basename path))]
@@ -240,7 +240,7 @@
   [repo]
   (when-let [path (config/get-repo-assets-root repo)]
     (p/catch (fs/readdir path {:path-only? true})
-             (fn [_e] nil))))
+             (constantly nil))))
 
 (defn <read-asset
   [repo asset-block-id asset-type]
@@ -255,7 +255,7 @@
               blob (js/Blob. (array file) (clj->js {:type "image"}))
               checksum (get-file-checksum blob)]
         {:checksum checksum})
-      (p/catch (fn [_e] nil))))
+      (p/catch (constantly nil))))
 
 (defn <write-asset
   [repo asset-block-id asset-type data]

+ 32 - 19
src/main/frontend/worker/rtc/asset.cljs

@@ -42,13 +42,13 @@
 (comment
   (def cancel ((m/reduce (fn [_ v] (prn :v v)) remote-asset-updates-flow) prn prn)))
 
-(comment
-  (defn- new-task--get-asset-file-metadata
-    [repo block-uuid asset-type]
-    (m/sp
-      (ldb/read-transit-str
-       (c.m/<?
-        (.get-asset-file-metadata ^js @worker-state/*main-thread repo (str block-uuid) asset-type))))))
+(defn- new-task--get-asset-file-metadata
+  "Return nil if this asset not exist"
+  [repo block-uuid asset-type]
+  (m/sp
+    (ldb/read-transit-str
+     (c.m/<?
+      (.get-asset-file-metadata ^js @worker-state/*main-thread repo (str block-uuid) asset-type)))))
 
 (defn- remote-block-ops=>remote-asset-ops
   [db-before remove-ops]
@@ -68,18 +68,31 @@
              (not-empty (remote-block-ops=>remote-asset-ops db-before remove-ops))]
     (reset! *remote-asset-updates asset-update-ops)))
 
-(defn emit-remote-asset-updates-from-push-asset-upload-updates
-  [db push-asset-upload-updates-message]
-  (let [{:keys [uploaded-assets]} push-asset-upload-updates-message]
-    (when-let [asset-update-ops
-               (not-empty
-                (keep (fn [[asset-uuid _remote-metadata]]
-                        ;; TODO: compare checksum in remote-metadata
-                        (when (some? (d/entity db [:block/uuid asset-uuid]))
-                          {:op :update-asset
-                           :block/uuid asset-uuid}))
-                      uploaded-assets))]
-      (reset! *remote-asset-updates asset-update-ops))))
+(defn new-task--emit-remote-asset-updates-from-push-asset-upload-updates
+  [repo db push-asset-upload-updates-message]
+  (m/sp
+    (let [{:keys [uploaded-assets]} push-asset-upload-updates-message]
+      (when-let [asset-update-ops
+                 (->> uploaded-assets
+                      (map
+                       (fn [[asset-uuid remote-metadata]]
+                         (m/sp
+                           (let [ent (d/entity db [:block/uuid asset-uuid])
+                                 asset-type (:logseq.property.asset/type ent)
+                                 local-checksum (:logseq.property.asset/checksum ent)
+                                 remote-checksum (get remote-metadata "checksum")]
+                             (when (or (and local-checksum remote-checksum
+                                            (not= local-checksum remote-checksum))
+                                       (and asset-type
+                                            (nil? (m/? (new-task--get-asset-file-metadata
+                                                        repo asset-uuid asset-type)))))
+                               {:op :update-asset
+                                :block/uuid asset-uuid})))))
+                      (apply m/join vector)
+                      m/?
+                      (remove nil?)
+                      not-empty)]
+        (reset! *remote-asset-updates asset-update-ops)))))
 
 (defn- create-mixed-flow
   "Return a flow that emits different events:

+ 2 - 1
src/main/frontend/worker/rtc/core.cljs

@@ -188,7 +188,8 @@
                         (m/? (r.client/new-task--pull-remote-data
                               repo conn graph-uuid date-formatter get-ws-create-task add-log-fn)))))
                :remote-asset-update
-               (r.asset/emit-remote-asset-updates-from-push-asset-upload-updates @conn (:value event))
+               (m/? (r.asset/new-task--emit-remote-asset-updates-from-push-asset-upload-updates
+                     repo @conn (:value event)))
 
                :local-update-check
                (m/? (r.client/new-task--push-local-ops