elixir-waffle / waffle_ecto

Waffle.Ecto provides an integration with Waffle and Ecto
https://hexdocs.pm/waffle_ecto
112 stars 30 forks source link

Don't require `%{filename: _, path: _}` maps to be `Plug.Upload` structs #36

Closed micahbf closed 3 years ago

micahbf commented 3 years ago

Waffle does not have any matching on Plug.Upload structs, it just matches on %{filename: _, path: _}.

With Waffle.Ecto, there is currently no way to supply a plain map like that, because it explicitly matches on Plug.Upload here.

It would be nice if maps in that format were also accepted. My use case is having a local file that I want to upload, but with a different filename.

I would be happy to open a PR for this.

micahbf commented 3 years ago

For a bit more context, this is what I need to do currently:

    updated_file =
      case file do
        %{filename: _, binary: _} = file_hash ->
          %{file_hash | filename: document_params.key}

        %{filename: _, path: _} = file_hash ->
          %Plug.Upload{file_hash | filename: document_params.key}

        path when is_binary(path) ->
          %Plug.Upload{filename: document_params.key, path: path}
      end
achempion commented 3 years ago

There is also a similar case when casting urls, it would also be great to be able to override the filename in such case.

I would keep the plug as is and add new section to support custom filenames for paths and urls.

PR is welcome 👍