Skip to content

Commit b09316a

Browse files
committed
fix: preserve file timestamps during import
1 parent 4e372ad commit b09316a

5 files changed

Lines changed: 67 additions & 19 deletions

File tree

‎deps/graph-parser/src/logseq/graph_parser/exporter.cljs‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,17 @@
4242

4343
(defn- add-missing-timestamps
4444
"Add updated-at or created-at timestamps if they doesn't exist"
45-
[block]
46-
(let [updated-at (common-util/time-ms)
47-
block (cond-> block
48-
(nil? (:block/updated-at block))
49-
(assoc :block/updated-at updated-at)
50-
(nil? (:block/created-at block))
51-
(assoc :block/created-at updated-at))]
52-
block))
45+
([block]
46+
(add-missing-timestamps block nil))
47+
([block {:keys [file-created-at file-updated-at]}]
48+
(let [updated-at (or file-updated-at (common-util/time-ms))
49+
created-at (or file-created-at updated-at)
50+
block (cond-> block
51+
(nil? (:block/updated-at block))
52+
(assoc :block/updated-at updated-at)
53+
(nil? (:block/created-at block))
54+
(assoc :block/created-at created-at))]
55+
block)))
5356

5457
(defn- build-new-namespace-page [block]
5558
(let [new-title (ns-util/get-last-part (:block/title block))]
@@ -1993,7 +1996,7 @@
19931996
(handle-math)
19941997
(update-block-marker db options)
19951998
(update-block-priority options)
1996-
add-missing-timestamps
1999+
(add-missing-timestamps options)
19972000
(dissoc :block/format :block.temp/ast-blocks)
19982001
;; ((fn [x] (prn ::block-out x) x))
19992002
)]
@@ -2008,7 +2011,7 @@
20082011
aliases))))
20092012

20102013
(defn- build-new-page-or-class
2011-
[m db per-file-state all-idents {:keys [user-options journal-created-ats]}]
2014+
[m db per-file-state all-idents {:keys [user-options journal-created-ats] :as options}]
20122015
(-> (cond-> m
20132016
;; Fix pages missing :block/title. Shouldn't happen
20142017
(not (:block/title m))
@@ -2017,7 +2020,7 @@
20172020
(update-page-alias (:page-names-to-uuids per-file-state))
20182021
(journal-created-ats (:block/name m))
20192022
(assoc :block/created-at (journal-created-ats (:block/name m))))
2020-
add-missing-timestamps
2023+
(add-missing-timestamps options)
20212024
(update-page-tags db user-options per-file-state all-idents)))
20222025

20232026
(defn- get-page-parents
@@ -2476,8 +2479,12 @@
24762479

24772480
(defn- extract-pages-and-blocks
24782481
"Main fn which calls graph-parser to convert markdown into data"
2479-
[db file content {:keys [extract-options import-state]}]
2482+
[db file content {:keys [extract-options import-state file-created-at file-updated-at]}]
24802483
(let [format (common-util/get-format file)
2484+
with-file-timestamps (fn [node]
2485+
(cond-> node
2486+
file-created-at (assoc :block/created-at file-created-at)
2487+
file-updated-at (assoc :block/updated-at file-updated-at)))
24812488
extract-options' (merge {:block-pattern (get-block-pattern format)
24822489
:date-formatter "MMM do, yyyy"
24832490
:uri-encoded? false
@@ -2490,8 +2497,13 @@
24902497
(cond (contains? #{:org :markdown :md} format)
24912498
(-> (extract/extract file content extract-options')
24922499
(update :pages (fn [pages]
2493-
(map #(dissoc % :block.temp/original-page-name) pages)))
2494-
(update :blocks fix-extracted-block-tags-and-refs))
2500+
(map #(-> %
2501+
(dissoc :block.temp/original-page-name)
2502+
with-file-timestamps)
2503+
pages)))
2504+
(update :blocks (fn [blocks]
2505+
(fix-extracted-block-tags-and-refs
2506+
(map with-file-timestamps blocks)))))
24952507

24962508
:else
24972509
(when-not (re-find #"whiteboards/.*\.edn$" (str file))
@@ -2640,7 +2652,7 @@
26402652

26412653
(defn- export-doc-file
26422654
[{:keys [path idx] :as file} conn <read-file
2643-
{:keys [notify-user set-ui-state <export-file]
2655+
{:keys [notify-user set-ui-state <export-file <get-file-stat]
26442656
:or {set-ui-state (constantly nil)
26452657
<export-file (fn <export-file [conn m opts]
26462658
(<add-file-to-db-graph conn (:file/path m) (:file/content m) opts))}
@@ -2649,8 +2661,15 @@
26492661
(-> (p/let [_ (set-ui-state [:graph/importing-state :current-idx] (inc idx))
26502662
_ (set-ui-state [:graph/importing-state :current-page] path)
26512663
content (<read-file file)
2664+
stat (when (fn? <get-file-stat)
2665+
(<get-file-stat (or (:fs-path file) path)))
2666+
created-at (or (:birthtime stat) (some-> stat .-birthtime))
2667+
modified-at (or (:mtime stat) (some-> stat .-mtime) (:last-modified-at file))
26522668
m {:file/path path :file/content content}
2653-
_ (<export-file conn m (dissoc options :set-ui-state :<export-file))]
2669+
export-options (cond-> (dissoc options :set-ui-state :<export-file)
2670+
created-at (assoc :file-created-at (.getTime created-at))
2671+
modified-at (assoc :file-updated-at (.getTime modified-at)))
2672+
_ (<export-file conn m export-options)]
26542673
;; returning val results in smoother ui updates
26552674
m)
26562675
(p/catch (fn [error]

‎deps/graph-parser/test/logseq/graph_parser/exporter_test.cljs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,29 @@ abc
737737
(is (nil? (d/entity @conn [:block/uuid missing-uuid]))
738738
"Missing OG block ref placeholder is removed"))))
739739

740+
(deftest-async export-doc-files-preserves-filesystem-timestamps
741+
(let [created-at (js/Date. "2020-01-02T03:04:05.000Z")
742+
modified-at (js/Date. "2021-06-07T08:09:10.000Z")]
743+
(p/let [file (write-temp-graph-file "pages/timestamps.md" "- timestamped\n")
744+
conn (db-test/create-conn)
745+
_ (db-pipeline/add-listener conn)
746+
doc-options (gp-exporter/build-doc-options
747+
{:macros {} :file/name-format :triple-lowbar}
748+
(merge default-export-options
749+
{:user-options {:convert-all-tags? false}
750+
:<get-file-stat (fn [_]
751+
{:birthtime created-at
752+
:mtime modified-at})
753+
:<export-file (fn [conn' file-map opts]
754+
(gp-exporter/<add-file-to-db-graph
755+
conn' (:file/path file-map) (:file/content file-map) opts))}))
756+
_ (gp-exporter/export-doc-files conn [{:path file}] <read-file doc-options)
757+
page (ldb/get-page @conn "timestamps")
758+
block (db-test/find-block-by-content @conn "timestamped")]
759+
(is (= (.getTime created-at) (:block/created-at page) (:block/created-at block)))
760+
(is (= (.getTime modified-at) (:block/updated-at page) (:block/updated-at block)))
761+
(is (empty? (:errors (db-validate/validate-local-db! @conn)))))))
762+
740763
(deftest update-asset-links-in-block-title
741764
(are [x y]
742765
(= y (@#'gp-exporter/update-asset-links-in-block-title (first x) {(second x) "UUID"} (atom {})))

‎resources/js/preload.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const fs = require('fs')
22
const path = require('path')
33
const os = require('os')
4-
const { ipcRenderer, contextBridge, shell, clipboard, webFrame } = require('electron')
4+
const { ipcRenderer, contextBridge, shell, clipboard, webFrame, webUtils } = require('electron')
55

66
const IS_MAC = process.platform === 'darwin'
77
const IS_WIN32 = process.platform === 'win32'
@@ -34,6 +34,8 @@ function getClipboardData (format) {
3434
}
3535

3636
contextBridge.exposeInMainWorld('apis', {
37+
getFilePath: (file) => webUtils.getPathForFile(file),
38+
3739
doAction: async (arg) => {
3840
return await ipcRenderer.invoke('main', arg)
3941
},

‎src/electron/electron/utils.cljs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,5 +353,6 @@
353353
[path]
354354
(let [stat (fs/statSync path)]
355355
{:size (.-size stat)
356+
:birthtime (.-birthtime stat)
356357
:mtime (.-mtime stat)
357358
:ctime (.-ctime stat)}))

‎src/main/frontend/components/imports.cljs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,11 @@
416416
import-graph-fn (or (:import-graph-fn opts)
417417
(fn [user-inputs]
418418
(let [files (->> file-objs
419-
(map #(hash-map :file-object %
420-
:path (path/trim-dir-prefix original-graph-name (.-webkitRelativePath %))))
419+
(map #(hash-map :file-object %
420+
:path (path/trim-dir-prefix original-graph-name (.-webkitRelativePath %))
421+
:fs-path (when (util/electron?)
422+
(js/window.apis.getFilePath %))
423+
:last-modified-at (some-> (.-lastModified %) js/Date.)))
421424
(remove #(and (not (string/starts-with? (:path %) "assets/"))
422425
;; TODO: Update this when supporting more formats as this aggressively excludes most formats
423426
(ignored-path? original-graph-name (.-webkitRelativePath (:file-object %))))))]

0 commit comments

Comments
 (0)