Serve covers from assets

This commit is contained in:
Claudio Ortolina
2025-09-01 14:44:17 +03:00
parent 49c3a5cba2
commit bd12454884
4 changed files with 32 additions and 35 deletions
@@ -6,56 +6,52 @@ defmodule MusicLibraryWeb.CoverControllerTest do
alias MusicLibrary.Assets
alias MusicLibrary.Records.Cover
defp create_record(_) do
%{record: record()}
end
defp create_asset(%{record: record}) do
{:ok, asset} = Assets.store(%{content: record.cover_data, format: "image/jpeg"})
defp create_asset(_config) do
{:ok, asset} = Assets.store(%{content: marbles_cover_data(), format: "image/jpeg"})
%{asset: asset}
end
describe "GET /covers/:record_id" do
setup [:create_record, :create_asset]
describe "GET /covers/:hash" do
setup [:create_asset]
test "404s when record doesn't exist", %{conn: conn} do
test "404s when asset doesn't exist", %{conn: conn} do
id = Ecto.UUID.generate()
conn = get(conn, ~p"/covers/#{id}")
assert text_response(conn, 404) == "Not found"
end
test "serves the cover without etag", %{conn: conn, record: record} do
conn = get(conn, ~p"/covers/#{record.id}")
test "serves the cover without etag", %{conn: conn, asset: asset} do
conn = get(conn, ~p"/covers/#{asset.hash}")
assert conn.status == 200
assert get_resp_header(conn, "content-type") == ["image/jpeg; charset=utf-8"]
assert get_resp_header(conn, "cache-control") == ["public, max-age=31536000"]
assert get_resp_header(conn, "etag") == [record.cover_hash]
assert get_resp_header(conn, "etag") == [asset.hash]
assert conn.resp_body == record.cover_data
assert conn.resp_body == asset.content
end
test "serves the cover when etag doesn't match", %{conn: conn, record: record} do
test "serves the cover when etag doesn't match", %{conn: conn, asset: asset} do
conn =
conn
|> put_req_header("if-none-match", "invalid-etag")
|> get(~p"/covers/#{record.id}")
|> get(~p"/covers/#{asset.hash}")
assert conn.status == 200
assert get_resp_header(conn, "content-type") == ["image/jpeg; charset=utf-8"]
assert get_resp_header(conn, "cache-control") == ["public, max-age=31536000"]
assert get_resp_header(conn, "etag") == [record.cover_hash]
assert get_resp_header(conn, "etag") == [asset.hash]
assert conn.resp_body == record.cover_data
assert conn.resp_body == asset.content
end
test "serves a 304 when etag matches", %{conn: conn, record: record} do
test "serves a 304 when etag matches", %{conn: conn, asset: asset} do
conn =
conn
|> put_req_header("if-none-match", record.cover_hash)
|> get(~p"/covers/#{record.id}")
|> put_req_header("if-none-match", asset.hash)
|> get(~p"/covers/#{asset.hash}")
assert conn.status == 304
assert get_resp_header(conn, "content-type") == []
@@ -65,8 +61,8 @@ defmodule MusicLibraryWeb.CoverControllerTest do
assert conn.resp_body == <<>>
end
test "accepts a size attribute for resizing", %{conn: conn, record: record} do
conn = get(conn, ~p"/covers/#{record.id}?size=480")
test "accepts a size attribute for resizing", %{conn: conn, asset: asset} do
conn = get(conn, ~p"/covers/#{asset.hash}?size=480")
thumb = marbles_thumb_data()
hash = Cover.hash(thumb)