arpan

arpan

Hi everyone, I have created a project which involves live view file uploads but I am facing a weird problem.

After the user uploads a file, I copy the uploaded file to a directory and then upload it to S3 using Arc.

This works fine in my local development environment however after deploying to Heroku the uploaded file is not found for some reason leading to an error when I try to copy the file.

2021-01-26T13:00:47.055190+00:00 app[web.1]: ** (File.CopyError) could not copy from "/tmp/plug-1611/live_view_upload-1611666046-602244053833136-3" to "priv/static/uploads/live_view_upload-1611666046-602244053833136-3": no such file or directory
2021-01-26T13:00:47.055191+00:00 app[web.1]: (elixir 1.11.2) lib/file.ex:816: File.cp!/3

The destination directory priv/static/uploads/ does exists on heroku so the problem is with the source that is /tmp/plug-1611/live_view_upload-1611666046-602244053833136-3 I believe.

Here’s the relevant code…

From my live view

@impl Phoenix.LiveView
  def handle_event("update_profile", %{"profile" => params}, socket) do
    params =
      if socket.assigns.remove_existing_avatar, do: Map.put(params, "avatar", nil), else: params

    Logger.warn(File.ls!("/tmp"))  # This shows that /tmp/plug-1611 does exist at this point
    socket.assigns.user
    |> Accounts.update_profile(params)
    |> case do
      {:error, profile_changeset} ->
        {
          :noreply,
          socket
          |> assign(profile_changeset: profile_changeset |> Map.put(:action, :insert))
          |> clear_flash()
          |> put_flash(:error, "Failed to update profile, check for errors")
        }

      _ ->
        case handle_avatar_upload(socket, socket.assigns.user) do
          {:error, _} ->
            {
              :noreply,
              socket
              |> put_flash(:error, "Avatar upload failed")
            }

          _ ->
            {
              :noreply,
              socket
              |> put_flash(:info, "Profile updated successfully")
              |> redirect(to: Routes.settings_path(socket, :index))
            }
        end
    end
  end

  def handle_avatar_upload(socket, user) do
    consume_uploaded_entries(socket, :avatar, fn %{path: path}, _entry ->
      dest = Path.join("priv/static/uploads", Path.basename(path))
      File.cp!(path, dest)  # ERROR HERE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      dest
    end)
    |> case do
      [file_path] ->
        case Avatar.store({file_path, user}) do
          {:ok, img_url} ->
            File.rm(file_path)
            Accounts.update_avatar(user, img_url)

          _ ->
            File.rm(file_path)
            {:error, "Image upload failed"}
        end

      _ ->
        :ok
    end
  end

I don’t understand why this problem would occur in Heroku and not in my local development environment.

I am stuck with this, any help or advice will be very helpful for me.

Showing Posts 1 to 3

arpan

arpan OP

I found a fix finally after long hours of debugging.

Turns out the problem was not in the source but the destination path passed to File.cp!

Changing this…

consume_uploaded_entries(socket, :avatar, fn %{path: path}, _entry ->
      dest = Path.join("priv/static/uploads", Path.basename(path))
      File.cp!(path, dest)  # ERROR HERE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      dest
    end)

to…

    consume_uploaded_entries(socket, :avatar, fn %{path: path}, _entry ->
      File.mkdir("uploads")
      dest = Path.join("uploads", Path.basename(path))
      File.cp!(path, dest)
      dest
    end)

Fixed the issue, I suspect this is because of some relative path issue with Path.join("priv/static/uploads", Path.basename(path))

This happened in Heroku and not on my local environment probably because the current directory was set correctly in my local environemnt so “priv/static/uploads” was found.

al2o3cr

al2o3cr

FWIW, if you need to reliably reference “application :some_app’s priv directory” it’s best to use :code.priv_dir(:some_app) and avoid working-directory hassles.

arpan

arpan OP

Nice, didn’t know this was possible. It is definitely a good idea. Thanks :+1:

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews