Przeglądaj źródła

fix(fs): file editing on windows

Andelf 2 lat temu
rodzic
commit
2ad52eab3e

+ 21 - 14
deps/common/src/logseq/common/path.cljs

@@ -1,10 +1,17 @@
 (ns logseq.common.path
-  "Path manipulation functions, use '/' on all platforms.
+  "Path manipulation functions, use '/' sep on all platforms.
    Also handles URL paths."
-  (:require [clojure.string :as string]
-            [logseq.graph-parser.util :as gp-util]))
+  (:require [clojure.string :as string]))
 
 
+(defn- safe-decode-uri-component
+  [uri]
+  (try
+    (js/decodeURIComponent uri)
+    (catch :default _
+      (js/console.error "decode-uri-component-failed" uri)
+      uri)))
+
 (defn is-file-url
   [s]
   (and (string? s)
@@ -22,8 +29,8 @@
   (let [fname (if (string/ends-with? path "/")
                 nil
                 (last (string/split path #"/")))]
-    (if (and (not-empty fname) (is-file-url path))
-      (gp-util/safe-decode-uri-component fname)
+    (if (and (seq fname) (is-file-url path))
+      (safe-decode-uri-component fname)
       fname)))
 
 (defn split-ext
@@ -75,7 +82,7 @@
                     [""] "/"
                     #_{:clj-kondo/ignore [:path-invalid-construct/string-join]}
                     (string/join "/" segs)))]
-    (->> (filter not-empty segments)
+    (->> (filter seq segments)
          (mapcat split-fn)
          (reduce (fn [acc segment]
                    (cond
@@ -114,7 +121,7 @@
                     [""] "/"
                     #_{:clj-kondo/ignore [:path-invalid-construct/string-join]}
                     (string/join "/" segs)))]
-    (->> (filter not-empty segments)
+    (->> (filter seq segments)
          (mapcat split-fn)
          (map #(js/encodeURIComponent %))
          (reduce (fn [acc segment]
@@ -143,7 +150,7 @@
   (let [^js url (js/URL. base-url)
         scheme (.-protocol url)
         domain (or (not-empty (.-host url)) "")
-        path (gp-util/safe-decode-uri-component (.-pathname url))
+        path (safe-decode-uri-component (.-pathname url))
         encoded-new-path (apply uri-path-join-internal path segments)]
     (str scheme "//" domain encoded-new-path)))
 
@@ -176,7 +183,7 @@
   (let [^js url (js/URL. origin-url)
         scheme (.-protocol url)
         domain (or (not-empty (.-host url)) "")
-        path (gp-util/safe-decode-uri-component (.-pathname url))
+        path (safe-decode-uri-component (.-pathname url))
         encoded-new-path (uri-path-join-internal path)]
     (str scheme "//" domain encoded-new-path)))
 
@@ -196,7 +203,7 @@
     ;; NOTE: URL type is not consistent across all protocols
     ;; Check file:// and assets://, pathname behavior is different
     (let [^js url (js/URL. (string/replace original-url "assets://" "file://"))
-          path (gp-util/safe-decode-uri-component (.-pathname url))
+          path (safe-decode-uri-component (.-pathname url))
           path (if (string/starts-with? path "///")
                  (subs path 2)
                  path)
@@ -214,7 +221,7 @@
         is-url? (is-file-url base-path)]
     (if (string/starts-with? sub-path base-path)
       (if is-url?
-        (gp-util/safe-decode-uri-component (string/replace (subs sub-path (count base-path)) #"^/+", ""))
+        (safe-decode-uri-component (string/replace (subs sub-path (count base-path)) #"^/+", ""))
         (string/replace (subs sub-path (count base-path)) #"^/+", ""))
       (do
         (js/console.error "unhandled trim-base" base-path sub-path)
@@ -229,7 +236,7 @@
         is-url? (is-file-url base-path)]
     (if (string/starts-with? sub-path base-path)
       (if is-url?
-        (gp-util/safe-decode-uri-component (string/replace (subs sub-path (count base-path)) #"^/+", ""))
+        (safe-decode-uri-component (string/replace (subs sub-path (count base-path)) #"^/+", ""))
         (string/replace (subs sub-path (count base-path)) #"^/+", ""))
        ;; append as many .. 
       ;; NOTE: buggy impl
@@ -242,7 +249,7 @@
         (js/console.error (js/Error. "buggy relative-path") base-path sub-path)
         #_{:clj-kondo/ignore [:path-invalid-construct/string-join]}
         (if is-url?
-          (gp-util/safe-decode-uri-component (str base-prefix (string/join "/" remain-segs)))
+          (safe-decode-uri-component (str base-prefix (string/join "/" remain-segs)))
           (str base-prefix (string/join "/" remain-segs)))))))
 
 
@@ -281,7 +288,7 @@
         base-prefix (apply str (repeat (max 0 (count base-segs)) "../"))]
     #_{:clj-kondo/ignore [:path-invalid-construct/string-join]}
     (if is-url?
-      (gp-util/safe-decode-uri-component (str base-prefix (string/join "/" remain-segs)))
+      (safe-decode-uri-component (str base-prefix (string/join "/" remain-segs)))
       (str base-prefix (string/join "/" remain-segs)))))
 
 ;; compat

+ 12 - 14
deps/common/src/logseq/common/path_test.cljs

@@ -2,8 +2,6 @@
   (:require [cljs.test :refer [deftest is testing]]
             [logseq.common.path :as path]))
 
-
-
 (deftest test-safe-file-name?
   (testing "safe-file-name"
     (is (path/safe-filename? "foo"))
@@ -13,22 +11,22 @@
     (is (path/safe-filename? "foo.bar"))
     (is (path/safe-filename? "foo..bar"))
     (is (path/safe-filename? "foo...bar"))
-    (is (= nil (path/safe-filename? "foo/bar")))
+    (is (not (path/safe-filename? "foo/bar")))
     (is (not (path/safe-filename? "foo?bar")))
     (is (not (path/safe-filename? "foo<bar")))
     (is (not (path/safe-filename? "foo>bar")))))
 
 
 (deftest path-join
-  (testing "join-path")
-  (is (= "foo/bar" (path/path-join "foo" "bar")))
-  (is (= "foo/bar" (path/path-join "foo/" "bar")))
-  (is (= "/foo/bar/baz/asdf" (path/path-join "/foo/bar//baz/asdf/quux/..")))
-  (is (= "https://foo.bar/baz" (path/path-join "https://foo.bar" "baz")))
-  (is (= "https://foo.bar/baz" (path/path-join "https://foo.bar/" "baz")))
-  (is (= "https://foo.bar/baz" (path/path-join "https://foo.bar/" "/baz")))
-  (is (= "https://foo.bar/baz" (path/path-join "https://foo.bar" "/baz")))
-  (is (= "https://foo.bar/baz" (path/path-join "https://foo.bar" "/baz/")))
-  (is (= "https://foo.bar/baz" (path/path-join "https://foo.bar/" "/baz/")))
-  (is (= "https://foo.bar/baz" (path/path-join "https://foo.bar/" "/baz"))))
+  (testing "join-path"
+    (is (= "foo/bar" (path/path-join "foo" "bar")))
+    (is (= "foo/bar" (path/path-join "foo/" "bar")))
+    (is (= "/foo/bar/baz/asdf" (path/path-join "/foo/bar//baz/asdf/quux/..")))
+    (is (= "assets:///foo.bar/baz" (path/path-join "assets:///foo.bar" "baz")))
+    (is (= "assets:///foo.bar/baz" (path/path-join "assets:///foo.bar/" "baz")))
+    (is (= "assets:///foo.bar/baz" (path/path-join "assets:///foo.bar/" "/baz")))
+    (is (= "assets:///foo.bar/baz" (path/path-join "assets:///foo.bar" "/baz")))
+    (is (= "assets:///foo.bar/baz" (path/path-join "assets:///foo.bar" "/baz/")))
+    (is (= "assets:///foo.bar/baz" (path/path-join "assets:///foo.bar/" "/baz/")))
+    (is (= "assets:///foo.bar/baz" (path/path-join "assets:///foo.bar/" "/baz")))))
 

+ 1 - 1
src/main/frontend/components/block.cljs

@@ -200,7 +200,7 @@
                     (if (and (gp-config/local-protocol-asset? src)
                              (file-sync/current-graph-sync-on?))
                       (let [*exist? (::exist? state)
-                            ;; special handling for asset:// protcol
+                            ;; special handling for asset:// protocol
                             ;; Capacitor uses a special URL for assets loading
                             asset-path (gp-config/remove-asset-protocol src)
                             asset-path (fs/asset-path-normalize asset-path)]

+ 6 - 4
src/main/frontend/components/file.cljs

@@ -84,7 +84,11 @@
                                     (string/starts-with? path (global-config-handler/global-config-dir))
                                     [nil path]
 
-                                    ;; local file
+                                    ;; in-repo file, absolute path
+                                    (string/starts-with? path repo-dir)
+                                    [nil path]
+
+                                    ;; assume local file, relative path
                                     (not (string/starts-with? path "/"))
                                     [repo-dir path]
 
@@ -92,7 +96,7 @@
                                     [nil path])]
                    (when (and format (contains? (gp-config/text-formats) format))
                      (p/let [content (fs/read-file dir path)]
-                       (prn ::read-content content)
+                       (prn ::read-content dir path content)
                        (reset! *content (or content ""))))
                    (assoc state ::file-content *content)))
    :did-mount (fn [state]
@@ -107,11 +111,9 @@
                    (path/trim-dir-prefix repo-dir path))
         original-name (db/get-file-page (or path rel-path))
         in-db? (boolean (db/get-file (or path rel-path)))
-
         file-fpath (if in-db?
                      (path/path-join repo-dir path)
                      path)
-
         random-id (str (d/squuid))
         content (rum/react (::file-content state))]
     [:div.file {:id (str "file-edit-wrapper-" random-id)

+ 0 - 10
src/main/frontend/config.cljs

@@ -349,7 +349,6 @@
 (defonce idb-db-prefix "logseq-db/")
 (defonce local-db-prefix "logseq_local_")
 (defonce local-handle "handle")
-(defonce local-handle-prefix (str local-handle "/" local-db-prefix))
 
 (defn local-db?
   [s]
@@ -467,15 +466,6 @@
                  rpath)]
       (and (not-empty path) (gp-util/path-normalize path)))))
 
-;; NOTE: js/encodeURIComponent cannot be used here
-(defn get-page-file-path
-  "Get the path to the page file for the given page. This is used when creating new files."
-  [repo-url sub-dir page-name ext]
-  (let [page-basename (if (mobile-util/native-platform?)
-                        (js/encodeURI page-name)
-                        page-name)]
-    (get-file-path repo-url (str sub-dir "/" page-basename "." ext))))
-
 (defn get-repo-config-path
   ([]
    (get-repo-config-path (state/get-current-repo)))

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

@@ -9,7 +9,6 @@
             [frontend.routes :as routes]
             [frontend.spec]
             [frontend.log]
-            [frontend.util.persist-var :as persist-var]
             [reitit.frontend :as rf]
             [reitit.frontend.easy :as rfe]
             [logseq.api]

+ 2 - 8
src/main/frontend/fs.cljs

@@ -162,15 +162,12 @@
   [dir]
   (let [record (get-native-backend)]
     (p/let [result (protocol/open-dir record dir)]
-      (prn ::open-dir-stage-1 result)
       (when result
         (let [{:keys [path files]} result
               dir path
-              _ (prn ::open-dir dir)
               files (mapv (fn [entry]
                             (assoc entry :path (path/relative-path dir (:path entry))))
                           files)]
-          (prn :got-fixed files)
           {:path dir :files files})))))
 
 (defn get-files
@@ -180,16 +177,13 @@
   [dir]
   (let [fs-record (get-native-backend)]
     (p/let [result (protocol/get-files fs-record dir)]
-      (prn ::get-files (first result) "....")
+      (println ::get-files (count result) "files")
       (if (seq result) ;; electron, mobile, nfs
-        (let [files result ;; TODO(andelf): rm first item from electron
-              dir dir
-              _ (prn ::prepare-rel-path dir)
+        (let [files result
               files (mapv (fn [entry]
                             ;; (prn ::xx entry)
                             (assoc entry :path (path/relative-path dir (:path entry))))
                           files)]
-          (prn :got files)
           {:path dir :files files})
         result))))
 

+ 0 - 9
src/main/frontend/fs/nfs.cljs

@@ -73,15 +73,6 @@
         (add-nfs-file-handle! handle-path handle)
         (verify-permission repo true)))))
 
-(defn save-root-handle-to-idb!
-  "Save root handle to idb, keep alive across browser refresh/restart"
-  [repo repo-dir]
-  (let [handle-path (str "handle/" repo-dir)
-        handle (get-nfs-file-handle handle-path)]
-    (prn ::saving-to-idb repo repo-dir handle)
-    (when handle
-      (idb/set-item! handle-path handle))))
-
 (defn- contents-matched?
   [disk-content db-content]
   (when (and (string? disk-content) (string? db-content))

+ 0 - 13
src/main/frontend/fs/node.cljs

@@ -18,21 +18,8 @@
   (when (and (string? disk-content) (string? db-content))
     (p/resolved (= (string/trim disk-content) (string/trim db-content)))))
 
-(defn- write-file-without-backup
-  [repo dir path content ok-handler error-handler]
-  (p/catch
-   (p/let [result (ipc/ipc "writeFile" repo path content)]
-     (when ok-handler
-       (ok-handler repo path result)))
-   (fn [error]
-     (if error-handler
-       (error-handler error)
-       (log/error :write-file-failed error))))
-  )
-
 (defn- write-file-impl!
   [repo dir rpath content {:keys [ok-handler error-handler old-content skip-compare?]} stat]
-  (prn ::write-file-impl repo dir rpath)
   (let [file-fpath (path/path-join dir rpath)]
     (if skip-compare?
       (p/catch

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

@@ -18,7 +18,6 @@
             [frontend.db.react :as react]
             [frontend.error :as error]
             [frontend.extensions.srs :as srs]
-            [frontend.fs.sync :as sync]
             [frontend.handler.command-palette :as command-palette]
             [frontend.handler.events :as events]
             [frontend.handler.file :as file-handler]

+ 4 - 10
src/main/frontend/handler/repo.cljs

@@ -288,20 +288,14 @@
   (spec/validate :repos/url repo-url)
   (route-handler/redirect-to-home!)
   (state/set-parsing-state! {:graph-loading? true})
-  (let [repo-dir (config/get-local-dir repo-url)
-        _ (prn ::load-to-db-repo-dir repo-dir)
-        config (or (when-let [content (some-> (first (filter #(= (config/get-repo-config-path repo-url) (:file/path %)) file-objs))
+  (let [config (or (when-let [content (some-> (first (filter #(= (config/get-repo-config-path repo-url) (:file/path %)) file-objs))
                                               :file/content)]
                      (repo-config-handler/read-repo-config content))
                    (state/get-config repo-url))
-        _ (prn ::repo-config config)
         ;; NOTE: Use config while parsing. Make sure it's the current journal title format
         _ (state/set-config! repo-url config)
-        relate-path-fn (fn [m k]
-                         (some-> (get m k)
-                                 (string/replace (js/decodeURI (config/get-local-dir repo-url)) "")))
-        nfs-files (common-handler/remove-hidden-files file-objs config #(relate-path-fn % :file/path))
-        diffs (common-handler/remove-hidden-files diffs config #(relate-path-fn % :path))
+        nfs-files (common-handler/remove-hidden-files file-objs config :file/path)
+        diffs (common-handler/remove-hidden-files diffs config :path)
         load-contents (fn [files option]
                         (file-handler/load-files-contents!
                          repo-url
@@ -356,7 +350,7 @@
                         (when graph-exists? (ipc/ipc "graphUnlinked" repo))
                         (when (= current-repo url)
                           (state/set-current-repo! (:url (first (state/get-repos)))))))]
-    (when (or (config/local-db? url) (= url "local"))
+    (when (or (config/local-db? url) (config/demo-graph? url))
       (-> (p/let [_ (idb/clear-local-db! url)] ; clear file handles
             )
           (p/finally delete-db-f)))))

+ 2 - 32
src/main/frontend/handler/web/nfs.cljs

@@ -19,7 +19,6 @@
             [frontend.util.fs :as util-fs]
             [goog.object :as gobj]
             [lambdaisland.glogi :as log]
-            [logseq.graph-parser.config :as gp-config]
             [logseq.graph-parser.util :as gp-util]
             [promesa.core :as p]))
 
@@ -87,34 +86,6 @@
                        (keyword (util/get-file-ext (:file/path file)))))
           files))
 
-(defn- set-batch!
-  [handles]
-  (let [handles (map (fn [[path handle]]
-                       {:key   path
-                        :value handle}) handles)]
-    (idb/set-batch! handles)))
-
-(defn- set-files-aux!
-  [handles]
-  (when (seq handles)
-    (let [[h t] (split-at 50 handles)]
-      (p/let [_ (p/promise (fn [_]
-                             (js/setTimeout (fn []
-                                              (p/resolved nil)) 10)))
-              _ (set-batch! h)]
-        (when (seq t)
-          (set-files-aux! t))))))
-
-(defn- set-files!
-  [handles]
-  (let [handles (map (fn [[path handle]]
-                       (let [handle-path (str config/local-handle-prefix path)]
-                         [handle-path handle]))
-                     handles)]
-    (doseq [[path handle] handles]
-      (nfs/add-nfs-file-handle! path handle))
-    (set-files-aux! handles)))
-
 ;; TODO: extract code for `ls-dir-files` and `reload-dir!`
 (defn ls-dir-files-with-handler!
   "Read files from directory and setup repo (for the first time setup a repo)"
@@ -192,7 +163,7 @@
   ([path] (ls-dir-files-with-path! path nil))
   ([path opts]
    (when-let [dir-result-fn
-              (and path (fn [{:keys [nfs?]}]
+              (and path (fn [{:keys [_nfs?]}]
                           (p/let [files-result (fs/get-files path)]
                             files-result)))]
      (ls-dir-files-with-handler!
@@ -260,8 +231,7 @@
                                                           :nfs-files modified-files
                                                           :refresh? (not re-index?)
                                                           :new-graph? re-index?})
-                          (p/then (fn [state]
-                                    (prn :load-repo-to-db! state)
+                          (p/then (fn [_state]
                                     (ok-handler)))
                           (p/catch (fn [error]
                                      (js/console.error "load-repo-to-db" error)))))

+ 1 - 1
src/main/frontend/mobile/core.cljs

@@ -32,7 +32,7 @@
         (state/set-state! :mobile/container-urls paths)
         (println "iOS container path: " paths))))
 
-  ;; Fix iOS App direcotry change accross installation
+  ;; Fix iOS App directory change across installation
   (when (not (config/demo-graph?))
     (state/pub-event! [:validate-appId]))