From f0212eeb6fa72fb7215f4047edc5c113b8152d74 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Tue, 26 Nov 2024 08:35:16 +0000 Subject: [PATCH] Consolidate cover resize logic Moves resizing out of the MusicBrainz namespace to Records - which improves division of responsibilities, and removes double resizing during import. --- lib/music_brainz/api_impl.ex | 8 +++---- lib/music_library/records.ex | 10 ++++----- lib/music_library/records/cover.ex | 6 +++++ .../live/record_live/form_component.ex | 4 ++-- priv/gettext/default.pot | 22 +++++++++---------- .../live/collection_live/index_test.exs | 8 ++++--- 6 files changed, 32 insertions(+), 26 deletions(-) create mode 100644 lib/music_library/records/cover.ex diff --git a/lib/music_brainz/api_impl.ex b/lib/music_brainz/api_impl.ex index 9373070e..f91799b7 100644 --- a/lib/music_brainz/api_impl.ex +++ b/lib/music_brainz/api_impl.ex @@ -418,14 +418,14 @@ defmodule MusicBrainz.APIImpl do end def get_cover_art({:url, url}) do - with {:ok, cover_data} <- blob_get(url), - {:ok, thumb} = Vix.Vips.Operation.thumbnail_buffer(cover_data, 600) do - Vix.Vips.Image.write_to_buffer(thumb, ".jpg") - else + case blob_get(url) do {:error, reason} -> Logger.error("Failed to fetch cover art for #{url}, reason: #{inspect(reason)}") {:error, :cover_not_available} + + success -> + success end end diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index ed5c3082..2f6a6b54 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -6,7 +6,7 @@ defmodule MusicLibrary.Records do import Ecto.Query, warn: false alias MusicLibrary.Repo - alias MusicLibrary.Records.{Record, SearchParser} + alias MusicLibrary.Records.{Cover, Record, SearchParser} def essential_fields do [ @@ -131,14 +131,13 @@ defmodule MusicLibrary.Records do defp get_cover_art_or_default(musicbrainz_id) do case musicbrainz().get_cover_art({:musicbrainz_id, musicbrainz_id}) do {:error, :cover_not_available} -> {:ok, Record.fallback_cover_data()} - success -> success + {:ok, cover_data} -> Cover.resize(cover_data) end end def refresh_cover(record) do with {:ok, cover_data} <- musicbrainz().get_cover_art({:url, record.cover_url}) do - {:ok, thumb} = Vix.Vips.Operation.thumbnail_buffer(cover_data, 600) - {:ok, thumb_data} = Vix.Vips.Image.write_to_buffer(thumb, ".jpg") + {:ok, thumb_data} = Cover.resize(cover_data) record |> Record.add_cover_data(thumb_data) @@ -147,8 +146,7 @@ defmodule MusicLibrary.Records do end def resize_cover(record) do - {:ok, thumb} = Vix.Vips.Operation.thumbnail_buffer(record.cover_data, 600) - {:ok, thumb_data} = Vix.Vips.Image.write_to_buffer(thumb, ".jpg") + {:ok, thumb_data} = Cover.resize(record.cover_data) record |> Record.add_cover_data(thumb_data) diff --git a/lib/music_library/records/cover.ex b/lib/music_library/records/cover.ex new file mode 100644 index 00000000..dad9d1c2 --- /dev/null +++ b/lib/music_library/records/cover.ex @@ -0,0 +1,6 @@ +defmodule MusicLibrary.Records.Cover do + def resize(cover_data) do + {:ok, thumb} = Vix.Vips.Operation.thumbnail_buffer(cover_data, 600) + Vix.Vips.Image.write_to_buffer(thumb, ".jpg") + end +end diff --git a/lib/music_library_web/live/record_live/form_component.ex b/lib/music_library_web/live/record_live/form_component.ex index 0b4c406b..d2a365c7 100644 --- a/lib/music_library_web/live/record_live/form_component.ex +++ b/lib/music_library_web/live/record_live/form_component.ex @@ -2,6 +2,7 @@ defmodule MusicLibraryWeb.RecordLive.FormComponent do use MusicLibraryWeb, :live_component alias MusicLibrary.Records + alias MusicLibrary.Records.Cover @impl true def mount(socket) do @@ -109,8 +110,7 @@ defmodule MusicLibraryWeb.RecordLive.FormComponent do record_params [cover_data] -> - {:ok, thumb} = Vix.Vips.Operation.thumbnail_buffer(cover_data, 600) - {:ok, thumb_data} = Vix.Vips.Image.write_to_buffer(thumb, ".jpg") + {:ok, thumb_data} = Cover.resize(cover_data) Map.put(record_params, "cover_data", thumb_data) end diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 62bbb357..974242a3 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -44,8 +44,8 @@ msgstr "" msgid "Basics" msgstr "" -#: lib/music_library_web/live/record_live/form_component.ex:35 -#: lib/music_library_web/live/record_live/form_component.ex:42 +#: lib/music_library_web/live/record_live/form_component.ex:36 +#: lib/music_library_web/live/record_live/form_component.ex:43 #, elixir-autogen, elixir-format msgid "Choose a value" msgstr "" @@ -56,7 +56,7 @@ msgstr "" msgid "Collection" msgstr "" -#: lib/music_library_web/live/record_live/form_component.ex:56 +#: lib/music_library_web/live/record_live/form_component.ex:57 #, elixir-autogen, elixir-format msgid "Cover art" msgstr "" @@ -95,7 +95,7 @@ msgstr "" msgid "Error!" msgstr "" -#: lib/music_library_web/live/record_live/form_component.ex:41 +#: lib/music_library_web/live/record_live/form_component.ex:42 #, elixir-autogen, elixir-format msgid "Format" msgstr "" @@ -155,7 +155,7 @@ msgid "Logout" msgstr "" #: lib/music_library_web/live/collection_live/show.html.heex:85 -#: lib/music_library_web/live/record_live/form_component.ex:46 +#: lib/music_library_web/live/record_live/form_component.ex:47 #: lib/music_library_web/live/wishlist_live/show.html.heex:86 #, elixir-autogen, elixir-format msgid "MusicBrainz ID" @@ -167,7 +167,7 @@ msgstr "" msgid "Next" msgstr "" -#: lib/music_library_web/live/record_live/form_component.ex:62 +#: lib/music_library_web/live/record_live/form_component.ex:63 #, elixir-autogen, elixir-format msgid "No cover selected" msgstr "" @@ -199,7 +199,7 @@ msgstr "" msgid "Purchase" msgstr "" -#: lib/music_library_web/live/record_live/form_component.ex:52 +#: lib/music_library_web/live/record_live/form_component.ex:53 #, elixir-autogen, elixir-format msgid "Purchased at" msgstr "" @@ -222,17 +222,17 @@ msgstr "" msgid "Record updated successfully" msgstr "" -#: lib/music_library_web/live/record_live/form_component.ex:47 +#: lib/music_library_web/live/record_live/form_component.ex:48 #, elixir-autogen, elixir-format msgid "Release" msgstr "" -#: lib/music_library_web/live/record_live/form_component.ex:73 +#: lib/music_library_web/live/record_live/form_component.ex:74 #, elixir-autogen, elixir-format msgid "Save" msgstr "" -#: lib/music_library_web/live/record_live/form_component.ex:73 +#: lib/music_library_web/live/record_live/form_component.ex:74 #, elixir-autogen, elixir-format msgid "Saving..." msgstr "" @@ -301,7 +301,7 @@ msgstr "" msgid "Total wishlist" msgstr "" -#: lib/music_library_web/live/record_live/form_component.ex:34 +#: lib/music_library_web/live/record_live/form_component.ex:35 #, elixir-autogen, elixir-format msgid "Type" msgstr "" diff --git a/test/music_library_web/live/collection_live/index_test.exs b/test/music_library_web/live/collection_live/index_test.exs index ec21919e..45608a62 100644 --- a/test/music_library_web/live/collection_live/index_test.exs +++ b/test/music_library_web/live/collection_live/index_test.exs @@ -5,7 +5,7 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do import MusicLibrary.RecordsFixtures import MusicLibrary.ReleaseGroupsFixtures import Mox - alias MusicLibrary.Records.Record + alias MusicLibrary.Records.{Cover, Record} alias MusicBrainz.APIBehaviourMock setup :verify_on_exit! @@ -330,9 +330,11 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do ] assert record.cover_hash == - "599407DDF69907D4A60FE13CCAA824D25CF08DC124FD6AA3E8E7ECD98C885FFE" + "0ED79C93C5BECC7B28FE05CAA3E49B924A3377EA3219CA8FFAE3B2B0960F2AC8" - assert record.cover_data == cover_data + {:ok, resized_cover_data} = Cover.resize(cover_data) + + assert record.cover_data == resized_cover_data assert record.inserted_at !== nil assert record.updated_at !== nil