Live view Uploads get deleted before processing on heroku

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.

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.

1 Like

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.

3 Likes

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