0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-22 23:06:08 -05:00

🎉 Add srepl fix function for process media refs on a file

This commit is contained in:
Andrey Antukh 2025-01-23 19:11:18 +01:00
parent 4c1312d4f6
commit 16934b734b
3 changed files with 53 additions and 7 deletions

View 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)))

View file

@ -8,7 +8,7 @@
"A main namespace for server repl."
(:refer-clojure :exclude [parse-uuid])
(:require
[app.binfile.common :refer [update-file! decode-file]]
[app.binfile.common :as bfc]
[app.common.data :as d]
[app.common.files.validate :as cfv]
[app.db :as db]
@ -24,6 +24,10 @@
(locking println
(apply println params)))
(defn get-current-system
[]
*system*)
(defn parse-uuid
[v]
(if (string? v)
@ -37,8 +41,8 @@
([system id]
(db/run! system
(fn [system]
(->> (files/get-file system id :migrate? false)
(decode-file system))))))
(->> (bfc/get-file system id ::db/for-update true)
(bfc/decode-file system))))))
(defn get-raw-file
"Get the migrated data of one file."
@ -124,11 +128,11 @@
(defn process-file!
[system file-id update-fn & {:keys [label validate? with-libraries?] :or {validate? true} :as opts}]
(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?
(->> (files/get-file-libraries conn file-id)
(into [file] (map (fn [{:keys [id]}]
(get-file system id))))
(bfc/get-file system id))))
(d/index-by :id)))
file' (if with-libraries?
@ -144,5 +148,5 @@
(fsnap/create-file-snapshot! system nil file-id label))
(let [file' (update file' :revn inc)]
(update-file! system file')
(bfc/update-file! system file')
true))))

View file

@ -253,7 +253,6 @@
(->> (db/exec! conn [sql:get-file min-age file-id])
(first))))
(defn- process-file!
[cfg file-id]
(if-let [file (get-file cfg file-id)]