elixir-waffle/waffle_ecto

How to use filename definition with ecto?

Closed this issue ยท 6 comments

I am using the Waffle.Definition to update the file name to a generated UUID.
The filename saved is the original file name.

Could you help me understanding what should I do differently.

defmodule Logo do
  use Waffle.Definition
  use Waffle.Ecto.Definition
  alias Ecto.UUID
  # Include ecto support (requires package waffle_ecto installed):
  # use Waffle.Ecto.Definition

  @versions [:thumb]

  # To add a thumbnail version:
  # @versions [:original, :thumb]

  # Override the bucket on a per definition basis:
  # def bucket do
  #   :custom_bucket_name
  # end

  # Whitelist file extensions:
  def validate({file, _}) do
    ~w(.jpg .jpeg .png) |> Enum.member?(Path.extname(file.file_name))
  end

  # Define a thumbnail transformation:
  def transform(:thumb, _) do
    {:convert, "-strip -thumbnail 400x400^ -gravity center -extent 400x400 -format png", :png}
  end

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

  # Override the storage directory:
  def storage_dir(_, {_, _}) do
    "logo/"
  end

  # Provide a default URL if there hasn't been a file uploaded
  # def default_url(version, scope) do
  #   "/images/avatars/default_#{version}.png"
  # end

  # Specify custom headers for s3 objects
  # Available options are [:cache_control, :content_disposition,
  #    :content_encoding, :content_length, :content_type,
  #    :expect, :expires, :storage_class, :website_redirect_location]
  #
  # def s3_object_headers(version, {file, scope}) do
  #   [content_type: MIME.from_path(file.file_name)]
  # end
end

Hi, @steffenix.

Have you resolved you issue? What solution you have chosen?

I have generated a UUID in the changeset that will be saved and I then use as a filename.

@achempion This problem still unresolved, I think came from arc code

@guigaoliveira could you please describe your problem by openning a new issue?

Changing the file name using def filename(version, _) do works partially:

The file saved to the storage (local HD for instance) does have the new name but in the DB, original file name is saved.

I'm also having this isssue. We are in need of changing the filename in our codebase, but we cannot easily change all the old files to fit the new naming scheme. Having the new name persisted in the database too would allow me to differentiate between "old" files, and "new" files by, for example, using a prefix for each filename indicating it's the new naming scheme.