fix: importing assets in subdirectories

Also fix lint
This commit is contained in:
Gabriel Horner
2025-06-12 17:58:58 -04:00
committed by Tienson Qin
parent 4524fc8370
commit bb31c4e6f1
7 changed files with 19 additions and 12 deletions

View File

@@ -9,6 +9,7 @@
logseq.db.common.order
logseq.db.sqlite.create-graph
logseq.db.frontend.malli-schema
logseq.db.frontend.asset
;; Some fns are used by frontend but not worth moving over yet
logseq.db.frontend.schema
logseq.db.frontend.validate

View File

@@ -53,7 +53,7 @@
(p/let [buffer (fs/readFileSync (:path file))
checksum (db-asset/<get-file-array-buffer-checksum buffer)]
(swap! assets assoc
(node-path/basename (:path file))
(gp-exporter/asset-path->name (:path file))
{:size (.-length buffer)
:checksum checksum
:type (db-asset/asset-path->type (:path file))

View File

@@ -20,6 +20,7 @@
[logseq.db :as ldb]
[logseq.db.common.order :as db-order]
[logseq.db.common.property-util :as db-property-util]
[logseq.db.frontend.asset :as db-asset]
[logseq.db.frontend.class :as db-class]
[logseq.db.frontend.content :as db-content]
[logseq.db.frontend.db-ident :as db-ident]
@@ -32,8 +33,7 @@
[logseq.graph-parser.block :as gp-block]
[logseq.graph-parser.extract :as extract]
[logseq.graph-parser.property :as gp-property]
[promesa.core :as p]
[logseq.db.frontend.asset :as db-asset]))
[promesa.core :as p]))
(defn- add-missing-timestamps
"Add updated-at or created-at timestamps if they doesn't exist"
@@ -888,6 +888,12 @@
(:block/name (:block/parent block))
(assoc :block/parent {:block/uuid (get-page-uuid page-names-to-uuids (:block/name (:block/parent block)) {:block block :block/parent (:block/parent block)})})))
(defn asset-path->name
"Given an asset's relative or full path, create a unique name for identifying an asset.
Must handle to paths as ../assets/*, assets/* and with subdirectories"
[path]
(re-find #"assets/.*$" path))
(defn- handle-assets-in-block
[block block-ast {:keys [assets ignored-assets]}]
(let [asset-links
@@ -899,7 +905,7 @@
(filter #(and (= "Link" (first %))
(common-config/local-asset? (second (:url (second %))))))))))
asset-link (first asset-links)
asset-name (some-> asset-link second :url second node-path/basename)]
asset-name (some->> asset-link second :url second asset-path->name)]
(when (> (count asset-links) 1)
(swap! ignored-assets into
(map #(hash-map
@@ -910,7 +916,7 @@
(if asset-name
(if-let [asset-data (get @assets asset-name)]
(do
(prn :asset-added! asset-name (get @assets asset-name))
(prn :asset-added! (node-path/basename asset-name) #_(get @assets asset-name))
;; (cljs.pprint/pprint asset-link)
(swap! assets assoc-in [asset-name :block/uuid] (:block/uuid block))
(merge block
@@ -918,7 +924,7 @@
:logseq.property.asset/type (:type asset-data)
:logseq.property.asset/checksum (:checksum asset-data)
:logseq.property.asset/size (:size asset-data)
:block/title (db-asset/asset-name->title asset-name)}
:block/title (db-asset/asset-name->title (node-path/basename asset-name))}
(when-let [metadata (not-empty (common-util/safe-read-map-string (:metadata (second asset-link))))]
{:logseq.property.asset/resize-metadata metadata})))
(do

View File

@@ -99,7 +99,7 @@
(p/let [buffer (fs/readFileSync (:path file))
checksum (db-asset/<get-file-array-buffer-checksum buffer)]
(swap! assets assoc
(node-path/basename (:path file))
(gp-exporter/asset-path->name (:path file))
{:size (.-length buffer)
:checksum checksum
:type (db-asset/asset-path->type (:path file))
@@ -183,9 +183,9 @@
;; Counts
;; Includes journals as property values e.g. :logseq.property/deadline
(is (= 25 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Journal]] @conn))))
(is (= 26 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Journal]] @conn))))
(is (= 1 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Asset]] @conn))))
(is (= 2 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Asset]] @conn))))
(is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Task]] @conn))))
(is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Query]] @conn))))
(is (= 2 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Card]] @conn))))
@@ -211,7 +211,7 @@
(is (= 0 (count @(:ignored-properties import-state))) "No ignored properties")
(is (= 0 (count @(:ignored-assets import-state))) "No ignored assets")
(is (= 1 (count @(:ignored-files import-state))) "Ignore .edn for now")
(is (= 1 (count @assets))))
(is (= 2 (count @assets))))
(testing "logseq files"
(is (= ".foo {}\n"

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

View File

@@ -0,0 +1 @@
- ![dino!](assets/subdir/partydino.gif){:width 105} tests an asset with a manual link, custom title and in a subdirectory

View File

@@ -36,7 +36,6 @@
[logseq.shui.ui :as shui]
[promesa.core :as p]
[rum.core :as rum]
["path" :as node-path]
[logseq.common.config :as common-config]))
;; Can't name this component as `frontend.components.import` since shadow-cljs
@@ -353,7 +352,7 @@
(p/then (fn [buffer]
(p/let [checksum (db-asset/<get-file-array-buffer-checksum buffer)]
(swap! assets assoc
(node-path/basename (:path file))
(gp-exporter/asset-path->name (:path file))
{:size (.-size (:file-object file))
:checksum checksum
:type (db-asset/asset-path->type (:path file))