Serve local cover images

This commit is contained in:
Claudio Ortolina
2024-09-16 09:50:07 +01:00
parent f1160eddd4
commit afc1802399
12 changed files with 92 additions and 15 deletions
+5
View File
@@ -42,6 +42,11 @@ defmodule MusicLibrary.Records do
"""
def get_record!(id), do: Repo.get!(Record, id)
def get_image!(id) do
record = get_record!(id)
{:ok, record.image_data}
end
@doc """
Creates a record.
+29
View File
@@ -61,6 +61,14 @@ defmodule MusicLibrary.Records.Importer do
end
end
def import_cover_image(record) do
with {:ok, image_data} <- blob_get(record.image_url) do
record
|> Rec.add_image_data(image_data)
|> MusicLibrary.Repo.update!()
end
end
def import_all do
Rec
|> MusicLibrary.Repo.all()
@@ -97,4 +105,25 @@ defmodule MusicLibrary.Records.Importer do
{:error, msg}
end
end
defp blob_get(url) do
req =
Finch.build(:get, url, [
{"User-Agent", "MusicLibrary/0.1.0 ( cloud8421@gmail.com )"}
])
case Finch.request(req, MusicLibrary.Finch) do
{:ok, response} when response.status == 200 ->
{:ok, response.body}
{:ok, response} when response.status in 301..308 ->
location = :proplists.get_value("location", response.headers)
blob_get(location)
other ->
msg = "Failed to fetch data from #{url}, reason: #{inspect(other)}"
Logger.error(msg)
{:error, msg}
end
end
end
+8 -3
View File
@@ -7,7 +7,8 @@ defmodule MusicLibrary.Records.Record do
schema "records" do
field :type, Ecto.Enum, values: [:album, :ep, :live, :compilation, :single, :other]
field :title, :string
field :image, :string
field :image_url, :string
field :image_data, :binary
field :year, :integer
field :musicbrainz_id, Ecto.UUID
field :genres, {:array, :string}
@@ -25,8 +26,8 @@ defmodule MusicLibrary.Records.Record do
@doc false
def changeset(record, attrs) do
record
|> cast(attrs, [:type, :title, :musicbrainz_id, :year, :genres, :image])
|> validate_required([:type, :title, :musicbrainz_id, :year, :genres, :image])
|> cast(attrs, [:type, :title, :musicbrainz_id, :year, :genres, :image_url])
|> validate_required([:type, :title, :musicbrainz_id, :year, :genres, :image_url])
end
def add_artists(record, artists_attrs) do
@@ -34,4 +35,8 @@ defmodule MusicLibrary.Records.Record do
|> change()
|> put_embed(:artists, artists_attrs)
end
def add_image_data(record, image_data) do
change(record, image_data: image_data)
end
end