file_sync.cljs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. (ns frontend.handler.file-sync
  2. "Provides util handler fns for file sync"
  3. (:require ["path" :as path]
  4. [cljs-time.format :as tf]
  5. [cljs.core.async :as async :refer [go <!]]
  6. [cljs.core.async.interop :refer [p->c]]
  7. [clojure.string :as string]
  8. [frontend.config :as config]
  9. [frontend.db :as db]
  10. [frontend.fs.sync :as sync]
  11. [frontend.handler.notification :as notification]
  12. [frontend.state :as state]
  13. [frontend.handler.user :as user]
  14. [frontend.fs :as fs]
  15. [cljs-time.coerce :as tc]
  16. [cljs-time.core :as t]))
  17. (def *beta-unavailable? (volatile! false))
  18. (def refresh-file-sync-component (atom false))
  19. (defn get-current-graph-uuid []
  20. (state/get-current-file-sync-graph-uuid))
  21. (defn enable-sync?
  22. []
  23. (or (state/enable-sync?)
  24. config/dev?))
  25. (defn current-graph-sync-on?
  26. []
  27. (when-let [sync-state (state/sub-file-sync-state (state/get-current-file-sync-graph-uuid))]
  28. (not (sync/sync-state--stopped? sync-state))))
  29. (defn synced-file-graph?
  30. [graph]
  31. (some (fn [item] (and (= graph (:url item))
  32. (:GraphUUID item))) (state/get-repos)))
  33. (defn create-graph
  34. [name]
  35. (go
  36. (let [r* (<! (sync/<create-graph sync/remoteapi name))
  37. r (if (instance? ExceptionInfo r*) r* (:GraphUUID r*))]
  38. (if (and (not (instance? ExceptionInfo r))
  39. (string? r))
  40. (let [tx-info [0 r (user/user-uuid) (state/get-current-repo)]]
  41. (<! (apply sync/<update-graphs-txid! tx-info))
  42. (swap! refresh-file-sync-component not) tx-info)
  43. (do
  44. (state/set-state! [:ui/loading? :graph/create-remote?] false)
  45. (cond
  46. ;; already processed this exception by events
  47. ;; - :file-sync/storage-exceed-limit
  48. ;; - :file-sync/graph-count-exceed-limit
  49. (or (sync/storage-exceed-limit? r)
  50. (sync/graph-count-exceed-limit? r))
  51. nil
  52. (contains? #{400 404} (get-in (ex-data r) [:err :status]))
  53. (notification/show! (str "Create graph failed: already existed graph: " name) :warning true nil 4000)
  54. :else
  55. (notification/show! (str "Create graph failed:" r) :warning true nil 4000)))))))
  56. (defn <delete-graph
  57. [graph-uuid]
  58. (go
  59. (let [same-graph? (= graph-uuid (get-current-graph-uuid))]
  60. (when same-graph?
  61. (<! (sync/<sync-stop)))
  62. (let [r (<! (sync/<delete-graph sync/remoteapi graph-uuid))]
  63. (if (instance? ExceptionInfo r)
  64. (notification/show! (str "Delete graph failed: " graph-uuid) :warning)
  65. (do
  66. (when same-graph?
  67. (sync/clear-graphs-txid! graph-uuid)
  68. (swap! refresh-file-sync-component not))
  69. (notification/show! (str "Graph deleted") :success)))))))
  70. (defn <list-graphs
  71. []
  72. (go (:Graphs (<! (sync/<list-remote-graphs sync/remoteapi)))))
  73. (defn load-session-graphs
  74. []
  75. (when-not (state/sub [:file-sync/remote-graphs :loading])
  76. (go (state/set-state! [:file-sync/remote-graphs :loading] true)
  77. (let [graphs (<! (<list-graphs))]
  78. (state/set-state! :file-sync/remote-graphs {:loading false :graphs graphs})))))
  79. (defn reset-session-graphs
  80. []
  81. (state/set-state! :file-sync/remote-graphs {:loading false :graphs nil}))
  82. (defn init-graph [graph-uuid]
  83. (go
  84. (let [repo (state/get-current-repo)
  85. user-uuid (user/user-uuid)]
  86. (state/set-state! :sync-graph/init? true)
  87. (<! (sync/<update-graphs-txid! 0 graph-uuid user-uuid repo))
  88. (swap! refresh-file-sync-component not)
  89. (state/pub-event! [:graph/switch repo {:persist? false}]))))
  90. (defn download-version-file
  91. ([graph-uuid file-uuid version-uuid]
  92. (download-version-file graph-uuid file-uuid version-uuid false))
  93. ([graph-uuid file-uuid version-uuid silent-download?]
  94. (go
  95. (let [key (path/join file-uuid version-uuid)
  96. r (<! (sync/<download-version-files
  97. sync/rsapi graph-uuid (config/get-repo-dir (state/get-current-repo)) [key]))]
  98. (if (instance? ExceptionInfo r)
  99. (notification/show! (ex-cause r) :error)
  100. (when-not silent-download?
  101. (notification/show! [:div
  102. [:div "Downloaded version file at: "]
  103. [:div key]] :success false)))
  104. (when-not (instance? ExceptionInfo r)
  105. (path/join "logseq" "version-files" key))))))
  106. (defn- list-file-local-versions
  107. [page]
  108. (go
  109. (when-let [path (-> page :block/file :file/path)]
  110. (let [base-path (config/get-repo-dir (state/get-current-repo))
  111. rel-path (string/replace-first path base-path "")
  112. version-files-dir (->> (path/join "logseq/version-files/local" rel-path)
  113. path/parse
  114. (#(js->clj % :keywordize-keys true))
  115. ((juxt :dir :name))
  116. (apply path/join base-path))
  117. version-file-paths (<! (p->c (fs/readdir version-files-dir :path-only? true)))]
  118. (when-not (instance? ExceptionInfo version-file-paths)
  119. (when (seq version-file-paths)
  120. (mapv
  121. (fn [path]
  122. (let [create-time
  123. (-> (path/parse path)
  124. (js->clj :keywordize-keys true)
  125. :name
  126. (#(tf/parse (tf/formatter "yyyy-MM-dd'T'HH_mm_ss.SSSZZ") %)))]
  127. {:create-time create-time :path path :relative-path (string/replace-first path base-path "")}))
  128. version-file-paths)))))))
  129. (defn fetch-page-file-versions [graph-uuid page]
  130. []
  131. (let [file-id (:db/id (:block/file page))]
  132. (when-let [path (:file/path (db/entity file-id))]
  133. (let [base-path (config/get-repo-dir (state/get-current-repo))
  134. base-path (if (string/starts-with? base-path "file://")
  135. (js/decodeURIComponent base-path)
  136. base-path)
  137. path* (string/replace-first (string/replace-first path base-path "") #"^/" "")]
  138. (go
  139. (let [version-list (:VersionList
  140. (<! (sync/<get-remote-file-versions sync/remoteapi graph-uuid path*)))
  141. local-version-list (<! (list-file-local-versions page))
  142. all-version-list (->> (concat version-list local-version-list)
  143. (sort-by #(or (:CreateTime %)
  144. (:create-time %))
  145. >))]
  146. all-version-list))))))
  147. (defn init-remote-graph
  148. [local graph]
  149. (when (and local graph)
  150. (notification/show!
  151. (str "Start syncing the remote graph "
  152. (:GraphName graph)
  153. " to "
  154. (config/get-string-repo-dir (config/get-local-dir local)))
  155. :success)
  156. (init-graph (:GraphUUID graph))
  157. (state/close-modal!)))
  158. (defn setup-file-sync-event-listeners
  159. []
  160. (let [c (async/chan 1)
  161. p sync/sync-events-publication
  162. topics [:finished-local->remote :finished-remote->local :start]]
  163. (doseq [topic topics]
  164. (async/sub p topic c))
  165. (async/go-loop []
  166. (let [{:keys [event data]} (async/<! c)]
  167. (case event
  168. (list :finished-local->remote :finished-remote->local)
  169. (when-let [current-uuid (state/get-current-file-sync-graph-uuid)]
  170. (state/clear-file-sync-progress! current-uuid)
  171. (state/set-state! [:file-sync/graph-state current-uuid :file-sync/last-synced-at] (:epoch data)))
  172. :start
  173. (when-let [current-uuid (state/get-current-file-sync-graph-uuid)]
  174. (state/set-state! [:file-sync/graph-state current-uuid :file-sync/start-time] data))
  175. nil)
  176. (when (and (:file-change-events data)
  177. (= :page (state/get-current-route)))
  178. (state/pub-event! [:file-sync/maybe-onboarding-show :sync-history])))
  179. (recur))
  180. #(doseq [topic topics]
  181. (async/unsub p topic c))))
  182. (defn reset-user-state! []
  183. (vreset! *beta-unavailable? false)
  184. (state/set-state! :file-sync/onboarding-state nil))
  185. (defn calculate-time-left
  186. "This assumes that the network speed is stable which could be wrong sometimes."
  187. [sync-state progressing]
  188. (when-let [start-time (get-in @state/state
  189. [:file-sync/graph-state
  190. (state/get-current-file-sync-graph-uuid)
  191. :file-sync/start-time
  192. :epoch])]
  193. (let [now (tc/to-epoch (t/now))
  194. diff-seconds (- now start-time)
  195. finished (reduce + (map (comp :progress second) progressing))
  196. local->remote-files (:full-local->remote-files sync-state)
  197. remote->local-files (:full-remote->local-files sync-state)
  198. total (if (seq remote->local-files)
  199. (reduce + (map (fn [m] (or (:size m) 0)) remote->local-files))
  200. (reduce + (map #(:size (.-stat %)) local->remote-files)))
  201. mins (int (/ (* (/ total finished) diff-seconds) 60))]
  202. (if (or (zero? total) (zero? finished))
  203. "waiting"
  204. (cond
  205. (zero? mins) "soon"
  206. (= mins 1) "1 min left"
  207. (> mins 30) "calculating..."
  208. :else (str mins " mins left"))))))