mirror of
https://github.com/penpot/penpot.git
synced 2025-05-19 15:36:11 +02:00
🐛 Fix many bugs on onboarding file loading process.
This commit is contained in:
parent
61ad112451
commit
2a8a0afd09
1 changed files with 90 additions and 45 deletions
|
@ -10,11 +10,15 @@
|
||||||
(ns app.setup.initial-data
|
(ns app.setup.initial-data
|
||||||
(:refer-clojure :exclude [load])
|
(:refer-clojure :exclude [load])
|
||||||
(:require
|
(:require
|
||||||
|
[app.common.data :as d]
|
||||||
|
[app.common.pages.migrations :as pmg]
|
||||||
[app.common.uuid :as uuid]
|
[app.common.uuid :as uuid]
|
||||||
[app.config :as cfg]
|
[app.config :as cfg]
|
||||||
[app.db :as db]
|
[app.db :as db]
|
||||||
[app.rpc.mutations.projects :as projects]
|
[app.rpc.mutations.projects :as projects]
|
||||||
[app.rpc.queries.profile :as profile]))
|
[app.rpc.queries.profile :as profile]
|
||||||
|
[app.util.blob :as blob]
|
||||||
|
[clojure.walk :as walk]))
|
||||||
|
|
||||||
;; --- DUMP GENERATION
|
;; --- DUMP GENERATION
|
||||||
|
|
||||||
|
@ -38,47 +42,76 @@
|
||||||
([system project-id {:keys [skey project-name]
|
([system project-id {:keys [skey project-name]
|
||||||
:or {project-name "Penpot Onboarding"}}]
|
:or {project-name "Penpot Onboarding"}}]
|
||||||
(db/with-atomic [conn (:app.db/pool system)]
|
(db/with-atomic [conn (:app.db/pool system)]
|
||||||
(let [skey (or skey (cfg/get :initial-project-skey))
|
(let [skey (or skey (cfg/get :initial-project-skey))
|
||||||
file (db/exec! conn [sql:file project-id])
|
files (db/exec! conn [sql:file project-id])
|
||||||
file-library-rel (db/exec! conn [sql:file-library-rel project-id])
|
flibs (db/exec! conn [sql:file-library-rel project-id])
|
||||||
file-media-object (db/exec! conn [sql:file-media-object project-id])
|
fmeds (db/exec! conn [sql:file-media-object project-id])
|
||||||
data {:project-name project-name
|
data {:project-name project-name
|
||||||
:file file
|
:files files
|
||||||
:file-library-rel file-library-rel
|
:flibs flibs
|
||||||
:file-media-object file-media-object}]
|
:fmeds fmeds}]
|
||||||
|
|
||||||
(db/delete! conn :server-prop
|
(db/delete! conn :server-prop {:id skey})
|
||||||
{:id skey})
|
|
||||||
(db/insert! conn :server-prop
|
(db/insert! conn :server-prop
|
||||||
{:id skey
|
{:id skey
|
||||||
:preload false
|
:preload false
|
||||||
:content (db/tjson data)})
|
:content (db/tjson data)})
|
||||||
nil))))
|
skey))))
|
||||||
|
|
||||||
|
|
||||||
;; --- DUMP LOADING
|
;; --- DUMP LOADING
|
||||||
|
|
||||||
(defn- remap-ids
|
(defn- process-file
|
||||||
"Given a collection and a map from ID to ID. Changes all the `keys`
|
[file index]
|
||||||
properties so they point to the new ID existing in `map-ids`"
|
(letfn [(process-form [form]
|
||||||
[map-ids coll keys]
|
(cond-> form
|
||||||
(let [generate-id
|
;; Relink Components
|
||||||
(fn [map-ids {:keys [id]}]
|
(and (map? form)
|
||||||
(assoc map-ids id (uuid/next)))
|
(uuid? (:component-file form)))
|
||||||
|
(update :component-file #(get index % %))
|
||||||
|
|
||||||
remap-key
|
;; Relink Image Shapes
|
||||||
(fn [obj map-ids key]
|
(and (map? form)
|
||||||
(cond-> obj
|
(map? (:metadata form))
|
||||||
(contains? obj key)
|
(= :image (:type form)))
|
||||||
(assoc key (get map-ids (get obj key) (get obj key)))))
|
(update-in [:metadata :id] #(get index % %))))
|
||||||
|
|
||||||
change-id
|
;; A function responsible to analize all file data and
|
||||||
(fn [map-ids obj]
|
;; replace the old :component-file reference with the new
|
||||||
(reduce #(remap-key %1 map-ids %2) obj keys))
|
;; ones, using the provided file-index
|
||||||
|
(relink-shapes [data]
|
||||||
|
(walk/postwalk process-form data))
|
||||||
|
|
||||||
new-map-ids (reduce generate-id map-ids coll)]
|
;; A function responsible of process the :media attr of file
|
||||||
|
;; data and remap the old ids with the new ones.
|
||||||
|
(relink-media [media]
|
||||||
|
(reduce-kv (fn [res k v]
|
||||||
|
(let [id (get index k)]
|
||||||
|
(if (uuid? id)
|
||||||
|
(-> res
|
||||||
|
(assoc id (assoc v :id id))
|
||||||
|
(dissoc k))
|
||||||
|
res)))
|
||||||
|
media
|
||||||
|
media))]
|
||||||
|
|
||||||
[new-map-ids (map (partial change-id new-map-ids) coll)]))
|
(update file :data
|
||||||
|
(fn [data]
|
||||||
|
(-> data
|
||||||
|
(blob/decode)
|
||||||
|
(assoc :id (:id file))
|
||||||
|
(pmg/migrate-data)
|
||||||
|
(update :pages-index relink-shapes)
|
||||||
|
(update :components relink-shapes)
|
||||||
|
(update :media relink-media)
|
||||||
|
(d/without-nils)
|
||||||
|
(blob/encode))))))
|
||||||
|
|
||||||
|
(defn- remap-id
|
||||||
|
[item index key]
|
||||||
|
(cond-> item
|
||||||
|
(contains? item key)
|
||||||
|
(assoc key (get index (get item key) (get item key)))))
|
||||||
|
|
||||||
(defn- retrieve-data
|
(defn- retrieve-data
|
||||||
[conn skey]
|
[conn skey]
|
||||||
|
@ -97,19 +130,26 @@
|
||||||
:team-id (:default-team-id profile)
|
:team-id (:default-team-id profile)
|
||||||
:name (:project-name data)})
|
:name (:project-name data)})
|
||||||
|
|
||||||
map-ids {}
|
index (as-> {} index
|
||||||
|
(reduce #(assoc %1 (:id %2) (uuid/next)) index (:files data))
|
||||||
|
(reduce #(assoc %1 (:id %2) (uuid/next)) index (:fmeds data)))
|
||||||
|
|
||||||
[map-ids file] (remap-ids map-ids (:file data) #{:id})
|
flibs (map #(remap-id % index :file-id) (:flibs data))
|
||||||
[map-ids file-library-rel] (remap-ids map-ids (:file-library-rel data) #{:file-id :library-file-id})
|
|
||||||
[_ file-media-object] (remap-ids map-ids (:file-media-object data) #{:id :file-id :media-id :thumbnail-id})
|
|
||||||
|
|
||||||
file (map #(assoc % :project-id (:id project)) file)
|
files (->> (:files data)
|
||||||
file-profile-rel (map #(array-map :file-id (:id %)
|
(map #(assoc % :id (get index (:id %))))
|
||||||
:profile-id (:id profile)
|
(map #(assoc % :project-id (:id project)))
|
||||||
:is-owner true
|
(map #(process-file % index)))
|
||||||
:is-admin true
|
|
||||||
:can-edit true)
|
fmeds (->> (:fmeds data)
|
||||||
file)]
|
(map #(assoc % :id (get index (:id %))))
|
||||||
|
(map #(remap-id % index :file-id)))
|
||||||
|
|
||||||
|
fprofs (map #(array-map :file-id (:id %)
|
||||||
|
:profile-id (:id profile)
|
||||||
|
:is-owner true
|
||||||
|
:is-admin true
|
||||||
|
:can-edit true) files)]
|
||||||
|
|
||||||
(projects/create-project-profile conn {:project-id (:id project)
|
(projects/create-project-profile conn {:project-id (:id project)
|
||||||
:profile-id (:id profile)})
|
:profile-id (:id profile)})
|
||||||
|
@ -119,19 +159,24 @@
|
||||||
:profile-id (:id profile)})
|
:profile-id (:id profile)})
|
||||||
|
|
||||||
;; Re-insert into the database
|
;; Re-insert into the database
|
||||||
(doseq [params file]
|
(doseq [params files]
|
||||||
(db/insert! conn :file params))
|
(db/insert! conn :file params))
|
||||||
(doseq [params file-profile-rel]
|
|
||||||
|
(doseq [params fprofs]
|
||||||
(db/insert! conn :file-profile-rel params))
|
(db/insert! conn :file-profile-rel params))
|
||||||
(doseq [params file-library-rel]
|
|
||||||
|
(doseq [params flibs]
|
||||||
(db/insert! conn :file-library-rel params))
|
(db/insert! conn :file-library-rel params))
|
||||||
(doseq [params file-media-object]
|
|
||||||
|
(doseq [params fmeds]
|
||||||
(db/insert! conn :file-media-object params)))))))
|
(db/insert! conn :file-media-object params)))))))
|
||||||
|
|
||||||
(defn load
|
(defn load
|
||||||
[system {:keys [email] :as opts}]
|
[system {:keys [email] :as opts}]
|
||||||
(db/with-atomic [conn (:app.db/pool system)]
|
(db/with-atomic [conn (:app.db/pool system)]
|
||||||
(when-let [profile (profile/retrieve-profile-data-by-email conn email)]
|
(when-let [profile (some->> email
|
||||||
|
(profile/retrieve-profile-data-by-email conn)
|
||||||
|
(profile/populate-additional-data conn))]
|
||||||
(load-initial-project! conn profile opts)
|
(load-initial-project! conn profile opts)
|
||||||
true)))
|
true)))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue