Store and edit artist images as assets
This commit is contained in:
@@ -2,6 +2,7 @@ defmodule MusicLibrary.Artists do
|
||||
import Ecto.Query, warn: false
|
||||
|
||||
alias MusicLibrary.Artists.ArtistInfo
|
||||
alias MusicLibrary.Assets
|
||||
alias MusicLibrary.Records.{ArtistRecord, Record}
|
||||
alias MusicLibrary.Repo
|
||||
alias MusicLibrary.{BackgroundRepo, Worker}
|
||||
@@ -114,11 +115,11 @@ defmodule MusicLibrary.Artists do
|
||||
artist_info = get_artist_info!(artist_id)
|
||||
|
||||
with {:ok, image} <- ArtistInfo.extract_image(artist_info),
|
||||
{:ok, image_data} <- Discogs.get_artist_image(image.url) do
|
||||
{:ok, image_data} <- Discogs.get_artist_image(image.url),
|
||||
{:ok, asset} <- Assets.store_image(%{content: image_data, format: "image/jpeg"}) do
|
||||
artist_info
|
||||
|> ArtistInfo.changeset(%{
|
||||
image_data: image_data,
|
||||
image_data_width: image.width
|
||||
image_data_hash: asset.hash
|
||||
})
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@@ -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