yeroc

yeroc

Hi all!

Trying to follow along with this and hitting an error I cant figure out. When we first get to testing %Plug.Upload{} in IEx after implementing the create_upload_from_plug_upload fn, I’m getting a function clause error.

If I understand the logs, apparently I’m giving a nil value to Upload.local_path(). I’m not sure how to rectify this, as I can’t figure out how I’m giving a nil value. Is my upload.id value nil for File.cp/2 ? I don’t understand how if so.

I’ve followed the tutorial up to this point without any issues and my code is identical. Thanks for any help!

Here is the full error -

<code>iex(3)> Documents.create_upload_from_plug_upload(upload) 
[debug] QUERY OK db=13.9ms idle=487.9ms
begin []
↳ :erl_eval.do_apply/7, at: erl_eval.erl:744
[debug] QUERY OK db=2.9ms
INSERT INTO "uploads" ("content_type","filename","hash","size","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5,$6) RETURNING "id" ["image/png", "phoenix.png", "07aa9b01595fe10fd4e5ceb6cc67ba186ef8ce91e5a5cba47166f7d8498d7852", 13900, ~N[2022-11-16 07:26:37], ~N[2022-11-16 07:26:37]]
↳ anonymous fn/4 in Woof.Documents.create_upload_from_plug_upload/1, at: lib/woof/documents.ex:25
[debug] QUERY OK db=0.3ms
rollback []
↳ :erl_eval.do_apply/7, at: erl_eval.erl:744
** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1 

The following arguments were given to IO.chardata_to_string/1:

# 1
nil

Attempted function clauses (showing 2 out of 2):

def chardata_to_string(string) when is_binary(string)
def chardata_to_string(list) when is_list(list)

(elixir 1.13.4) IO.chardata_to_string/1
(elixir 1.13.4) lib/path.ex:538: Path.join/2
(elixir 1.13.4) lib/path.ex:509: Path.join/1
(woof 0.1.0) lib/woof/documents.ex:26: anonymous fn/4 in Woof.Documents.create_upload_from_plug_upload/1
(ecto_sql 3.9.0) lib/ecto/adapters/sql.ex:1190: anonymous fn/3 in Ecto.Adapters.SQL.checkout_or_transaction/4
(db_connection 2.4.2) lib/db_connection.ex:1562: DBConnection.run_transaction/4</code>

And my relevant code-

lib.woof.documents.ex

defmodule Woof.Documents do
  import Ecto.Query, warn: false

  alias Woof.Repo
  alias Woof.Documents.Upload

  def create_upload_from_plug_upload(%Plug.Upload{
        filename: filename,
        path: tmp_path,
        content_type: content_type
      }) do
    hash =
      File.stream!(tmp_path, [], 2048)
      |> Upload.sha256()

    with {:ok, %File.Stat{size: size}} <- File.stat(tmp_path),
         {:ok, upload} <-
           %Upload{}
           |> Upload.changeset(%{
             filename: filename,
             content_type: content_type,
             hash: hash,
             size: size
           })
           |> Repo.insert(),
         :ok <-
           File.cp(
             tmp_path,
             Upload.local_path(upload.id, filename)
           ) do
      {:ok, upload}
    else
      {:error, reason} = error -> error
    end
  end
end

lib.woof.documents.upload.ex

defmodule Woof.Documents.Upload do
  use Ecto.Schema
  import Ecto.Changeset

  schema "uploads" do
    field :content_type, :string
    field :filename, :string
    field :hash, :string
    field :size, :integer

    timestamps()
  end

  @doc false
  def changeset(upload, attrs) do
    upload
    |> cast(attrs, [:filename, :size, :content_type, :hash])
    |> validate_required([:filename, :size, :content_type, :hash])
    |> validate_number(:size, greater_than: 0)
    |> validate_length(:hash, is: 64)
  end

  def sha256(chunks_enum) do
    chunks_enum
    |> Enum.reduce(
      :crypto.hash_init(:sha256),
      &:crypto.hash_update(&2, &1)
    )
    |> :crypto.hash_final()
    |> Base.encode16()
    |> String.downcase()
  end

  def upload_directory do
    Application.get_env(:woof, :uploads_directory)
  end

  def local_path(id, filename) do
    [upload_directory(), "#{id}-#{filename}"]
    |> Path.join()
  end
end

Showing Posts 1 to 2

al2o3cr

al2o3cr

The error you’re getting is what I’d expect if upload_directory() returned nil - if id or filename was nil, the "#{id}-#{filename}" part wouldn’t let those values get to Path.join.

iex(1)> Path.join(nil, "foo")
** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1    
    
    The following arguments were given to IO.chardata_to_string/1:
    
        # 1
        nil
    
    Attempted function clauses (showing 2 out of 2):
    
        def chardata_to_string(string) when is_binary(string)
        def chardata_to_string(list) when is_list(list)
    
    (elixir 1.14.0) lib/io.ex:670: IO.chardata_to_string/1
    (elixir 1.14.0) lib/path.ex:548: Path.join/2
    iex:1: (file)
yeroc

yeroc OP

I definitely track with that. I can successfully write the first upload with the front end, writing to the temporary uploads directory. So I clearly have a filename, but not an id. Don’t know why I’m missing that id. :thinking:

[debug] Processing with WoofWeb.UploadController.create/2
  Parameters: %{"_csrf_token" => "IRcuIzFFeS81JQZYORMQPjIMbWAGC2AhnNTEarLnAiu7QgfnSnXPtnUf", "upload" => %Plug.Upload{content_type: "image/jpeg", filename: "hound.jpg", path: "/var/folders/w_/rrq1p7rj7pq7js2brwm6wpsc0000gn/T/plug-1668/multipart-1668625495-142439731089-6"}}
  Pipelines: [:browser]
UPLOAD: %Plug.Upload{
  content_type: "image/jpeg",
  filename: "hound.jpg",
  path: "/var/folders/w_/rrq1p7rj7pq7js2brwm6wpsc0000gn/T/plug-1668/multipart-1668625495-142439731089-6"
}
[info] Sent 200 in 26ms
— All posts loaded —

Where Next? Top

Trending in Discussions Top

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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
jtormey
Lately I’ve been thinking about how to organize components as a LiveView application grows. One of the pain points I’ve found (for myself...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
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
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
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
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews