Consolidate cover resize logic

Moves resizing out of the MusicBrainz namespace to Records - which
improves division of responsibilities, and removes double resizing
during import.
This commit is contained in:
Claudio Ortolina
2024-11-26 08:35:16 +00:00
parent 5cb2214922
commit f0212eeb6f
6 changed files with 32 additions and 26 deletions
+4 -4
View File
@@ -418,14 +418,14 @@ defmodule MusicBrainz.APIImpl do
end end
def get_cover_art({:url, url}) do def get_cover_art({:url, url}) do
with {:ok, cover_data} <- blob_get(url), case blob_get(url) do
{:ok, thumb} = Vix.Vips.Operation.thumbnail_buffer(cover_data, 600) do
Vix.Vips.Image.write_to_buffer(thumb, ".jpg")
else
{:error, reason} -> {:error, reason} ->
Logger.error("Failed to fetch cover art for #{url}, reason: #{inspect(reason)}") Logger.error("Failed to fetch cover art for #{url}, reason: #{inspect(reason)}")
{:error, :cover_not_available} {:error, :cover_not_available}
success ->
success
end end
end end
+4 -6
View File
@@ -6,7 +6,7 @@ defmodule MusicLibrary.Records do
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias MusicLibrary.Repo alias MusicLibrary.Repo
alias MusicLibrary.Records.{Record, SearchParser} alias MusicLibrary.Records.{Cover, Record, SearchParser}
def essential_fields do def essential_fields do
[ [
@@ -131,14 +131,13 @@ defmodule MusicLibrary.Records do
defp get_cover_art_or_default(musicbrainz_id) do defp get_cover_art_or_default(musicbrainz_id) do
case musicbrainz().get_cover_art({:musicbrainz_id, musicbrainz_id}) do case musicbrainz().get_cover_art({:musicbrainz_id, musicbrainz_id}) do
{:error, :cover_not_available} -> {:ok, Record.fallback_cover_data()} {:error, :cover_not_available} -> {:ok, Record.fallback_cover_data()}
success -> success {:ok, cover_data} -> Cover.resize(cover_data)
end end
end end
def refresh_cover(record) do def refresh_cover(record) do
with {:ok, cover_data} <- musicbrainz().get_cover_art({:url, record.cover_url}) 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} = Cover.resize(cover_data)
{:ok, thumb_data} = Vix.Vips.Image.write_to_buffer(thumb, ".jpg")
record record
|> Record.add_cover_data(thumb_data) |> Record.add_cover_data(thumb_data)
@@ -147,8 +146,7 @@ defmodule MusicLibrary.Records do
end end
def resize_cover(record) do def resize_cover(record) do
{:ok, thumb} = Vix.Vips.Operation.thumbnail_buffer(record.cover_data, 600) {:ok, thumb_data} = Cover.resize(record.cover_data)
{:ok, thumb_data} = Vix.Vips.Image.write_to_buffer(thumb, ".jpg")
record record
|> Record.add_cover_data(thumb_data) |> Record.add_cover_data(thumb_data)
+6
View File
@@ -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
@@ -2,6 +2,7 @@ defmodule MusicLibraryWeb.RecordLive.FormComponent do
use MusicLibraryWeb, :live_component use MusicLibraryWeb, :live_component
alias MusicLibrary.Records alias MusicLibrary.Records
alias MusicLibrary.Records.Cover
@impl true @impl true
def mount(socket) do def mount(socket) do
@@ -109,8 +110,7 @@ defmodule MusicLibraryWeb.RecordLive.FormComponent do
record_params record_params
[cover_data] -> [cover_data] ->
{:ok, thumb} = Vix.Vips.Operation.thumbnail_buffer(cover_data, 600) {:ok, thumb_data} = Cover.resize(cover_data)
{:ok, thumb_data} = Vix.Vips.Image.write_to_buffer(thumb, ".jpg")
Map.put(record_params, "cover_data", thumb_data) Map.put(record_params, "cover_data", thumb_data)
end end
+11 -11
View File
@@ -44,8 +44,8 @@ msgstr ""
msgid "Basics" msgid "Basics"
msgstr "" msgstr ""
#: lib/music_library_web/live/record_live/form_component.ex:35 #: lib/music_library_web/live/record_live/form_component.ex:36
#: lib/music_library_web/live/record_live/form_component.ex:42 #: lib/music_library_web/live/record_live/form_component.ex:43
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Choose a value" msgid "Choose a value"
msgstr "" msgstr ""
@@ -56,7 +56,7 @@ msgstr ""
msgid "Collection" msgid "Collection"
msgstr "" 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 #, elixir-autogen, elixir-format
msgid "Cover art" msgid "Cover art"
msgstr "" msgstr ""
@@ -95,7 +95,7 @@ msgstr ""
msgid "Error!" msgid "Error!"
msgstr "" 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 #, elixir-autogen, elixir-format
msgid "Format" msgid "Format"
msgstr "" msgstr ""
@@ -155,7 +155,7 @@ msgid "Logout"
msgstr "" msgstr ""
#: lib/music_library_web/live/collection_live/show.html.heex:85 #: 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 #: lib/music_library_web/live/wishlist_live/show.html.heex:86
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "MusicBrainz ID" msgid "MusicBrainz ID"
@@ -167,7 +167,7 @@ msgstr ""
msgid "Next" msgid "Next"
msgstr "" 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 #, elixir-autogen, elixir-format
msgid "No cover selected" msgid "No cover selected"
msgstr "" msgstr ""
@@ -199,7 +199,7 @@ msgstr ""
msgid "Purchase" msgid "Purchase"
msgstr "" 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 #, elixir-autogen, elixir-format
msgid "Purchased at" msgid "Purchased at"
msgstr "" msgstr ""
@@ -222,17 +222,17 @@ msgstr ""
msgid "Record updated successfully" msgid "Record updated successfully"
msgstr "" 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 #, elixir-autogen, elixir-format
msgid "Release" msgid "Release"
msgstr "" 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 #, elixir-autogen, elixir-format
msgid "Save" msgid "Save"
msgstr "" 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 #, elixir-autogen, elixir-format
msgid "Saving..." msgid "Saving..."
msgstr "" msgstr ""
@@ -301,7 +301,7 @@ msgstr ""
msgid "Total wishlist" msgid "Total wishlist"
msgstr "" 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 #, elixir-autogen, elixir-format
msgid "Type" msgid "Type"
msgstr "" msgstr ""
@@ -5,7 +5,7 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do
import MusicLibrary.RecordsFixtures import MusicLibrary.RecordsFixtures
import MusicLibrary.ReleaseGroupsFixtures import MusicLibrary.ReleaseGroupsFixtures
import Mox import Mox
alias MusicLibrary.Records.Record alias MusicLibrary.Records.{Cover, Record}
alias MusicBrainz.APIBehaviourMock alias MusicBrainz.APIBehaviourMock
setup :verify_on_exit! setup :verify_on_exit!
@@ -330,9 +330,11 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do
] ]
assert record.cover_hash == 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.inserted_at !== nil
assert record.updated_at !== nil assert record.updated_at !== nil