store server_ip on uploaded files

This commit is contained in:
Kurt 2021-11-17 09:40:31 -06:00 committed by Chris McCord
parent 5ce9142253
commit 2243651c70
4 changed files with 20 additions and 1 deletions

View file

@ -158,6 +158,7 @@ defmodule LiveBeats.MediaLibrary do
chset
|> Song.put_user(user)
|> Song.put_mp3_path()
|> Song.put_server_ip()
Ecto.Multi.insert(acc, {:song, ref}, chset)
end)

View file

@ -19,6 +19,7 @@ defmodule LiveBeats.MediaLibrary.Song do
field :mp3_url, :string
field :mp3_filepath, :string
field :mp3_filename, :string
field :server_ip, EctoNetwork.INET
belongs_to :user, Accounts.User
belongs_to :genre, LiveBeats.MediaLibrary.Genre
@ -32,7 +33,7 @@ defmodule LiveBeats.MediaLibrary.Song do
@doc false
def changeset(song, attrs) do
song
|> cast(attrs, [:album_artist, :artist, :title, :attribution, :date_recorded, :date_released])
|> cast(attrs, [:album_artist, :artist, :title, :attribution, :date_recorded, :date_released, :server_ip])
|> validate_required([:artist, :title])
|> unique_constraint(:title,
message: "is a duplicated from another song",
@ -68,6 +69,13 @@ defmodule LiveBeats.MediaLibrary.Song do
end
end
def put_server_ip(%Ecto.Changeset{} = changeset) do
server_ip = (System.get_env("LIVE_BEATS_SERVER_IP") || "127.0.0.1")
changeset
|> Ecto.Changeset.cast(%{server_ip: server_ip}, [:server_ip])
end
defp mp3_url(filename) do
%{scheme: scheme, host: host, port: port} = Enum.into(LiveBeats.config([:files, :host]), %{})
URI.to_string(%URI{scheme: scheme, host: host, port: port, path: "/files/#{filename}"})

View file

@ -7,6 +7,7 @@
"db_connection": {:hex, :db_connection, "2.4.0", "d04b1b73795dae60cead94189f1b8a51cc9e1f911c234cc23074017c43c031e5", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ad416c21ad9f61b3103d254a71b63696ecadb6a917b36f563921e0de00d7d7c8"},
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
"ecto": {:hex, :ecto, "3.7.1", "a20598862351b29f80f285b21ec5297da1181c0442687f9b8329f0445d228892", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d36e5b39fc479e654cffd4dbe1865d9716e4a9b6311faff799b6f90ab81b8638"},
"ecto_network": {:hex, :ecto_network, "1.3.0", "1e77fa37c20e0f6a426d3862732f3317b0fa4c18f123d325f81752a491d7304e", [:mix], [{:ecto_sql, ">= 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:phoenix_html, ">= 0.0.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.14.0", [hex: :postgrex, repo: "hexpm", optional: false]}], "hexpm", "053a5e46ef2837e8ea5ea97c82fa0f5494699209eddd764e663c85f11b2865bd"},
"ecto_sql": {:hex, :ecto_sql, "3.7.0", "2fcaad4ab0c8d76a5afbef078162806adbe709c04160aca58400d5cbbe8eeac6", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.4.0 or ~> 0.5.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a26135dfa1d99bf87a928c464cfa25bba6535a4fe761eefa56077a4febc60f70"},
"erlog": {:git, "git://github.com/segun/erlog.git", "76825e0500b6b62b99f28411933dc15a1cb2817f", [ref: "master"]},
"erlp3tags": {:git, "https://github.com/segun/erlp3tags.git", "9b6c72da9057b9e00c7711148ce10e6cdbacae18", []},

View file

@ -0,0 +1,9 @@
defmodule LiveBeats.Repo.Migrations.AddServerIpToSongs do
use Ecto.Migration
def change do
alter table(:songs) do
add :server_ip, :inet
end
end
end