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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews