Просмотр исходного кода

fix(sync): encode&decode file-path

rcmerci 3 лет назад
Родитель
Сommit
85a5ba4154
3 измененных файлов с 76 добавлено и 72 удалено
  1. 1 1
      resources/package.json
  2. 47 43
      src/main/frontend/fs/sync.cljs
  3. 28 28
      static/yarn.lock

+ 1 - 1
resources/package.json

@@ -36,7 +36,7 @@
     "https-proxy-agent": "5.0.0",
     "https-proxy-agent": "5.0.0",
     "@sentry/electron": "2.5.1",
     "@sentry/electron": "2.5.1",
     "posthog-js": "1.10.2",
     "posthog-js": "1.10.2",
-    "@andelf/rsapi": "0.0.2"
+    "@andelf/rsapi": "0.0.5"
   },
   },
   "devDependencies": {
   "devDependencies": {
     "@electron-forge/cli": "^6.0.0-beta.57",
     "@electron-forge/cli": "^6.0.0-beta.57",

+ 47 - 43
src/main/frontend/fs/sync.cljs

@@ -16,27 +16,27 @@
             [cljs.core.async.impl.channels]))
             [cljs.core.async.impl.channels]))
 
 
 ;;; Commentary
 ;;; Commentary
-;;; file-sync related local files/dirs:
-;;; - logseq/graphs-txid.edn
-;;;   this file contains graph-uuid & transaction-id
-;;;   graph-uuid: the unique identifier of the graph on the server
-;;;   transaction-id: sync progress of local files
-;;; - logseq/version-files
-;;;   downloaded version-files
-;;; files included by `get-ignore-files` will not be synchronized.
-;;; files in these `get-monitored-dirs` dirs will be synchronized.
-;;;
-;;; sync strategy:
-;;; - when toggle file-sync on, trigger a full-sync first,
-;;;   full-sync will compare local-files with remote-files (by md5 & size),
-;;;   and upload new-added-files to remote server.
-;;; - full-sync will be triggered after 20min of idle
-;;; - every 20s will flush local changes, and sync to remote
-
-
-;;; TODO: add some spec validate
-;;; TODO: use access-token instead of id-token
-;;; TODO: update-remote-file failed when pagename contains whitespace
+;; file-sync related local files/dirs:
+;; - logseq/graphs-txid.edn
+;;   this file contains graph-uuid & transaction-id
+;;   graph-uuid: the unique identifier of the graph on the server
+;;   transaction-id: sync progress of local files
+;; - logseq/version-files
+;;   downloaded version-files
+;; files included by `get-ignore-files` will not be synchronized.
+;; files in these `get-monitored-dirs` dirs will be synchronized.
+;;
+;; sync strategy:
+;; - when toggle file-sync on, trigger a full-sync first,
+;;   full-sync will compare local-files with remote-files (by md5 & size),
+;;   and upload new-added-files to remote server.
+;; - full-sync will be triggered after 20min of idle
+;; - every 20s will flush local changes, and sync to remote
+
+
+;; TODO: add some spec validate
+;; TODO: use access-token instead of id-token
+;; TODO: update-remote-file failed when pagename contains whitespace
 
 
 (def ws-addr "wss://og96xf1si7.execute-api.us-east-2.amazonaws.com/production?graphuuid=%s")
 (def ws-addr "wss://og96xf1si7.execute-api.us-east-2.amazonaws.com/production?graphuuid=%s")
 
 
@@ -174,7 +174,7 @@
         (comp
         (comp
          (remove empty?)
          (remove empty?)
          (map #(->FileTxn % % update? delete? TXId)))
          (map #(->FileTxn % % update? delete? TXId)))
-        filepaths (string/split-lines TXContent)]
+        filepaths (map js/decodeURIComponent (string/split-lines TXContent))]
     (case TXType
     (case TXType
       ("update_files" "delete_files")
       ("update_files" "delete_files")
       (sequence update-or-del-type-xf filepaths)
       (sequence update-or-del-type-xf filepaths)
@@ -319,6 +319,8 @@
     :else
     :else
     (throw (js/Error. (str "unsupport type " (type o))))))
     (throw (js/Error. (str "unsupport type " (type o))))))
 
 
+;;; APIs
+;; `RSAPI` call apis through rsapi package, supports operations on files
 
 
 (defprotocol IRSAPI
 (defprotocol IRSAPI
   (get-local-files-meta [this graph-uuid base-path filepaths] "get local files' metadata")
   (get-local-files-meta [this graph-uuid base-path filepaths] "get local files' metadata")
@@ -467,29 +469,28 @@
       (let [r (<! (.request this "get_all_files" {:GraphUUID graph-uuid}))]
       (let [r (<! (.request this "get_all_files" {:GraphUUID graph-uuid}))]
         (if (instance? ExceptionInfo r)
         (if (instance? ExceptionInfo r)
           r
           r
-          (->> r
-               :Objects
-               (map #(->FileMetadata (:Size %)
-                                     (:ETag %)
-                                     (remove-user-graph-uuid-prefix (:Key %))
-                                     (:LastModified %)
-                                     true nil))
-               set)))))
+          (into #{} (map
+                     #(->FileMetadata (:Size %)
+                                      (:ETag %)
+                                      (remove-user-graph-uuid-prefix (js/decodeURIComponent (:Key %)))
+                                      (:LastModified %)
+                                      true nil))
+                (:Objects r))))))
 
 
   (get-remote-files-meta [this graph-uuid filepaths]
   (get-remote-files-meta [this graph-uuid filepaths]
     {:pre [(coll? filepaths)]}
     {:pre [(coll? filepaths)]}
     (go
     (go
-      (let [r (<! (.request this "get_files_meta" {:GraphUUID graph-uuid :Files filepaths}))]
+      (let [encoded-filepaths (map js/encodeURIComponent filepaths)
+            r (<! (.request this "get_files_meta" {:GraphUUID graph-uuid :Files encoded-filepaths}))]
         (if (instance? ExceptionInfo r)
         (if (instance? ExceptionInfo r)
           r
           r
-          (->> r
-               :Files
-               (map #(->FileMetadata (:Size %)
-                                     (:ETag %)
-                                     (:FilePath %)
-                                     (:LastModified %)
-                                     true nil))
-               (into #{}))))))
+          (into #{}
+                (map #(->FileMetadata (:Size %)
+                                      (:ETag %)
+                                      (js/decodeURIComponent (:FilePath %))
+                                      (:LastModified %)
+                                      true nil))
+                (:Files r))))))
 
 
   (get-remote-graph [this graph-name-opt graph-uuid-opt]
   (get-remote-graph [this graph-name-opt graph-uuid-opt]
     {:pre [(or graph-name-opt graph-uuid-opt)]}
     {:pre [(or graph-name-opt graph-uuid-opt)]}
@@ -499,7 +500,7 @@
                                  (seq graph-uuid-opt)
                                  (seq graph-uuid-opt)
                                  (assoc :GraphUUID graph-uuid-opt))))
                                  (assoc :GraphUUID graph-uuid-opt))))
   (get-remote-file-versions [this graph-uuid filepath]
   (get-remote-file-versions [this graph-uuid filepath]
-    (.request this "get_file_version_list" {:GraphUUID graph-uuid :File filepath}))
+    (.request this "get_file_version_list" {:GraphUUID graph-uuid :File (js/encodeURIComponent filepath)}))
   (list-remote-graphs [this]
   (list-remote-graphs [this]
     (.request this "list_graphs"))
     (.request this "list_graphs"))
 
 
@@ -590,9 +591,8 @@
 
 
 
 
 
 
-(def local-changes-chan (chan 100))
 
 
-;;; type = "change" | "add" | "unlink"
+;; type = "change" | "add" | "unlink"
 (deftype FileChangeEvent [type dir path stat]
 (deftype FileChangeEvent [type dir path stat]
   IRelativePath
   IRelativePath
   (-relative-path [_] (remove-dir-prefix dir path))
   (-relative-path [_] (remove-dir-prefix dir path))
@@ -620,8 +620,9 @@
    (map #(partition-all n %))
    (map #(partition-all n %))
    cat))
    cat))
 
 
-
+(def local-changes-chan (chan 100))
 (defn file-watch-handler
 (defn file-watch-handler
+  "file-watcher callback"
   [type {:keys [dir path _content stat] :as _payload}]
   [type {:keys [dir path _content stat] :as _payload}]
   (go
   (go
     (when (some-> (state/get-file-sync-state-manager)
     (when (some-> (state/get-file-sync-state-manager)
@@ -629,6 +630,7 @@
                   not)
                   not)
       (>! local-changes-chan (->FileChangeEvent type dir path stat)))))
       (>! local-changes-chan (->FileChangeEvent type dir path stat)))))
 
 
+;;; remote->local syncer & local->remote syncer
 
 
 (defprotocol IRemote->LocalSync
 (defprotocol IRemote->LocalSync
   (stop-remote->local! [this])
   (stop-remote->local! [this])
@@ -825,6 +827,7 @@
 
 
                   (or need-sync-remote unknown) r)))))))))
                   (or need-sync-remote unknown) r)))))))))
 
 
+;;; sync state
 (deftype SyncState [^:mutable state ^:mutable current-local->remote-files ^:mutable current-remote->local-files
 (deftype SyncState [^:mutable state ^:mutable current-local->remote-files ^:mutable current-remote->local-files
                     ^:mutable history]
                     ^:mutable history]
   Object
   Object
@@ -869,6 +872,7 @@
                   :current-downloading-files current-remote->local-files}]
                   :current-downloading-files current-remote->local-files}]
       (-pr-writer pr-map w opts))))
       (-pr-writer pr-map w opts))))
 
 
+;;; put all stuff together
 
 
 (deftype SyncManager [graph-uuid base-path ^SyncState sync-state
 (deftype SyncManager [graph-uuid base-path ^SyncState sync-state
                       ^Local->RemoteSyncer local->remote-syncer ^Remote->LocalSyncer remote->local-syncer
                       ^Local->RemoteSyncer local->remote-syncer ^Remote->LocalSyncer remote->local-syncer

+ 28 - 28
static/yarn.lock

@@ -12,35 +12,35 @@
   resolved "https://registry.yarnpkg.com/7zip-bin/-/7zip-bin-5.1.1.tgz#9274ec7460652f9c632c59addf24efb1684ef876"
   resolved "https://registry.yarnpkg.com/7zip-bin/-/7zip-bin-5.1.1.tgz#9274ec7460652f9c632c59addf24efb1684ef876"
   integrity sha512-sAP4LldeWNz0lNzmTird3uWfFDWWTeg6V/MsmyyLR9X1idwKBWIgt/ZvinqQldJm3LecKEs1emkbquO6PCiLVQ==
   integrity sha512-sAP4LldeWNz0lNzmTird3uWfFDWWTeg6V/MsmyyLR9X1idwKBWIgt/ZvinqQldJm3LecKEs1emkbquO6PCiLVQ==
 
 
-"@andelf/[email protected].2":
-  version "0.0.2"
-  resolved "https://registry.npmmirror.com/@andelf/rsapi-darwin-arm64/download/@andelf/rsapi-darwin-arm64-0.0.2.tgz#c89a025fbc2e632ef1ed2a96fa64787e29e4bab8"
-  integrity sha512-WfsVD+5EZkGD7NMyJ0Akwrowgf3IF2BcckERwx0PHQzHi09NZLxZtMr2NN2s6q8QOvGUfbqHYEATZ8zaZMU7XQ==
-
-"@andelf/[email protected].2":
-  version "0.0.2"
-  resolved "https://registry.npmmirror.com/@andelf/rsapi-darwin-x64/download/@andelf/rsapi-darwin-x64-0.0.2.tgz#d9f71946122dc4bb056e512fa9fc820d30c7159b"
-  integrity sha512-r5F9odlNt0dD2W4eKYlJnqqoim/5kn/lzch7kHmvR9SKbxIe8jn/XwGqk5Kj5zIlhwpksT24dF47ppnZ8gJZRw==
-
-"@andelf/[email protected].2":
-  version "0.0.2"
-  resolved "https://registry.npmmirror.com/@andelf/rsapi-linux-x64-gnu/download/@andelf/rsapi-linux-x64-gnu-0.0.2.tgz#f90d8402cbb7e07fcc5defc5c3743d2a591cc22b"
-  integrity sha512-olB2TYRzqB5e8brtyvj0+YnbdLyWCvsQjbDqV8nKyvUThSEvBPUgREBigdpb26wSETPCX7q/IEQNDDOLdXjzmQ==
-
-"@andelf/[email protected].2":
-  version "0.0.2"
-  resolved "https://registry.npmmirror.com/@andelf/rsapi-win32-x64-msvc/download/@andelf/rsapi-win32-x64-msvc-0.0.2.tgz#cee77358a4a3601d8e5a6aa5199ef537eadbd6f3"
-  integrity sha512-qgVZY4lnnlVFu5cNV/pEkvlI/hGokpfpIAGAipYLKVcAmuZ/H9+3aD8PcSi4uRIuexFlSBIcNBi7FtnNahMUcA==
-
-"@andelf/[email protected].2":
-  version "0.0.2"
-  resolved "https://registry.npmmirror.com/@andelf/rsapi/download/@andelf/rsapi-0.0.2.tgz#8b24e9792123131f5f4529f00fb8f380333acc26"
-  integrity sha512-/CVyYM3+fRFvO6Dqyd8TJv6IOZsaFpKAkX07Vs2FbnSzR50XtGujYpcjyKjmq3zLPmtJYuvD80qwKqixvzCPPw==
+"@andelf/[email protected].5":
+  version "0.0.5"
+  resolved "https://registry.npmmirror.com/@andelf/rsapi-darwin-arm64/download/@andelf/rsapi-darwin-arm64-0.0.5.tgz#894724098fcb722088a2971b6e7771b1aaf1a77b"
+  integrity sha512-S+BzDs0a3El4VnULFBRnaWm8oKGlW+oYVTPELop53gbjcDgmoRq+wIYI8MvvkPdtVYRZ6a/e4DfqrtDKDVWOxQ==
+
+"@andelf/[email protected].5":
+  version "0.0.5"
+  resolved "https://registry.npmmirror.com/@andelf/rsapi-darwin-x64/download/@andelf/rsapi-darwin-x64-0.0.5.tgz#a520066709b84c3c20b987c0cda2efa1d0fe1468"
+  integrity sha512-4nVKhh48mVbDgI+EYd3o4J1TC1WwzLk/5/pTvt76lwd5+OhzgFDxQnaFeUsxSMkc7UKixWhp5pnBrrwKRhbVRA==
+
+"@andelf/[email protected].5":
+  version "0.0.5"
+  resolved "https://registry.npmmirror.com/@andelf/rsapi-linux-x64-gnu/download/@andelf/rsapi-linux-x64-gnu-0.0.5.tgz#e35c976c6cd73cb22c6fd61f0ee943ee475d3774"
+  integrity sha512-ed5FI+8IGgQMho1hVEV2iip7AdvgeRYtgfCPPmHqRi3uAxILJqp+vRltZb1SqFqmPGSm4IJzvIGZCMGNqcLXSg==
+
+"@andelf/[email protected].5":
+  version "0.0.5"
+  resolved "https://registry.npmmirror.com/@andelf/rsapi-win32-x64-msvc/download/@andelf/rsapi-win32-x64-msvc-0.0.5.tgz#8506d864596cf8bf1f8f381ed62653e4731436b8"
+  integrity sha512-8JYGrPGOm00xaYb3KmjMjcIb8T8gkjhNOwduWOS81xkLwGFbwYPeDIKCWEOyXUGy7VCQUvQg2UY9H1t08cR+3w==
+
+"@andelf/[email protected].5":
+  version "0.0.5"
+  resolved "https://registry.npmmirror.com/@andelf/rsapi/download/@andelf/rsapi-0.0.5.tgz#6d90290c5a8a88d81402aa2a43d30030fceab60a"
+  integrity sha512-kEobsAzceX+8cGKaAYx4hin/Aw7p5ObdbVapBBzRQRfFqdxU3CJADPxo+InpIRCK4+4B9yzfi7cXWSyi/Av0hQ==
   optionalDependencies:
   optionalDependencies:
-    "@andelf/rsapi-darwin-arm64" "0.0.2"
-    "@andelf/rsapi-darwin-x64" "0.0.2"
-    "@andelf/rsapi-linux-x64-gnu" "0.0.2"
-    "@andelf/rsapi-win32-x64-msvc" "0.0.2"
+    "@andelf/rsapi-darwin-arm64" "0.0.5"
+    "@andelf/rsapi-darwin-x64" "0.0.5"
+    "@andelf/rsapi-linux-x64-gnu" "0.0.5"
+    "@andelf/rsapi-win32-x64-msvc" "0.0.5"
 
 
 "@develar/schema-utils@~2.1.0":
 "@develar/schema-utils@~2.1.0":
   version "2.1.0"
   version "2.1.0"