Unable to store files with Waffle.Storage.local

Hi,

I have an app with Boats. Each Boat may have a picture.

So I’ve added this to boats/boat.exs:


defmodule MyApp.Boats.Boat do
  use Ecto.Schema
  use Waffle.Ecto.Schema

  schema "boats" do
    field :picture, MyApp.FileImage.Type

  def changeset(boat, attrs) do
    boat
    |> cast(attrs, [
[snipp..]
    |> cast_attachments(attrs, [:picture])

This is my config.exs:

config :waffle,
  storage: Waffle.Storage.Local

And this is my uploader:
lib/my_app/uploaders/file_image.ex


defmodule MyApp.FileImage do
  use Waffle.Definition
  use Waffle.Ecto.Definition

  @versions [:original]
 
  # Whitelist file extensions:
  def validate({file, _}) do
    file_extension = file.file_name |> Path.extname() |> String.downcase()

    case Enum.member?(~w(.jpg .jpeg .gif .png), file_extension) do
      true -> :ok
      false -> {:error, "invalid file type"}
    end
  end

  def storage_dir(version, {file, scope}) do
    "uploads/boats/pictures/#{scope.id}"
  end
end

 

Here´s my save_image().

  defp save_image(boat) do
    # Hard coded the local file, just to make sure that's a valid file.
    local_file = ~c"/tmp/solution-1-cover.png"
    FileImage.store({local_file, boat})
  end

When that is triggered, I get this error:

    ** (FunctionClauseError) no function clause matching in Waffle.Actions.Store.store/2

     The following arguments were given to Waffle.Actions.Store.store/2:

         # 1
         MyApp.FileImage

         # 2
         {~c"/tmp/solution-1-cover.png", %MyApp.Boats.Boat{... notion_cover_image_url: "https://prod-files-secure...", ...}}

     Attempted function clauses (showing 2 out of 2):

         def store(definition, {file, scope}) when is_binary(file) or is_map(file)
         def store(definition, filepath) when is_binary(filepath) or is_map(filepath)

I’m not able to see what I’m doing differetly than explained in the Waffle documentation for using local storage:
Avatar.store({"/path/to/my/selfie.png", current_user})

why do you convert it to a code list. It should be a string

2 Likes

I changed it " and now it works.

Thanks! :smiley:

1 Like