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:
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
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
|
||||
|
||||
|
||||
+11
-11
@@ -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 ""
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user