Image upload using waffle_ecto

I am using waffle_ecto for image upload. I am trying to generate unique filenames for each upload. The unique file is uploaded to the local folder but the images are not showing up in the app.

This is my waffle_ecto file code

defmodule Shop.FileImage do
  use Waffle.Definition

  use Waffle.Ecto.Definition

  alias Ecto.UUID


  @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
 

  # Override the persisted filenames:
  def filename(:original, _) do
    UUID.generate()
  end

 # ADDED FUNCTIONS FOR AVATAR 
  def avatar_changeset(user, attrs) do
    user
    |> cast(attrs, [])
    |> cast_attachments(attrs, [:avatar])
  end

The file is saved with a unique name in the uploads folder but the app is not picking up.Am I missing something in the changeset. Should I add some code before the cast_attachments.Thanks

Hi, what do you mean by app is not picking up.?
I am having an issue of displaying the same image with the correct path.

Need help…

If I recall correctly doing this will generate a new uuid whenever the uploader is called. So the uuid generated when the file was first persisted will be different from the new that will be generated later when you try to serve it.

You should generate the uuid in your changeset then use it in the uploader.

For example:

  # Override the persisted filenames:
  def filename(_version, {_file, scope}), do: scope.uuid

This way the uuid will be generated only once and just be accessed in the uploader.