2021-10-29 16:12:23 +00:00
|
|
|
defmodule LiveBeats.MediaLibrary.Song do
|
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
|
|
|
|
2021-11-05 19:57:33 +00:00
|
|
|
alias LiveBeats.MediaLibrary.Song
|
2021-11-05 00:49:19 +00:00
|
|
|
alias LiveBeats.Accounts
|
|
|
|
|
2021-10-29 16:12:23 +00:00
|
|
|
schema "songs" do
|
|
|
|
field :album_artist, :string
|
|
|
|
field :artist, :string
|
2021-11-05 19:57:33 +00:00
|
|
|
field :played_at, :utc_datetime
|
|
|
|
field :paused_at, :utc_datetime
|
2021-10-29 16:12:23 +00:00
|
|
|
field :date_recorded, :naive_datetime
|
|
|
|
field :date_released, :naive_datetime
|
|
|
|
field :duration, :integer
|
2021-11-05 19:57:33 +00:00
|
|
|
field :status, Ecto.Enum, values: [stopped: 1, playing: 2, paused: 3]
|
2021-10-29 16:12:23 +00:00
|
|
|
field :title, :string
|
2021-11-10 15:10:43 +00:00
|
|
|
field :attribution, :string
|
2021-11-06 03:02:31 +00:00
|
|
|
field :mp3_url, :string
|
2021-11-05 19:57:33 +00:00
|
|
|
field :mp3_filepath, :string
|
2021-11-06 03:02:31 +00:00
|
|
|
field :mp3_filename, :string
|
2021-11-17 15:40:31 +00:00
|
|
|
field :server_ip, EctoNetwork.INET
|
2021-11-05 00:49:19 +00:00
|
|
|
belongs_to :user, Accounts.User
|
2021-10-29 16:12:23 +00:00
|
|
|
belongs_to :genre, LiveBeats.MediaLibrary.Genre
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2021-11-05 19:57:33 +00:00
|
|
|
def playing?(%Song{} = song), do: song.status == :playing
|
|
|
|
def paused?(%Song{} = song), do: song.status == :paused
|
|
|
|
def stopped?(%Song{} = song), do: song.status == :stopped
|
|
|
|
|
2021-10-29 16:12:23 +00:00
|
|
|
@doc false
|
|
|
|
def changeset(song, attrs) do
|
|
|
|
song
|
2022-01-27 14:36:04 +00:00
|
|
|
|> cast(attrs, [:album_artist, :artist, :title, :attribution, :date_recorded, :date_released])
|
2021-11-01 19:57:53 +00:00
|
|
|
|> validate_required([:artist, :title])
|
2021-11-19 19:01:15 +00:00
|
|
|
|> unique_constraint(:title,
|
|
|
|
message: "is a duplicated from another song",
|
|
|
|
name: "songs_user_id_title_artist_index"
|
|
|
|
)
|
2021-11-05 00:49:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def put_user(%Ecto.Changeset{} = changeset, %Accounts.User{} = user) do
|
|
|
|
put_assoc(changeset, :user, user)
|
2021-11-01 19:57:53 +00:00
|
|
|
end
|
|
|
|
|
2021-11-10 15:10:43 +00:00
|
|
|
def put_duration(%Ecto.Changeset{} = changeset, duration) when is_integer(duration) do
|
|
|
|
changeset
|
|
|
|
|> Ecto.Changeset.change(%{duration: duration})
|
|
|
|
|> Ecto.Changeset.validate_number(:duration,
|
|
|
|
greater_than: 0,
|
|
|
|
less_than: 1200,
|
|
|
|
message: "must be less than 20 minutes"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-11-01 19:57:53 +00:00
|
|
|
def put_mp3_path(%Ecto.Changeset{} = changeset) do
|
|
|
|
if changeset.valid? do
|
|
|
|
filename = Ecto.UUID.generate() <> ".mp3"
|
2021-11-06 03:02:31 +00:00
|
|
|
filepath = LiveBeats.MediaLibrary.local_filepath(filename)
|
2021-11-01 19:57:53 +00:00
|
|
|
|
|
|
|
changeset
|
2021-11-06 03:02:31 +00:00
|
|
|
|> Ecto.Changeset.put_change(:mp3_filename, filename)
|
2021-11-05 19:57:33 +00:00
|
|
|
|> Ecto.Changeset.put_change(:mp3_filepath, filepath)
|
2021-11-06 03:02:31 +00:00
|
|
|
|> Ecto.Changeset.put_change(:mp3_url, mp3_url(filename))
|
2021-11-01 19:57:53 +00:00
|
|
|
else
|
|
|
|
changeset
|
|
|
|
end
|
2021-10-29 16:12:23 +00:00
|
|
|
end
|
2021-11-06 03:02:31 +00:00
|
|
|
|
2021-11-17 15:40:31 +00:00
|
|
|
def put_server_ip(%Ecto.Changeset{} = changeset) do
|
2022-01-27 14:36:04 +00:00
|
|
|
server_ip = LiveBeats.config([:files, :server_ip])
|
|
|
|
Ecto.Changeset.cast(changeset, %{server_ip: server_ip}, [:server_ip])
|
2021-11-17 15:40:31 +00:00
|
|
|
end
|
|
|
|
|
2021-11-06 03:02:31 +00:00
|
|
|
defp mp3_url(filename) do
|
2021-11-17 03:11:11 +00:00
|
|
|
%{scheme: scheme, host: host, port: port} = Enum.into(LiveBeats.config([:files, :host]), %{})
|
2021-11-06 03:02:31 +00:00
|
|
|
URI.to_string(%URI{scheme: scheme, host: host, port: port, path: "/files/#{filename}"})
|
|
|
|
end
|
2021-10-29 16:12:23 +00:00
|
|
|
end
|