From dea9ccd16897e0cf873a3aa079f2e65063c80f52 Mon Sep 17 00:00:00 2001 From: Kurt Date: Wed, 17 Nov 2021 09:40:31 -0600 Subject: [PATCH] store server_ip on uploaded files --- lib/live_beats/media_library.ex | 1 + lib/live_beats/media_library/song.ex | 10 +++++++++- mix.exs | 3 ++- mix.lock | 1 + .../20211117144505_add_server_ip_to_songs.exs | 9 +++++++++ rel/env.sh.eex | 3 ++- 6 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 priv/repo/migrations/20211117144505_add_server_ip_to_songs.exs diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex index 4f6e19c..bc4d9b4 100644 --- a/lib/live_beats/media_library.ex +++ b/lib/live_beats/media_library.ex @@ -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) diff --git a/lib/live_beats/media_library/song.ex b/lib/live_beats/media_library/song.ex index c843ea4..cfc53ea 100644 --- a/lib/live_beats/media_library/song.ex +++ b/lib/live_beats/media_library/song.ex @@ -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}"}) diff --git a/mix.exs b/mix.exs index 34c8c71..ac2bfb0 100644 --- a/mix.exs +++ b/mix.exs @@ -52,7 +52,8 @@ defmodule LiveBeats.MixProject do {:plug_cowboy, "~> 2.5"}, {:mint, "~> 1.0"}, {:heroicons, "~> 0.2.2"}, - {:castore, "~> 0.1.13"} + {:castore, "~> 0.1.13"}, + {:ecto_network, "~> 1.3.0"} ] end diff --git a/mix.lock b/mix.lock index 592a856..40bfcab 100644 --- a/mix.lock +++ b/mix.lock @@ -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", []}, diff --git a/priv/repo/migrations/20211117144505_add_server_ip_to_songs.exs b/priv/repo/migrations/20211117144505_add_server_ip_to_songs.exs new file mode 100644 index 0000000..c2e85e1 --- /dev/null +++ b/priv/repo/migrations/20211117144505_add_server_ip_to_songs.exs @@ -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 diff --git a/rel/env.sh.eex b/rel/env.sh.eex index 69cea06..0ffe768 100644 --- a/rel/env.sh.eex +++ b/rel/env.sh.eex @@ -3,4 +3,5 @@ ip=$(grep fly-local-6pn /etc/hosts | cut -f 1) export RELEASE_DISTRIBUTION=name export RELEASE_NODE=$FLY_APP_NAME@$ip -export ELIXIR_ERL_OPTIONS="-proto_dist inet6_tcp" \ No newline at end of file +export ELIXIR_ERL_OPTIONS="-proto_dist inet6_tcp" +export LIVE_BEATS_SERVER_IP=$ip \ No newline at end of file