diff --git a/backend/src/app/srepl/fixes/media_refs.clj b/backend/src/app/srepl/fixes/media_refs.clj new file mode 100644 index 0000000000..497dde3583 --- /dev/null +++ b/backend/src/app/srepl/fixes/media_refs.clj @@ -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))) diff --git a/backend/src/app/srepl/helpers.clj b/backend/src/app/srepl/helpers.clj index 0e0ae6f20d..126a3db518 100644 --- a/backend/src/app/srepl/helpers.clj +++ b/backend/src/app/srepl/helpers.clj @@ -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)))) diff --git a/backend/src/app/tasks/file_gc.clj b/backend/src/app/tasks/file_gc.clj index d62031b8bb..2f1edd1d78 100644 --- a/backend/src/app/tasks/file_gc.clj +++ b/backend/src/app/tasks/file_gc.clj @@ -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)]