From 3ff4ae2beab94b1ced3958caab19a4fbdda06ff8 Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Fri, 5 Nov 2021 08:57:48 -0400 Subject: [PATCH] Drop invalid uploads as they happen --- lib/live_beats_web/live/live_helpers.ex | 9 ++++++ lib/live_beats_web/live/song_live/index.ex | 31 ++++++------------- .../live/song_live/song_entry_component.ex | 7 +++-- .../live/song_live/upload_form_component.ex | 28 +++++++++++++++-- .../song_live/upload_form_component.html.heex | 6 ++-- 5 files changed, 52 insertions(+), 29 deletions(-) diff --git a/lib/live_beats_web/live/live_helpers.ex b/lib/live_beats_web/live/live_helpers.ex index ce43499..375fcdf 100644 --- a/lib/live_beats_web/live/live_helpers.ex +++ b/lib/live_beats_web/live/live_helpers.ex @@ -4,6 +4,15 @@ defmodule LiveBeatsWeb.LiveHelpers do alias Phoenix.LiveView.JS + def spinner(assigns) do + ~H""" + + + + + """ + end + def icon(assigns) do assigns = assigns diff --git a/lib/live_beats_web/live/song_live/index.ex b/lib/live_beats_web/live/song_live/index.ex index a1b1752..c2ee5c8 100644 --- a/lib/live_beats_web/live/song_live/index.ex +++ b/lib/live_beats_web/live/song_live/index.ex @@ -48,7 +48,8 @@ defmodule LiveBeatsWeb.SongLive.Index do if connected?(socket) do MediaLibrary.subscribe(socket.assigns.current_user) end - {:ok, assign(socket, songs: list_songs(), active_id: nil)} + + {:ok, assign(socket, songs: list_songs(), active_id: nil), temporary_assigns: [songs: []]} end def handle_params(params, _url, socket) do @@ -66,11 +67,6 @@ defmodule LiveBeatsWeb.SongLive.Index do {:noreply, socket} end - def handle_info({_ref, {:duration, entry_ref, result}}, socket) do - IO.inspect({:async_duration, entry_ref, result}) - {:noreply, socket} - end - def handle_info({:play, %Song{} = song, _meta}, socket) do {:noreply, play_song(socket, song)} end @@ -80,21 +76,20 @@ defmodule LiveBeatsWeb.SongLive.Index do end defp pause_song(socket, song_id) do - if old = Enum.find(socket.assigns.songs, fn song -> song.id == song_id end) do - send_update(LiveBeatsWeb.SongLive.SongRow, id: "song-#{old.id}", action: :deactivate) - end + send_update(LiveBeatsWeb.SongLive.SongRow, id: "song-#{song_id}", action: :deactivate) socket end defp play_song(socket, %Song{} = song) do - socket = pause_song(socket, socket.assigns.active_id) - next = Enum.find(socket.assigns.songs, &(&1.id == song.id)) + send_update(LiveBeatsWeb.SongLive.SongRow, id: "song-#{song.id}", action: :activate) - if next do - send_update(LiveBeatsWeb.SongLive.SongRow, id: "song-#{next.id}", action: :activate) + if socket.assigns.active_id do + socket + |> pause_song(socket.assigns.active_id) + |> assign(active_id: song.id) + else + assign(socket, active_id: song.id) end - - assign(socket, active_id: next.id) end defp maybe_show_modal(socket) do @@ -116,12 +111,6 @@ defmodule LiveBeatsWeb.SongLive.Index do socket end - defp apply_action(socket, :edit, %{"id" => id}) do - socket - |> assign(:page_title, "Edit Song") - |> assign(:song, MediaLibrary.get_song!(id)) - end - defp apply_action(socket, :new, _params) do socket |> assign(:page_title, "Add Songs") diff --git a/lib/live_beats_web/live/song_live/song_entry_component.ex b/lib/live_beats_web/live/song_live/song_entry_component.ex index 4aba2fd..fa2ae22 100644 --- a/lib/live_beats_web/live/song_live/song_entry_component.ex +++ b/lib/live_beats_web/live/song_live/song_entry_component.ex @@ -11,7 +11,10 @@ defmodule LiveBeatsWeb.SongLive.SongEntryComponent do <%= if @duration do %> Title (<%= MP3Stat.to_mmss(@duration) %>) <% else %> - Title (calculating duration...) + Title + + (calculating duration <.spinner class="inline-block animate-spin h-2.5 w-2.5 text-gray-400"/>) + <% end %> assign(:errors, changeset.errors) |> assign(title: Ecto.Changeset.get_field(changeset, :title)) |> assign(artist: Ecto.Changeset.get_field(changeset, :artist)) - |> assign(duration: IO.inspect(Ecto.Changeset.get_field(changeset, :duration))) + |> assign(duration: Ecto.Changeset.get_field(changeset, :duration)) |> assign_new(:progress, fn -> 0 end)} end end diff --git a/lib/live_beats_web/live/song_live/upload_form_component.ex b/lib/live_beats_web/live/song_live/upload_form_component.ex index d221392..8104622 100644 --- a/lib/live_beats_web/live/song_live/upload_form_component.ex +++ b/lib/live_beats_web/live/song_live/upload_form_component.ex @@ -21,7 +21,7 @@ defmodule LiveBeatsWeb.SongLive.UploadFormComponent do {:ok, socket |> assign(assigns) - |> assign(changesets: %{}) + |> assign(changesets: %{}, error_messages: []) |> allow_upload(:mp3, song_id: song.id, auto_upload: true, @@ -34,7 +34,7 @@ defmodule LiveBeatsWeb.SongLive.UploadFormComponent do @impl true def handle_event("validate", %{"_target" => ["mp3"]}, socket) do - {:noreply, socket} + {:noreply, drop_invalid_uploads(socket)} end def handle_event("validate", %{"songs" => songs_params, "_target" => ["songs", _, _]}, socket) do @@ -56,7 +56,7 @@ defmodule LiveBeatsWeb.SongLive.UploadFormComponent do defp consume_entry(socket, ref, store_func) when is_function(store_func) do {entries, []} = uploaded_entries(socket, :mp3) entry = Enum.find(entries, fn entry -> entry.ref == ref end) - consume_uploaded_entry(socket, entry, fn meta -> store_func.(meta.path) end) + consume_uploaded_entry(socket, entry, fn meta -> {:ok, store_func.(meta.path)} end) end def handle_event("save", %{"songs" => song_params}, socket) do @@ -123,6 +123,7 @@ defmodule LiveBeatsWeb.SongLive.UploadFormComponent do {:noreply, put_new_changeset(socket, entry)} end + defp file_error(%{kind: :dropped} = assigns), do: ~H|dropped (exceeds limit of 10 files)| defp file_error(%{kind: :too_large} = assigns), do: ~H|larger than 10MB| defp file_error(%{kind: :not_accepted} = assigns), do: ~H|not a valid MP3 file| defp file_error(%{kind: :too_many_files} = assigns), do: ~H|too many files| @@ -134,4 +135,25 @@ defmodule LiveBeatsWeb.SongLive.UploadFormComponent do socket end end + + defp drop_invalid_uploads(socket) do + %{uploads: uploads} = socket.assigns + + {new_socket, error_messages, _index} = + Enum.reduce(uploads.mp3.entries, {socket, [], 0}, fn entry, {socket, msgs, i} -> + if i >= @max_songs do + {cancel_upload(socket, :mp3, entry.ref), [{entry.client_name, :dropped} | msgs], i + 1} + else + case upload_errors(uploads.mp3, entry) do + [first | _] -> + {cancel_upload(socket, :mp3, entry.ref), [{entry.client_name, first} | msgs], i + 1} + + [] -> + {socket, msgs, i + 1} + end + end + end) + + assign(new_socket, error_messages: error_messages) + end end diff --git a/lib/live_beats_web/live/song_live/upload_form_component.html.heex b/lib/live_beats_web/live/song_live/upload_form_component.html.heex index d97516b..50be1ad 100644 --- a/lib/live_beats_web/live/song_live/upload_form_component.html.heex +++ b/lib/live_beats_web/live/song_live/upload_form_component.html.heex @@ -19,7 +19,7 @@
- <%= if Enum.any?(@uploads.mp3.errors) do %> + <%= if Enum.any?(@error_messages) do %>
@@ -31,8 +31,8 @@
    - <%= for {_ref, error} <- @uploads.mp3.errors do %> -
  • <.file_error kind={error} />
  • + <%= for {client_name, error} <- @error_messages do %> +
  • <%= client_name %>: <.file_error kind={error} />
  • <% end %>