enhance: import assets handles

unused assets by still copying them by name and handles
existing assets
This commit is contained in:
Gabriel Horner
2025-06-13 14:28:01 -04:00
committed by Tienson Qin
parent bb31c4e6f1
commit f318d68530
4 changed files with 39 additions and 27 deletions

View File

@@ -62,7 +62,12 @@
(defn- <copy-asset-file [asset-m db-graph-dir]
(p/let [parent-dir (node-path/join db-graph-dir common-config/local-assets-dir)
_ (fsp/mkdir parent-dir #js {:recursive true})]
(fsp/copyFile (:path asset-m) (node-path/join parent-dir (str (:block/uuid asset-m) "." (:type asset-m))))))
(if (:block/uuid asset-m)
(fsp/copyFile (:path asset-m) (node-path/join parent-dir (str (:block/uuid asset-m) "." (:type asset-m))))
(do
(println "[INFO]" "Copied asset" (pr-str (node-path/basename (:path asset-m)))
"by its name since it was unused.")
(fsp/copyFile (:path asset-m) (node-path/join parent-dir (node-path/basename (:path asset-m))))))))
(defn- notify-user [{:keys [continue debug]} m]
(println (:msg m))

View File

@@ -915,18 +915,21 @@
(rest asset-links))))
(if asset-name
(if-let [asset-data (get @assets asset-name)]
(do
(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
{:block/tags [:logseq.class/Asset]
: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 (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})))
(if (:block/uuid asset-data)
;; Link to existing assets instead of creating duplicates to preserve identity
(assoc block :block/title (page-ref/->page-ref (:block/uuid asset-data)))
(do
(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
{:block/tags [:logseq.class/Asset]
: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 (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
(swap! ignored-assets conj
{:reason "Asset file was not found when reading assets"
@@ -1659,15 +1662,12 @@
(sort-by :path asset-maps*)
(range 0 (count asset-maps*)))
copy-asset (fn copy-asset [{:keys [path] :as asset-m}]
(if (nil? (:block/uuid asset-m))
(notify-user {:msg (str "Import failed to copy " (pr-str path) " because the asset has no :block/uuid")
:ex-data {:path path}})
(p/catch
(<copy-asset-file asset-m)
(fn [error]
(notify-user {:msg (str "Import failed to copy " (pr-str path) " with error:\n" (.-message error))
:level :error
:ex-data {:path path :error error}})))))]
(p/catch
(<copy-asset-file asset-m)
(fn [error]
(notify-user {:msg (str "Import failed to copy " (pr-str path) " with error:\n" (.-message error))
:level :error
:ex-data {:path path :error error}}))))]
(when (seq asset-maps)
(set-ui-state [:graph/importing-state :current-page] "Copy asset files")
(<safe-async-loop copy-asset asset-maps notify-user))))

View File

@@ -1 +1,2 @@
- ![dino!](assets/subdir/partydino.gif){:width 105} tests an asset with a manual link, custom title and in a subdirectory
- ![dino!](assets/subdir/partydino.gif){:width 105} tests an asset with a manual link, custom title and in a subdirectory
- ![greg-popovich-thumbs-up.png](../assets/greg-popovich-thumbs-up_1704749687791_0.png){:height 288, :width 252}

View File

@@ -1,6 +1,7 @@
(ns frontend.components.imports
"Import data into Logseq."
(:require [cljs-time.core :as t]
(:require ["path" :as node-path]
[cljs-time.core :as t]
[cljs.pprint :as pprint]
[clojure.string :as string]
[frontend.components.onboarding.setups :as setups]
@@ -26,6 +27,7 @@
[goog.functions :refer [debounce]]
[goog.object :as gobj]
[lambdaisland.glogi :as log]
[logseq.common.config :as common-config]
[logseq.common.path :as path]
[logseq.db.frontend.asset :as db-asset]
[logseq.db.frontend.validate :as db-validate]
@@ -35,8 +37,7 @@
[logseq.shui.hooks :as hooks]
[logseq.shui.ui :as shui]
[promesa.core :as p]
[rum.core :as rum]
[logseq.common.config :as common-config]))
[rum.core :as rum]))
;; Can't name this component as `frontend.components.import` since shadow-cljs
;; will complain about it.
@@ -367,7 +368,12 @@
assets-dir (path/path-join repo-dir common-config/local-assets-dir)]
(p/do!
(fs/mkdir-if-not-exists assets-dir)
(fs/write-plain-text-file! repo assets-dir (str (:block/uuid asset-m) "." (:type asset-m)) content {:skip-transact? true})))))))
(if (:block/uuid asset-m)
(fs/write-plain-text-file! repo assets-dir (str (:block/uuid asset-m) "." (:type asset-m)) content {:skip-transact? true})
(do
(println "Copied asset" (pr-str (node-path/basename (:path asset-m)))
"by its name since it was unused.")
(fs/write-plain-text-file! repo assets-dir (node-path/basename (:path asset-m)) content {:skip-transact? true})))))))))
(defn- import-file-graph
[*files