From 72116c55c8df260b8a04736474a95653a58bf5c3 Mon Sep 17 00:00:00 2001 From: Geng Tan Date: Tue, 21 May 2024 00:11:19 -0400 Subject: [PATCH] Update scene.ex --- lib/ret/scene.ex | 66 +++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/lib/ret/scene.ex b/lib/ret/scene.ex index 134359490..5927034a3 100644 --- a/lib/ret/scene.ex +++ b/lib/ret/scene.ex @@ -220,39 +220,43 @@ defmodule Ret.Scene do Repo.transaction(fn -> Enum.each(scene_stream, fn scene -> - %Scene{ - scene_owned_file: old_scene_owned_file, - model_owned_file: old_model_owned_file, - account: account - } = scene - - new_scene_owned_file = - Storage.create_new_owned_file_with_replaced_string( - old_scene_owned_file, - account, - old_domain_url, - new_domain_url - ) - - {:ok, new_model_owned_file} = - Storage.duplicate_and_transform(old_model_owned_file, account, fn glb_stream, - _total_bytes -> - GLTFUtils.replace_in_glb(glb_stream, old_domain_url, new_domain_url) - end) - - scene - |> change() - |> put_change(:scene_owned_file_id, new_scene_owned_file.owned_file_id) - |> put_change(:model_owned_file_id, new_model_owned_file.owned_file_id) - |> Repo.update!() - - for old_owned_file <- [old_scene_owned_file, old_model_owned_file] do - OwnedFile.set_inactive(old_owned_file) - Storage.rm_files_for_owned_file(old_owned_file) - Repo.delete(old_owned_file) + try do + %Scene{ + scene_owned_file: old_scene_owned_file, + model_owned_file: old_model_owned_file, + account: account + } = scene + + new_scene_owned_file = + Storage.create_new_owned_file_with_replaced_string( + old_scene_owned_file, + account, + old_domain_url, + new_domain_url + ) + + {:ok, new_model_owned_file} = + Storage.duplicate_and_transform(old_model_owned_file, account, fn glb_stream, + _total_bytes -> + GLTFUtils.replace_in_glb(glb_stream, old_domain_url, new_domain_url) + end) + + scene + |> change() + |> put_change(:scene_owned_file_id, new_scene_owned_file.owned_file_id) + |> put_change(:model_owned_file_id, new_model_owned_file.owned_file_id) + |> Repo.update!() + + for old_owned_file <- [old_scene_owned_file, old_model_owned_file] do + OwnedFile.set_inactive(old_owned_file) + Storage.rm_files_for_owned_file(old_owned_file) + Repo.delete(old_owned_file) + end + rescue + e -> + IO.warn("Failed to process scene due to an error: #{inspect(e)}") end end) - :ok end) end