Rename image* to cover*

This commit is contained in:
Claudio Ortolina
2024-10-02 16:33:42 +01:00
parent fb978406cd
commit 1e0d5486e0
13 changed files with 86 additions and 71 deletions
+8 -8
View File
@@ -4,7 +4,7 @@ defmodule MusicLibrary.Records do
alias MusicLibrary.Records.{MusicBrainz, Record}
@fields [:id, :type, :format, :title, :release, :genres, :musicbrainz_id, :image_data_hash]
@fields [:id, :type, :format, :title, :release, :genres, :musicbrainz_id, :cover_hash]
def list_records(opts \\ []) do
limit = Keyword.get(opts, :limit, 20)
@@ -59,11 +59,11 @@ defmodule MusicLibrary.Records do
Repo.one!(q)
end
def get_image!(id) do
def get_cover!(id) do
q =
from r in Record,
where: r.id == ^id,
select: %{image_data: r.image_data, image_data_hash: r.image_data_hash}
select: %{cover_data: r.cover_data, cover_hash: r.cover_hash}
Repo.one!(q)
end
@@ -77,15 +77,15 @@ defmodule MusicLibrary.Records do
def import_from_musicbrainz(musicbrainz_id, opts \\ []) do
with format = Keyword.get(opts, :format, "cd"),
{:ok, release_group} <- MusicBrainz.get_release_group(musicbrainz_id),
{:ok, image_data} <- MusicBrainz.get_cover_art(musicbrainz_id),
record_params = build_record_params(release_group, image_data, format) do
{:ok, cover_data} <- MusicBrainz.get_cover_art(musicbrainz_id),
record_params = build_record_params(release_group, cover_data, format) do
create_record(record_params)
else
error -> error
end
end
defp build_record_params(release_group, image_data, format) do
defp build_record_params(release_group, cover_data, format) do
musicbrainz_id = release_group["id"]
artists_attrs =
@@ -108,8 +108,8 @@ defmodule MusicLibrary.Records do
"type" => parse_subtype(release_group["primary-type"]),
"format" => format,
"genres" => Enum.map(release_group["genres"], fn g -> g["name"] end),
"image_url" => "https://coverartarchive.org/release-group/#{musicbrainz_id}/front",
"image_data" => image_data
"cover_url" => "https://coverartarchive.org/release-group/#{musicbrainz_id}/front",
"cover_data" => cover_data
}
end
+22 -22
View File
@@ -81,50 +81,50 @@ defmodule MusicLibrary.Records.Importer do
end
@doc """
Pull the cover image from the stored url and keep a local, resized copy in
Pull the cover from the stored url and keep a local, resized copy in
the database for fast access/use.
"""
def import_cover_image(record) do
with {:ok, image_data} <- blob_get(record.image_url) do
{:ok, thumb} = Vix.Vips.Operation.thumbnail_buffer(image_data, 400)
def import_cover(record) do
with {:ok, cover_data} <- blob_get(record.cover_url) do
{:ok, thumb} = Vix.Vips.Operation.thumbnail_buffer(cover_data, 400)
{:ok, thumb_data} = Vix.Vips.Image.write_to_buffer(thumb, ".jpg")
record
|> Rec.add_image_data(thumb_data)
|> Rec.add_cover_data(thumb_data)
|> Repo.update!()
end
end
@doc """
Given an already stored image in the database, resize it to a 400px wide thumbnail.
Given an already stored cover, resize it to a 400px wide thumbnail.
"""
def resize_cover_image(record) do
{:ok, thumb} = Vix.Vips.Operation.thumbnail_buffer(record.image_data, 400)
def resize_cover(record) do
{:ok, thumb} = Vix.Vips.Operation.thumbnail_buffer(record.cover_data, 400)
{:ok, thumb_data} = Vix.Vips.Image.write_to_buffer(thumb, ".jpg")
record
|> Rec.add_image_data(thumb_data)
|> Rec.add_cover_data(thumb_data)
|> Repo.update!()
end
def import_all_cover_images do
def import_all_covers do
Rec
|> Repo.all()
|> Enum.each(fn r ->
if r.image_data == nil do
import_cover_image(r)
IO.puts("Imported cover image for #{r.title}")
if r.cover_data == nil do
import_cover(r)
IO.puts("Imported cover for #{r.title}")
end
end)
end
def resize_all_cover_images do
def resize_all_covers do
Rec
|> Repo.all()
|> Enum.each(fn r ->
if r.image_data != nil do
resize_cover_image(r)
IO.puts("Resized cover image for #{r.title}")
if r.cover_data != nil do
resize_cover(r)
IO.puts("Resized cover for #{r.title}")
end
end)
end
@@ -133,16 +133,16 @@ defmodule MusicLibrary.Records.Importer do
Rec
|> Repo.all()
|> Enum.each(fn r ->
if r.image_data != nil do
generate_cover_image_hash(r)
IO.puts("Generated cover image hash for #{r.title}")
if r.cover_data != nil do
generate_cover_hash(r)
IO.puts("Generated cover hash for #{r.title}")
end
end)
end
def generate_cover_image_hash(record) do
def generate_cover_hash(record) do
record
|> Rec.generate_image_data_hash()
|> Rec.generate_cover_hash()
|> Repo.update!()
end
+2 -2
View File
@@ -182,8 +182,8 @@ defmodule MusicLibrary.Records.MusicBrainz do
def get_cover_art(musicbrainz_id) do
url = "https://coverartarchive.org/release-group/#{musicbrainz_id}/front"
with {:ok, image_data} <- blob_get(url),
{:ok, thumb} = Vix.Vips.Operation.thumbnail_buffer(image_data, 400) do
with {:ok, cover_data} <- blob_get(url),
{:ok, thumb} = Vix.Vips.Operation.thumbnail_buffer(cover_data, 400) do
Vix.Vips.Image.write_to_buffer(thumb, ".jpg")
end
end
+17 -17
View File
@@ -10,9 +10,9 @@ defmodule MusicLibrary.Records.Record do
field :type, Ecto.Enum, values: [:album, :ep, :live, :compilation, :single, :other]
field :format, Ecto.Enum, values: @formats
field :title, :string
field :image_url, :string
field :image_data, :binary
field :image_data_hash, :string
field :cover_url, :string
field :cover_data, :binary
field :cover_hash, :string
field :musicbrainz_id, Ecto.UUID
field :genres, {:array, :string}
field :release, :string
@@ -37,11 +37,11 @@ defmodule MusicLibrary.Records.Record do
:musicbrainz_id,
:release,
:genres,
:image_url,
:image_data
:cover_url,
:cover_data
])
|> cast_embed(:artists, with: &artist_changeset/2)
|> generate_image_data_hash()
|> generate_cover_hash()
|> validate_required([:type, :title, :musicbrainz_id, :release, :genres])
end
@@ -58,28 +58,28 @@ defmodule MusicLibrary.Records.Record do
|> put_embed(:artists, artists_attrs)
end
def add_image_data(record, image_data) do
def add_cover_data(record, cover_data) do
record
|> change(image_data: image_data)
|> generate_image_data_hash()
|> change(cover_data: cover_data)
|> generate_cover_hash()
end
def generate_image_data_hash(record = %__MODULE__{image_data: image_data}) do
hash = :crypto.hash(:sha256, image_data) |> Base.encode16()
def generate_cover_hash(record = %__MODULE__{cover_data: cover_data}) do
hash = :crypto.hash(:sha256, cover_data) |> Base.encode16()
record
|> change()
|> put_change(:image_data_hash, hash)
|> put_change(:cover_hash, hash)
end
def generate_image_data_hash(changeset) do
case get_change(changeset, :image_data) do
def generate_cover_hash(changeset) do
case get_change(changeset, :cover_data) do
nil ->
changeset
image_data ->
hash = :crypto.hash(:sha256, image_data) |> Base.encode16()
put_change(changeset, :image_data_hash, hash)
cover_data ->
hash = :crypto.hash(:sha256, cover_data) |> Base.encode16()
put_change(changeset, :cover_hash, hash)
end
end