nallwhy

nallwhy

Allow default uploader when using external uploaders in LiveView

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: "..."}
        _ -> nil
      end

    {:ok, meta, socket}
  end
end

If simply returning nil for the uploader in meta seems inappropriate, alternatives like %{uploader: nil} or %{uploader: :default} could be used instead.

If this sounds like a reasonable enhancement, I’d be happy to polish the implementation and open a PR. I think this could make LiveView uploads more flexible without breaking existing behavior.

Where Next?

Popular in Proposals: Ideas Top

beepboop
(re-post of: Feature idea: measure and expose socket latency · Issue #1890 · phoenixframework/phoenix_live_view · GitHub) This is relate...
New
mxgrn
As one edits a Phoenix LiveView form in a modal, it’s very easy to accidentally press ‘Esc’ and lose all the edits. Similar to data-conf...
New
shahryarjb
When proposing or suggesting something please consider: As my experience implementing getBoundingClientRect as Phoenix.LiveView.JS funct...
New
cortfritz
Each time i use phx.gen I end up adding dark mode to it. I’m offering to add this to the default phx gen templates. This would use ta...
New
GregPhx
Greetings Everyone!!! A little bit of my background so it could be easier to understand where my comments are coming from, and to take t...
New
sezaru
When writing my code, I always find __MODULE__ very useful to use as alias of that module “inner dependencies”, ex: alias __MODULE__.{Im...
New
woylie
We are seeing a lot of warning logs like this: navigate event to "https://someurl" failed because you are redirecting across live_sessio...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement