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.

First 3 of 3 Posts Switch mode

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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement