Retrieving file names of uploads while consuming entries

HELLO.

I am trying to do file uploads in liveview.While i am uploading entries, i have noticed that the name defaults to liveview upload_unique id . check out this save event

  def handle_event("save", _params, socket) do
    uploaded_files =
      consume_uploaded_entries(socket, :avatar, fn %{path: path}, _entry ->
        dest = Path.join(Application.app_dir(:jungle, "priv/static/uploads"), Path.basename(path))
        # You will need to create `priv/static/uploads` for `File.cp!/2` to work.
        name = System.shell("exiftool  -FileName  #{path}")
        dbg(name)
        # System.shell("ffmpeg -i #{path} -f ffmetadata in.txt")
        # System.shell("ffmpeg -i #{path} lovejustindavedd.m4a")
        File.cp!(path, dest)
        {:ok, ~p"/uploads/#{Path.basename(dest)}"}
      end)

    {:noreply, update(socket, :uploaded_files, &(&1 ++ uploaded_files))}
  end

the path variable which as i have logged in iex contains the actual upload
How can i retrieve the file name from the upload ??

When you say ā€œfile nameā€ do you mean the name of the file being uploaded? If so its stored as the client name.

If you inspect the socket uploads:

IO.inspect(socket.assigns.uploads)

You will get something like this:

%{
  __phoenix_refs_to_names__: %{"phx-F_nCA8FN_nDDJWDD" => :media},
  media: #Phoenix.LiveView.UploadConfig<
    name: :media,
    max_entries: 10,
    max_file_size: 20000000,
    entries: [
      %Phoenix.LiveView.UploadEntry{
        progress: 0,
        preflighted?: false,
        upload_config: :media,
        upload_ref: "phx-F_nCA8FN_nDDJWDD",
        ref: "0",
        uuid: "4ae37c91-3f47-4ce2-87a3-15f7dfe39482",
        valid?: true,
        done?: false,
        cancelled?: false,
        client_name: "1707793389785110.png",
        client_relative_path: "",
        client_size: 457675,
        client_type: "image/png",
        client_last_modified: 1707812644000,
        client_meta: nil
      }
    ],
    accept: ".png,.jpeg,.jpg,.gif,.mp4,.webm",
    ref: "phx-F_nCA8FN_nDDJWDD",
    errors: [],
    auto_upload?: false,
    progress_event: nil,
    writer: #Function<1.57915182/3 in Phoenix.LiveView.UploadConfig.build/3>,
    ...
  >
}

The way you can access it:

entries = socket.assigns.uploads.avatar.entries

for entry <- entries do
  entry.client_name
end
1 Like

Thanks for that :sunglasses: