Skip to content

Allow default uploader when using external uploaders in LiveView - #3801

Open
nallwhy wants to merge 3 commits into
phoenixframework:mainfrom
nallwhy:allow_default_uploader_when_using_external_uploaders
Open

Allow default uploader when using external uploaders in LiveView#3801
nallwhy wants to merge 3 commits into
phoenixframework:mainfrom
nallwhy:allow_default_uploader_when_using_external_uploaders

Conversation

@nallwhy

@nallwhy nallwhy commented May 14, 2025

Copy link
Copy Markdown
Contributor

In the current implementation of Phoenix.LiveView.Upload, when you configure external: &function/2, it becomes mandatory to specify an uploader.

This makes sense in most cases, but there’s an important limitation: Once you introduce even a single external uploader, you lose the ability to fall back to the default built-in uploader.

This can be problematic when you want to mix different uploader strategies depending on the file — for instance, using a custom external uploader for images, but sticking to the built-in uploader for PDFs or other files.

I’ve patched LiveView locally to support falling back to the default uploader even when external: &function/2 is set — and it works well in my testing.

defmodule MyApp.SampleLive do
  ...
  def mount(_, _, socket) do
    socket =
      socket
      |> allow_upload(:files, external: &presign_upload/2)

    {:ok, socket}
  end

  defp presign_upload(entry, socket) do
    meta =
      case entry.client_type do
        "image/" <> _ -> %{uploader: "S3", presigned_url: "..."}
        _ -> %{uploader: :default}
      end

    {:ok, meta, socket}
  end
end

If simply returning :default for the uploader in meta seems inappropriate.

https://elixirforum.com/t/allow-default-uploader-when-using-external-uploaders-in-liveview/70501

@SteffenDE
SteffenDE requested a review from chrismccord May 28, 2025 14:26
@SteffenDE
SteffenDE force-pushed the allow_default_uploader_when_using_external_uploaders branch from 9adf4c5 to 7d797d3 Compare August 14, 2026 14:46
Copilot AI lite review requested due to automatic review settings August 14, 2026 14:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Phoenix.LiveView.Upload to allow an external: &fun/2 preflight to selectively fall back to the built-in channel uploader (via {:ok, :default, socket}), enabling mixed upload strategies within a single upload configuration.

Changes:

  • Extend external preflight handling to accept {:ok, :default, socket} and return a signed channel upload token for that entry.
  • Refactor upload consumption to support consuming a mix of channel-uploaded and externally-uploaded entries under the same upload config.
  • Add unit + E2E coverage demonstrating mixed default/channel and external uploaders.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/phoenix_live_view/upload/external_test.exs Adds coverage for external preflight returning :default to use built-in uploader.
test/e2e/tests/uploads.spec.js Adds E2E scenario mixing default and external uploaders in one form submission.
test/e2e/test_helper.exs Adds a test external uploader implementation and an external upload endpoint for E2E tests.
test/e2e/support/upload_live.ex Adds mixed-upload LiveView behavior and renders uploader type for assertions.
lib/phoenix_live_view/upload.ex Implements :default fallback for external preflight and refactors consumption to handle mixed entries.
lib/phoenix_live_view/test/upload_client.ex Updates test upload client to handle external entries (meta map tokens) and mixed uploads.
lib/phoenix_live_view/test/live_view_test.ex Uses a unified upload client start path for external and channel uploads.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread lib/phoenix_live_view/test/upload_client.ex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants