Store image hashes in the db - part 1
This commit is contained in:
@@ -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, [
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user