🎉 Import/export thumbnails in .penpot files

This commit is contained in:
Aitor 2023-11-03 10:04:37 +01:00 committed by Andrey Antukh
parent 617edd0fa8
commit 91f7874167

View file

@ -336,6 +336,14 @@
(->> (db/exec! conn [sql ids]) (->> (db/exec! conn [sql ids])
(mapv #(assoc % :file-id id)))))) (mapv #(assoc % :file-id id))))))
(defn- get-file-thumbnails
"Return all file thumbnails for a given file."
[{:keys [::db/pool]} id]
(pu/with-open [conn (db/open pool)]
(let [sql "SELECT * FROM file_tagged_object_thumbnail WHERE file_id = ?"]
(->> (db/exec! conn [sql id])
(mapv #(dissoc % :file-id))))))
(def ^:private storage-object-id-xf (def ^:private storage-object-id-xf
(comp (comp
(mapcat (juxt :media-id :thumbnail-id)) (mapcat (juxt :media-id :thumbnail-id))
@ -511,14 +519,19 @@
(doseq [file-id (-> *state* deref :files)] (doseq [file-id (-> *state* deref :files)]
(let [detach? (and (not embed-assets?) (not include-libraries?)) (let [detach? (and (not embed-assets?) (not include-libraries?))
file (cond-> (get-file cfg file-id) thumbnails (get-file-thumbnails cfg file-id)
detach? file (cond-> (get-file cfg file-id)
(-> (ctf/detach-external-references file-id) detach?
(dissoc :libraries)) (-> (ctf/detach-external-references file-id)
embed-assets? (dissoc :libraries))
(update :data embed-file-assets cfg file-id))
media (get-file-media cfg file)] embed-assets?
(update :data embed-file-assets cfg file-id)
:always
(assoc :thumbnails thumbnails))
media (get-file-media cfg file)]
(l/dbg :hint "write penpot file" (l/dbg :hint "write penpot file"
:id file-id :id file-id
@ -534,7 +547,8 @@
(write-obj! file) (write-obj! file)
(write-obj! media)) (write-obj! media))
(vswap! *state* update :sids into storage-object-id-xf media)))) (vswap! *state* update :sids into storage-object-id-xf media)
(vswap! *state* update :sids into storage-object-id-xf thumbnails))))
(defmethod write-section :v1/rels (defmethod write-section :v1/rels
[{:keys [::output ::include-libraries?] :as cfg}] [{:keys [::output ::include-libraries?] :as cfg}]
@ -676,6 +690,20 @@
(not (contains? cfeat/*previous* "fdata/pointer-map"))) (not (contains? cfeat/*previous* "fdata/pointer-map")))
(features.fdata/enable-pointer-map))) (features.fdata/enable-pointer-map)))
(defn- update-thumbnail
[thumbnail file-id]
(let [thumbnail (assoc thumbnail :file-id file-id)
object-id (:object-id thumbnail)
object-id (str/replace-first object-id #"^(.*?)/" (str file-id "/"))
thumbnail (assoc thumbnail :object-id object-id)]
thumbnail))
(defn- update-file-thumbnails
[file file-id]
(let [thumbnails (:thumbnails file)]
(mapv #(update-thumbnail % file-id) thumbnails)))
(defmethod read-section :v1/files (defmethod read-section :v1/files
[{:keys [::db/conn ::input ::project-id ::enabled-features ::timestamp ::overwrite?]}] [{:keys [::db/conn ::input ::project-id ::enabled-features ::timestamp ::overwrite?]}]
@ -688,8 +716,7 @@
features (-> enabled-features features (-> enabled-features
(set/difference cfeat/frontend-only-features) (set/difference cfeat/frontend-only-features)
(set/union (cfeat/check-supported-features! (:features file)))) (set/union (cfeat/check-supported-features! (:features file))))]
]
;; All features that are enabled and requires explicit migration ;; All features that are enabled and requires explicit migration
;; are added to the state for a posterior migration step ;; are added to the state for a posterior migration step
@ -705,6 +732,11 @@
:expected-id expected-file-id :expected-id expected-file-id
:hint "the penpot file seems corrupt, found unexpected uuid (file-id)")) :hint "the penpot file seems corrupt, found unexpected uuid (file-id)"))
(when (contains? file :thumbnails)
(let [updated-thumbnails (update-file-thumbnails file file-id')]
(vswap! *state* update :thumbnails (fnil into []) updated-thumbnails)
(l/dbg :hint "thumbnails updated" :thumbnails (count updated-thumbnails) ::l/sync? true)))
;; Update index using with media ;; Update index using with media
(l/dbg :hint "update index with media" ::l/sync? true) (l/dbg :hint "update index with media" ::l/sync? true)
(vswap! *state* update :index update-index (map :id media')) (vswap! *state* update :index update-index (map :id media'))
@ -729,6 +761,7 @@
(assoc :project-id project-id) (assoc :project-id project-id)
(assoc :created-at timestamp) (assoc :created-at timestamp)
(assoc :modified-at timestamp) (assoc :modified-at timestamp)
(dissoc :thumbnails)
(update :data (fn [data] (update :data (fn [data]
(-> data (-> data
(dissoc :recent-colors) (dissoc :recent-colors)
@ -838,7 +871,16 @@
(assoc :file-id file-id) (assoc :file-id file-id)
(d/update-when :media-id lookup-index) (d/update-when :media-id lookup-index)
(d/update-when :thumbnail-id lookup-index)) (d/update-when :thumbnail-id lookup-index))
{:on-conflict-do-nothing overwrite?})))))) {:on-conflict-do-nothing overwrite?}))))
(doseq [item (:thumbnails @*state*)]
(l/dbg :hint "inserting file tagged object thumbnail"
:file-id (:file-id item)
:object-id (:object-id item)
::l/sync? true)
(db/insert! conn :file-tagged-object-thumbnail
(update item :media-id lookup-index)
{:on-conflict-do-nothing overwrite?}))))
(defn- lookup-index (defn- lookup-index
[id] [id]
@ -980,8 +1022,8 @@
(l/info :hint "import: terminated" (l/info :hint "import: terminated"
:import-id id :import-id id
:elapsed (dt/format-duration (tp)) :elapsed (dt/format-duration (tp))
:error? (some? @cs) :error? (some? @cs))))))
)))))
;; --- Command: export-binfile ;; --- Command: export-binfile