Browse Source

enhance: add logseq/.recycle to store removed files for backup

Tienson Qin 4 years ago
parent
commit
f2ce4a1177

+ 9 - 5
src/electron/electron/handler.cljs

@@ -40,8 +40,15 @@
 (defmethod handle :readdir [_window [_ dir]]
   (readdir dir))
 
-(defmethod handle :unlink [_window [_ path]]
-  (fs/unlinkSync path))
+(defmethod handle :unlink [_window [_ repo path]]
+  (let [basename (path/basename path)
+        file-name (-> (string/replace path (str repo "/") "")
+                      (string/replace "/" "_")
+                      (string/replace "\\" "_"))
+        recycle-dir (str repo "/logseq/.recycle")
+        _ (fs-extra/ensureDirSync recycle-dir)
+        new-path (str recycle-dir "/" file-name)]
+    (fs/renameSync path new-path)))
 
 (defmethod handle :readFile [_window [_ path]]
   (utils/read-file path))
@@ -57,8 +64,6 @@
 (defmethod handle :stat [_window [_ path]]
   (fs/statSync path))
 
-
-
 (defonce allowed-formats
   #{:org :markdown :md :edn :json :css :excalidraw})
 
@@ -177,7 +182,6 @@
 (defmethod handle :quitApp []
   (.quit app))
 
-
 (defmethod handle :default [args]
   (println "Error: no ipc handler for: " (bean/->js args)))
 

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

@@ -289,6 +289,7 @@
 
 (defonce local-repo "local")
 (defonce local-assets-dir "assets")
+(defonce recycle-dir ".recycle")
 (def config-file "config.edn")
 (def custom-css-file "custom.css")
 (def metadata-file "metadata.edn")

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

@@ -52,8 +52,9 @@
   (protocol/readdir (get-fs dir) dir))
 
 (defn unlink!
-  [path opts]
-  (protocol/unlink! (get-fs path) path opts))
+  "Should move the path to logseq/recycle instead of deleting it."
+  [repo path opts]
+  (protocol/unlink! (get-fs path) repo path opts))
 
 (defn rmdir!
   "Remove the directory recursively.

+ 1 - 1
src/main/frontend/fs/bfs.cljs

@@ -14,7 +14,7 @@
   (readdir [this dir]
     (when js/window.pfs
       (js/window.pfs.readdir dir)))
-  (unlink! [this path opts]
+  (unlink! [this repo path opts]
     (when js/window.pfs
       (p/let [stat (js/window.pfs.stat path)]
         (if (= (.-type stat) "file")

+ 23 - 5
src/main/frontend/fs/nfs.cljs

@@ -57,7 +57,11 @@
 (defrecord Nfs []
   protocol/Fs
   (mkdir! [this dir]
-    (let [[root new-dir] (rest (string/split dir "/"))
+    (let [parts (->> (string/split dir "/")
+                     (remove string/blank?))
+          root (->> (butlast parts)
+                    (string/join "/"))
+          new-dir (last parts)
           root-handle (str "handle/" root)]
       (->
        (p/let [handle (idb/get-item root-handle)
@@ -82,12 +86,25 @@
             (map (fn [path]
                    (string/replace path prefix "")))))))
 
-  (unlink! [this path opts]
+  (unlink! [this repo path opts]
     (let [[dir basename] (util/get-dir-and-basename path)
           handle-path (str "handle" path)]
       (->
-       (p/let [handle (idb/get-item (str "handle" dir))
-               _ (idb/remove-item! handle-path)]
+       (p/let [recycle-dir (str "/" repo (util/format "/%s/%s" config/app-name config/recycle-dir))
+               _ (protocol/mkdir! this recycle-dir)
+               handle (idb/get-item handle-path)
+               file (.getFile handle)
+               content (.text file)
+               handle (idb/get-item (str "handle" dir))
+               _ (idb/remove-item! handle-path)
+               file-name (-> (string/replace path (str "/" repo "/") "")
+                             (string/replace "/" "_")
+                             (string/replace "\\" "_"))
+               new-path (str recycle-dir "/" file-name)
+               _ (protocol/write-file! this repo
+                                       "/"
+                                       new-path
+                                       content nil)]
          (when handle
            (.removeEntry ^js handle basename))
          (remove-nfs-file-handle! handle-path))
@@ -119,6 +136,7 @@
           handle-path (if (= "/" (last sub-dir-handle-path))
                         (subs sub-dir-handle-path 0 (dec (count sub-dir-handle-path)))
                         sub-dir-handle-path)
+          handle-path (string/replace handle-path "//" "/")
           basename-handle-path (str handle-path "/" basename)]
       (p/let [file-handle (idb/get-item basename-handle-path)]
         (when file-handle
@@ -189,7 +207,7 @@
             file (.getFile handle)
             content (.text file)
             _ (protocol/write-file! this repo dir new-path content nil)]
-      (protocol/unlink! this old-path nil)))
+      (protocol/unlink! this repo old-path nil)))
   (stat [this dir path]
     (if-let [file (get-nfs-file-handle (str "handle/"
                                             (string/replace-first dir "/" "")

+ 6 - 3
src/main/frontend/fs/node.cljs

@@ -7,7 +7,8 @@
             [electron.ipc :as ipc]
             [cljs-bean.core :as bean]
             [goog.object :as gobj]
-            [lambdaisland.glogi :as log]))
+            [lambdaisland.glogi :as log]
+            [frontend.config :as config]))
 
 (defn concat-path
   [dir path]
@@ -69,8 +70,10 @@
     (ipc/ipc "mkdir-recur" dir))
   (readdir [this dir]                   ; recursive
     (ipc/ipc "readdir" dir))
-  (unlink! [this path _opts]
-    (ipc/ipc "unlink" path))
+  (unlink! [this repo path _opts]
+    (ipc/ipc "unlink"
+             (config/get-repo-dir repo)
+             path))
   (rmdir! [this dir]
     ;; Too dangerious!!! We'll never implement this.
     nil)

+ 1 - 1
src/main/frontend/fs/protocol.cljs

@@ -4,7 +4,7 @@
   (mkdir! [this dir])
   (mkdir-recur! [this dir])
   (readdir [this dir])
-  (unlink! [this path opts])
+  (unlink! [this repo path opts])
   (rmdir! [this dir])
   (read-file [this dir path opts])
   (write-file! [this repo dir path content opts])

+ 2 - 1
src/main/frontend/handler/editor.cljs

@@ -1413,7 +1413,8 @@
     (when (and local? delete-local?)
       ;; FIXME: should be relative to current block page path
       (when-let [href (if (util/electron?) href (second (re-find #"\((.+)\)$" full-text)))]
-        (fs/unlink! (config/get-repo-path
+        (fs/unlink! repo
+                    (config/get-repo-path
                      repo (-> href
                               (string/replace #"^../" "/")
                               (string/replace #"^assets://" ""))) nil)))))

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

@@ -254,7 +254,7 @@
   (when-not (string/blank? file)
     (->
      (p/let [_ (or (config/local-db? repo) (git/remove-file repo file))
-             _ (fs/unlink! (config/get-repo-path repo file) nil)]
+             _ (fs/unlink! repo (config/get-repo-path repo file) nil)]
        (when-let [file (db/entity repo [:file/path file])]
          (common-handler/check-changed-files-status)
          (let [file-id (:db/id file)

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

@@ -173,7 +173,7 @@
               ;; remove file
               (->
                (p/let [_ (or (config/local-db? repo) (git/remove-file repo file-path))
-                       _ (fs/unlink! (config/get-repo-path repo file-path) nil)]
+                       _ (fs/unlink! repo (config/get-repo-path repo file-path) nil)]
                  (common-handler/check-changed-files-status)
                  (repo-handler/push-if-auto-enabled! repo))
                (p/catch (fn [err]

+ 9 - 6
src/main/frontend/handler/repo.cljs

@@ -151,12 +151,15 @@
    (create-default-files! repo-url false))
   ([repo-url encrypted?]
    (spec/validate :repos/url repo-url)
-   (file-handler/create-metadata-file repo-url encrypted?)
-   ;; TODO: move to frontend.handler.file
-   (create-config-file-if-not-exists repo-url)
-   (create-today-journal-if-not-exists repo-url {:write-file? false})
-   (create-contents-file repo-url)
-   (create-custom-theme repo-url)))
+   (let [repo-dir (config/get-repo-dir repo-url)]
+     (p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/" config/app-name))
+             _ (fs/mkdir-if-not-exists (str repo-dir "/" config/app-name "/" config/recycle-dir))]
+       (file-handler/create-metadata-file repo-url encrypted?)
+       ;; TODO: move to frontend.handler.file
+       (create-config-file-if-not-exists repo-url)
+       (create-today-journal-if-not-exists repo-url {:write-file? false})
+       (create-contents-file repo-url)
+       (create-custom-theme repo-url)))))
 
 (defn- remove-non-exists-refs!
   [data]

+ 2 - 2
src/main/logseq/api.cljs

@@ -132,7 +132,7 @@
           _ (if-not sub-dir? (do (log/info :debug user-path) (throw "access file denied")))
           exist? (fs/file-exists? "" user-path)
           _ (when-not exist? (do (log/info :debug user-path) (throw "file not existed")))
-          _ (fs/unlink! user-path {})]))
+          _ (fs/unlink! repo user-path {})]))
 
 (def ^:export write_user_tmp_file
   (fn [file content]
@@ -170,7 +170,7 @@
   (fn [plugin-id]
     (p/let [root (plugin-handler/get-ls-dotdir-root)
             plugin-id (util/node-path.basename plugin-id)]
-      (fs/rmdir! (util/node-path.join root  "storages" plugin-id)))))
+      (fs/rmdir! (util/node-path.join root "storages" plugin-id)))))
 
 (def ^:export load_user_preferences
   (fn []