mirror of
https://github.com/penpot/penpot.git
synced 2025-02-23 15:26:29 -05:00
🎉 Add srepl fix function for process media refs on a file
This commit is contained in:
parent
4c1312d4f6
commit
16934b734b
3 changed files with 53 additions and 7 deletions
43
backend/src/app/srepl/fixes/media_refs.clj
Normal file
43
backend/src/app/srepl/fixes/media_refs.clj
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
;;
|
||||||
|
;; Copyright (c) KALEIDOS INC
|
||||||
|
|
||||||
|
(ns app.srepl.fixes.media-refs
|
||||||
|
(:require
|
||||||
|
[app.binfile.common :as bfc]
|
||||||
|
[app.common.files.helpers :as cfh]
|
||||||
|
[app.srepl.helpers :as h]))
|
||||||
|
|
||||||
|
(defn- collect-media-refs
|
||||||
|
"Given a file data map, returns all media references used on pages of
|
||||||
|
the file data; components and other parts of the file data are not analized"
|
||||||
|
[data]
|
||||||
|
(let [xform-collect
|
||||||
|
(comp
|
||||||
|
(map val)
|
||||||
|
(mapcat (fn [object]
|
||||||
|
(->> (cfh/collect-shape-media-refs object)
|
||||||
|
(map (fn [id]
|
||||||
|
{:object-id (:id object)
|
||||||
|
:id id}))))))
|
||||||
|
process-page
|
||||||
|
(fn [result page-id container]
|
||||||
|
(let [xform (comp xform-collect (map #(assoc % :page-id page-id)))]
|
||||||
|
(into result xform (:objects container))))]
|
||||||
|
|
||||||
|
(reduce-kv process-page [] (:pages-index data))))
|
||||||
|
|
||||||
|
(defn update-all-media-references
|
||||||
|
"Check if all file media object references are in plance and create
|
||||||
|
new ones if some of them are missing; this prevents strange bugs on
|
||||||
|
having the same media object associated with two different files;"
|
||||||
|
[cfg file]
|
||||||
|
(let [media-refs (collect-media-refs (:data file))]
|
||||||
|
(bfc/update-media-references! cfg file media-refs)))
|
||||||
|
|
||||||
|
(defn process-file
|
||||||
|
[file _opts]
|
||||||
|
(let [system (h/get-current-system)]
|
||||||
|
(update-all-media-references system file)))
|
|
@ -8,7 +8,7 @@
|
||||||
"A main namespace for server repl."
|
"A main namespace for server repl."
|
||||||
(:refer-clojure :exclude [parse-uuid])
|
(:refer-clojure :exclude [parse-uuid])
|
||||||
(:require
|
(:require
|
||||||
[app.binfile.common :refer [update-file! decode-file]]
|
[app.binfile.common :as bfc]
|
||||||
[app.common.data :as d]
|
[app.common.data :as d]
|
||||||
[app.common.files.validate :as cfv]
|
[app.common.files.validate :as cfv]
|
||||||
[app.db :as db]
|
[app.db :as db]
|
||||||
|
@ -24,6 +24,10 @@
|
||||||
(locking println
|
(locking println
|
||||||
(apply println params)))
|
(apply println params)))
|
||||||
|
|
||||||
|
(defn get-current-system
|
||||||
|
[]
|
||||||
|
*system*)
|
||||||
|
|
||||||
(defn parse-uuid
|
(defn parse-uuid
|
||||||
[v]
|
[v]
|
||||||
(if (string? v)
|
(if (string? v)
|
||||||
|
@ -37,8 +41,8 @@
|
||||||
([system id]
|
([system id]
|
||||||
(db/run! system
|
(db/run! system
|
||||||
(fn [system]
|
(fn [system]
|
||||||
(->> (files/get-file system id :migrate? false)
|
(->> (bfc/get-file system id ::db/for-update true)
|
||||||
(decode-file system))))))
|
(bfc/decode-file system))))))
|
||||||
|
|
||||||
(defn get-raw-file
|
(defn get-raw-file
|
||||||
"Get the migrated data of one file."
|
"Get the migrated data of one file."
|
||||||
|
@ -124,11 +128,11 @@
|
||||||
(defn process-file!
|
(defn process-file!
|
||||||
[system file-id update-fn & {:keys [label validate? with-libraries?] :or {validate? true} :as opts}]
|
[system file-id update-fn & {:keys [label validate? with-libraries?] :or {validate? true} :as opts}]
|
||||||
(let [conn (db/get-connection system)
|
(let [conn (db/get-connection system)
|
||||||
file (get-file system file-id)
|
file (bfc/get-file system file-id ::db/for-update true)
|
||||||
libs (when with-libraries?
|
libs (when with-libraries?
|
||||||
(->> (files/get-file-libraries conn file-id)
|
(->> (files/get-file-libraries conn file-id)
|
||||||
(into [file] (map (fn [{:keys [id]}]
|
(into [file] (map (fn [{:keys [id]}]
|
||||||
(get-file system id))))
|
(bfc/get-file system id))))
|
||||||
(d/index-by :id)))
|
(d/index-by :id)))
|
||||||
|
|
||||||
file' (if with-libraries?
|
file' (if with-libraries?
|
||||||
|
@ -144,5 +148,5 @@
|
||||||
(fsnap/create-file-snapshot! system nil file-id label))
|
(fsnap/create-file-snapshot! system nil file-id label))
|
||||||
|
|
||||||
(let [file' (update file' :revn inc)]
|
(let [file' (update file' :revn inc)]
|
||||||
(update-file! system file')
|
(bfc/update-file! system file')
|
||||||
true))))
|
true))))
|
||||||
|
|
|
@ -253,7 +253,6 @@
|
||||||
(->> (db/exec! conn [sql:get-file min-age file-id])
|
(->> (db/exec! conn [sql:get-file min-age file-id])
|
||||||
(first))))
|
(first))))
|
||||||
|
|
||||||
|
|
||||||
(defn- process-file!
|
(defn- process-file!
|
||||||
[cfg file-id]
|
[cfg file-id]
|
||||||
(if-let [file (get-file cfg file-id)]
|
(if-let [file (get-file cfg file-id)]
|
||||||
|
|
Loading…
Add table
Reference in a new issue