Store and edit artist images as assets
This commit is contained in:
@@ -3,15 +3,11 @@ defmodule MusicLibrary.Artists.ArtistInfo do
|
||||
|
||||
import Ecto.Changeset
|
||||
|
||||
alias MusicLibrary.Assets.Asset
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
schema "artist_infos" do
|
||||
field :musicbrainz_data, :map, default: %{}
|
||||
field :discogs_data, :map, default: %{}
|
||||
field :image_data, :binary
|
||||
field :image_data_hash, :string
|
||||
field :image_data_width, :integer
|
||||
|
||||
timestamps(type: :utc_datetime)
|
||||
end
|
||||
@@ -22,11 +18,9 @@ defmodule MusicLibrary.Artists.ArtistInfo do
|
||||
:id,
|
||||
:musicbrainz_data,
|
||||
:discogs_data,
|
||||
:image_data,
|
||||
:image_data_width
|
||||
:image_data_hash
|
||||
])
|
||||
|> validate_required([:musicbrainz_data])
|
||||
|> generate_image_hash()
|
||||
end
|
||||
|
||||
def country(artist_info) do
|
||||
@@ -48,20 +42,6 @@ defmodule MusicLibrary.Artists.ArtistInfo do
|
||||
String.slice(country_code, 0..1)
|
||||
end
|
||||
|
||||
def generate_image_hash(%__MODULE__{image_data: image_data} = artist_info) do
|
||||
change(artist_info, image_data_hash: Asset.hash(image_data))
|
||||
end
|
||||
|
||||
def generate_image_hash(changeset) do
|
||||
case get_change(changeset, :image_data) do
|
||||
nil ->
|
||||
changeset
|
||||
|
||||
image_data ->
|
||||
put_change(changeset, :image_data_hash, Asset.hash(image_data))
|
||||
end
|
||||
end
|
||||
|
||||
def extract_image(artist_info) when is_nil(artist_info.discogs_data) do
|
||||
{:error, :no_discogs_data}
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user