Migrate from embedded covers to assets

This commit is contained in:
Claudio Ortolina
2025-09-01 16:10:50 +03:00
parent cbf567a502
commit dc77d337c0
19 changed files with 80 additions and 144 deletions
+6 -13
View File
@@ -5,8 +5,9 @@ defmodule MusicLibrary.RecordsTest do
import MusicBrainz.Fixtures.ReleaseGroup
import MusicLibrary.Fixtures.Records
alias MusicLibrary.Assets
alias MusicLibrary.Records
alias MusicLibrary.Records.SearchIndex
alias MusicLibrary.Records.{Cover, SearchIndex}
defp create_records(_) do
records = [
@@ -208,16 +209,6 @@ defmodule MusicLibrary.RecordsTest do
end
end
describe "get_cover/1" do
test "it returns the record cover by id" do
# while this test may seem redundant, it implicitely checks that ALL record fields are returned,
# as opposed to other code paths where we only return essential ones.
expected = record()
assert Map.take(expected, [:cover_hash, :cover_data]) == Records.get_cover(expected.id)
end
end
describe "import_from_musicbrainz_release_group/2" do
test "it saves a record with its cover art" do
current_time = DateTime.utc_now()
@@ -342,10 +333,12 @@ defmodule MusicLibrary.RecordsTest do
assert {:ok, updated_record} = Records.refresh_cover(record)
assert updated_record.cover_data !== record.cover_data
assert updated_record.cover_hash ==
"6E0D25D1FD1019D771D7EB3F777E2C7C1B06A73A92E56A584D674D86DD8AF441"
{:ok, expected_content} = Cover.resize(raven_cover_data())
assert Assets.get(updated_record.cover_hash).content == expected_content
end
end
end
@@ -31,10 +31,8 @@ defmodule MusicLibraryWeb.CollectionControllerTest do
"id" => record.id,
"artists" => ["Steven Wilson"],
"title" => record.title,
"cover_url" =>
"http://localhost:4002/api/covers/#{record.id}?vsn=#{record.cover_hash}",
"thumb_url" =>
"http://localhost:4002/api/covers/#{record.id}?vsn=#{record.cover_hash}&size=480"
"cover_url" => "http://localhost:4002/api/covers/#{record.cover_hash}",
"thumb_url" => "http://localhost:4002/api/covers/#{record.cover_hash}?size=480"
}
end
end
@@ -59,10 +57,8 @@ defmodule MusicLibraryWeb.CollectionControllerTest do
"id" => record.id,
"artists" => ["Steven Wilson"],
"title" => record.title,
"cover_url" =>
"http://localhost:4002/api/covers/#{record.id}?vsn=#{record.cover_hash}",
"thumb_url" =>
"http://localhost:4002/api/covers/#{record.id}?vsn=#{record.cover_hash}&size=480"
"cover_url" => "http://localhost:4002/api/covers/#{record.cover_hash}",
"thumb_url" => "http://localhost:4002/api/covers/#{record.cover_hash}?size=480"
}
end
end
@@ -91,10 +87,8 @@ defmodule MusicLibraryWeb.CollectionControllerTest do
"id" => record.id,
"artists" => ["Steven Wilson"],
"title" => record.title,
"cover_url" =>
"http://localhost:4002/api/covers/#{record.id}?vsn=#{record.cover_hash}",
"thumb_url" =>
"http://localhost:4002/api/covers/#{record.id}?vsn=#{record.cover_hash}&size=480"
"cover_url" => "http://localhost:4002/api/covers/#{record.cover_hash}",
"thumb_url" => "http://localhost:4002/api/covers/#{record.cover_hash}?size=480"
}
]
}
@@ -4,7 +4,6 @@ defmodule MusicLibraryWeb.CoverControllerTest do
import MusicLibrary.Fixtures.Records
alias MusicLibrary.Assets
alias MusicLibrary.Records.Cover
defp create_asset(_config) do
{:ok, asset} = Assets.store(%{content: marbles_cover_data(), format: "image/jpeg"})
@@ -65,7 +64,7 @@ defmodule MusicLibraryWeb.CoverControllerTest do
conn = get(conn, ~p"/covers/#{asset.hash}?size=480")
thumb = marbles_thumb_data()
hash = Cover.hash(thumb)
hash = Assets.Asset.hash(thumb)
assert conn.status == 200
assert get_resp_header(conn, "content-type") == ["image/jpeg; charset=utf-8"]
@@ -7,6 +7,7 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do
import MusicLibraryWeb.RecordComponents, only: [format_label: 1, type_label: 1]
alias MusicBrainz.ReleaseGroupSearchResult
alias MusicLibrary.Assets
alias MusicLibrary.Records.{Cover, Record}
# make it a multiple of 4 for easier calculations
@@ -50,7 +51,7 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do
session = visit(conn, ~p"/collection?order=alphabetical&page_size=#{page_size}")
for record <- expected_present do
cover_url = ~p"/covers/#{record.id}?vsn=#{record.cover_hash}"
cover_url = ~p"/covers/#{record.cover_hash}"
session
|> assert_has("#records-#{record.id}")
@@ -139,7 +140,7 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do
session =
visit(conn, ~p"/collection?#{qs}")
cover_url = ~p"/covers/#{record.id}?vsn=#{record.cover_hash}"
cover_url = ~p"/covers/#{record.cover_hash}"
session
|> assert_has("#records-#{record.id}")
@@ -181,7 +182,7 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do
visit(conn, ~p"/collection?#{qs}")
for record <- present do
cover_url = ~p"/covers/#{record.id}?vsn=#{record.cover_hash}"
cover_url = ~p"/covers/#{record.cover_hash}"
session
|> assert_has("#records-#{record.id}")
@@ -217,8 +218,8 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do
end
test "can change the record cover", %{conn: conn} do
record = record(cover_data: marbles_cover_data())
cover_url = ~p"/covers/#{record.id}?vsn=#{record.cover_hash}"
record = record()
cover_url = ~p"/covers/#{record.cover_hash}"
session =
conn
@@ -231,10 +232,10 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do
|> click_button("Save")
|> assert_has("p", text: "Record updated successfully")
updated_cover = MusicLibrary.Records.get_cover(record.id)
updated_cover_url = ~p"/covers/#{record.id}?vsn=#{updated_cover.cover_hash}"
updated_record = MusicLibrary.Records.get_record!(record.id)
updated_cover_url = ~p"/covers/#{updated_record.cover_hash}"
assert updated_cover.cover_hash !== record.cover_hash
assert updated_record.cover_hash !== record.cover_hash
assert_has(session, "img[src='#{updated_cover_url}']")
end
end
@@ -317,7 +318,9 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do
{:ok, resized_cover_data} = Cover.resize(cover_data)
assert record.cover_data == resized_cover_data
assets = Assets.get(record.cover_hash)
assert assets.content == resized_cover_data
assert record.inserted_at !== nil
assert record.updated_at !== nil
@@ -420,9 +423,10 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do
assert record.cover_hash ==
"E7238C742E5B8711FC5BFF01A4A1F727D9E404A4D1420429A6B37ABFFC0B5960"
asset = Assets.get(record.cover_hash)
{:ok, resized_cover_data} = Cover.resize(cover_data)
assert record.cover_data == resized_cover_data
assert asset.content == resized_cover_data
assert record.inserted_at !== nil
assert record.updated_at !== nil
@@ -29,7 +29,7 @@ defmodule MusicLibraryWeb.CollectionLive.ShowTest do
describe "Show record" do
test "it includes all needed information", %{conn: conn} do
record = record()
cover_url = ~p"/covers/#{record.id}?vsn=#{record.cover_hash}"
cover_url = ~p"/covers/#{record.cover_hash}"
release_response = Fixtures.Release.release(:marbles)
@@ -21,7 +21,7 @@ defmodule MusicLibraryWeb.WishlistLive.ShowTest do
describe "Show record" do
test "it includes all needed information", %{conn: conn} do
record = record(purchased_at: nil)
cover_url = ~p"/covers/#{record.id}?vsn=#{record.cover_hash}"
cover_url = ~p"/covers/#{record.cover_hash}"
session =
conn
+17 -1
View File
@@ -5,6 +5,8 @@ defmodule MusicLibrary.Fixtures.Records do
"""
alias MusicBrainz.Fixtures.ReleaseGroup
alias MusicLibrary.Assets
alias MusicLibrary.Assets.Asset
alias MusicLibrary.Records.Record
@genres [
@@ -51,6 +53,10 @@ defmodule MusicLibrary.Fixtures.Records do
@marbles_thumb_data File.read!(@marbles_thumb_data_path)
@raven_cover_data File.read!(@raven_cover_data_path)
@marbles_cover_hash Asset.hash(@marbles_cover_data)
@marbles_thumb_hash Asset.hash(@marbles_thumb_data)
@raven_cover_hash Asset.hash(@raven_cover_data)
def marbles_cover_fixture, do: @marbles_cover_data_path
def marbles_thumb_fixture, do: @marbles_thumb_data_path
def raven_cover_fixture, do: @raven_cover_data_path
@@ -59,17 +65,27 @@ defmodule MusicLibrary.Fixtures.Records do
def marbles_thumb_data, do: @marbles_thumb_data
def raven_cover_data, do: @raven_cover_data
def marbles_cover_hash, do: @marbles_cover_hash
def marbles_thumb_hash, do: @marbles_thumb_hash
def raven_cover_hash, do: @raven_cover_hash
def record(attrs \\ %{}) do
record_musicbrainz_id = Ecto.UUID.generate()
artist_name = Enum.random(@artists)
current_time = DateTime.utc_now()
{:ok, asset} =
Assets.store(%{
content: @marbles_cover_data,
format: "image/jpeg"
})
{:ok, record} =
attrs
|> Enum.into(%{
genres: Enum.take_random(@genres, :rand.uniform(3)),
cover_url: "https://coverartarchive.org/release-group/#{record_musicbrainz_id}/front",
cover_data: @marbles_cover_data,
cover_hash: asset.hash,
musicbrainz_id: record_musicbrainz_id,
musicbrainz_data: ReleaseGroup.release_group(:marbles),
title: Enum.random(@titles),