Skip to content

Commit

Permalink
word
Browse files Browse the repository at this point in the history
  • Loading branch information
tanfarming committed May 20, 2024
1 parent 10b621f commit 8e068c8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 45 deletions.
68 changes: 36 additions & 32 deletions lib/ret/scene.ex
Original file line number Diff line number Diff line change
Expand Up @@ -220,38 +220,42 @@ 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)
end
end)
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)

:ok
end)
Expand Down
20 changes: 7 additions & 13 deletions lib/ret/storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,13 @@ defmodule Ret.Storage do
search_string,
replacement_string
) do
case fetch(owned_file) do
{:ok, %{"content_type" => content_type}, file_stream} ->
file_stream
|> Enum.to_list()
|> Enum.join("")
|> String.replace(search_string, replacement_string)
|> store_string_as_owned_file(content_type, account)

{:error, reason} ->
# Print a warning and ignore the error
IO.warn("Failed to fetch the file: #{inspect(reason)}")
:ignore
end
{:ok, %{"content_type" => content_type}, file_stream} = fetch(owned_file)

file_stream
|> Enum.to_list()
|> Enum.join("")
|> String.replace(search_string, replacement_string)
|> store_string_as_owned_file(content_type, account)
end

def fetch(id, key) when is_binary(id) and is_binary(key) do
Expand Down

0 comments on commit 8e068c8

Please sign in to comment.