diff --git a/lib/music_library/records/importer.ex b/lib/music_library/records/importer.ex index c593837a..6bfa7a6c 100644 --- a/lib/music_library/records/importer.ex +++ b/lib/music_library/records/importer.ex @@ -129,6 +129,23 @@ defmodule MusicLibrary.Records.Importer do end) end + def generate_all_cover_hashes 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}") + end + end) + end + + def generate_cover_image_hash(record) do + record + |> Rec.generate_image_data_hash() + |> Repo.update!() + end + defp blob_get(url) do req = Finch.build(:get, url, [ diff --git a/lib/music_library/records/record.ex b/lib/music_library/records/record.ex index 5534038b..0a6c437d 100644 --- a/lib/music_library/records/record.ex +++ b/lib/music_library/records/record.ex @@ -12,6 +12,7 @@ defmodule MusicLibrary.Records.Record do field :title, :string field :image_url, :string field :image_data, :binary + field :image_data_hash, :string field :year, :integer field :musicbrainz_id, Ecto.UUID field :genres, {:array, :string} @@ -40,6 +41,7 @@ defmodule MusicLibrary.Records.Record do :image_data ]) |> cast_embed(:artists, with: &artist_changeset/2) + |> generate_image_data_hash() |> validate_required([:type, :title, :musicbrainz_id, :year, :genres]) end @@ -57,7 +59,28 @@ defmodule MusicLibrary.Records.Record do end def add_image_data(record, image_data) do - change(record, image_data: image_data) + record + |> change(image_data: image_data) + |> generate_image_data_hash() + end + + def generate_image_data_hash(record = %__MODULE__{image_data: image_data}) do + hash = :crypto.hash(:sha256, image_data) |> Base.encode16() + + record + |> change() + |> put_change(:image_data_hash, hash) + end + + def generate_image_data_hash(changeset) do + case get_change(changeset, :image_data) do + nil -> + changeset + + image_data -> + hash = :crypto.hash(:sha256, image_data) |> Base.encode16() + put_change(changeset, :image_data_hash, hash) + end end def formats, do: @formats diff --git a/lib/music_library_web/live/record_live/index.html.heex b/lib/music_library_web/live/record_live/index.html.heex index 5a0cb07d..39ef687f 100644 --- a/lib/music_library_web/live/record_live/index.html.heex +++ b/lib/music_library_web/live/record_live/index.html.heex @@ -25,7 +25,11 @@ > <:col :let={{_id, record}} label="Image"> - {record.title} + {record.title} <%= Records.Record.format_short_label(record.format) %> diff --git a/lib/music_library_web/live/record_live/show.html.heex b/lib/music_library_web/live/record_live/show.html.heex index 35b51e7a..a123cc8d 100644 --- a/lib/music_library_web/live/record_live/show.html.heex +++ b/lib/music_library_web/live/record_live/show.html.heex @@ -13,7 +13,11 @@
- {@record.title} + {@record.title} <%= Records.Record.format_short_label(@record.format) %> diff --git a/priv/repo/migrations/20240930145536_add_image_data_hash_to_records.exs b/priv/repo/migrations/20240930145536_add_image_data_hash_to_records.exs new file mode 100644 index 00000000..53b0fe4a --- /dev/null +++ b/priv/repo/migrations/20240930145536_add_image_data_hash_to_records.exs @@ -0,0 +1,9 @@ +defmodule MusicLibrary.Repo.Migrations.AddImageDataHashToRecords do + use Ecto.Migration + + def change do + alter table(:records) do + add :image_data_hash, :string + end + end +end