How to reorder the sequence of uploading files in `consume_uploaded_entries/3`

Hi community,

I am implementing a logic into my file uploader in which the first image of the selected list of images would have a special behaviour (in my case, it will be set as the thumbnail). However, I found out that the order of the uploaded images always flipped. For instance, filename, ["test_1.jpg", "test_2.jpg", "test_3.jpg"] will become ["test_3.jpg", "test_2.jpg", "test_1.jpg"].

My codes do not have much different from what you can find here Uploads — Phoenix LiveView v0.20.2, so I am curious if there is a way to set the order of the sequence in consume_uploaded_entries/3.

I don’t understand what is the sorting behaviour as I tried with different size, naming, it will always be flipped (descending)

Thanks in advance! :smiley:

I thought of a temporary solution:

Reversing the entries and reassign them back to the socket. From my understanding, consume_uploaded_entries/3 is a recursive function that is why the entry was flipped.

socket =
  Map.put(
    socket,
    :assigns,
    Map.put(
      assigns,
      :uploads,
      Map.put(uploads, :medias, Map.put(medias, :entries, Enum.reverse(entries)))
    )
  )

You can replace consume_uploaded_entries /3 with uploaded_entries/2 and consume_uploaded_entry/3 like this

def handle_event("save", _params, socket) do
  case uploaded_entries(socket, :documents) do
      {[_|_] = entries, []} ->
        entries = Enum.reverse(entries)
        uploaded_files = for entry <- entries do
          consume_uploaded_entry(socket, entry, fn %{path: path} ->
            dest = Path.join("priv/static/uploads", Path.basename(path))
            File.cp!(path, dest)
            {:ok, Routes.static_path(socket, "/uploads/#{Path.basename(dest)}")}
          end)
        end
        {:noreply, update(socket, :uploaded_files, &(&1 ++ uploaded_files))}
  
      _ ->
        {:noreply, socket}
    end
end