Skip to content

Conversation

@timfjord
Copy link

Introduces an optional normalize/1 callback to allow transforming/normalising the file after validation and before version processing/storage.
This enables use-cases like renaming files(generating unique file names) or fixing metadata in a centralised, explicit step.

The idea of this PR was born when I was looking for a way to generate unique file names for uploaded files.
There is the filename/2 function, but it is for runtime filename calculation and heavily relies on the scope, and I was looking for something that would rename the file before even processing the versions.

@achempion
Copy link
Member

achempion commented Oct 18, 2025

Would it be possible to pass the renamed File struct to the Uploader.store/1?

upload |> File.new() |> Map.put(:file_name, "normalized") |> then(&Uploader.store({&1, current_user}))

@timfjord
Copy link
Author

timfjord commented Oct 20, 2025

As per the current implementation, after normalisation, we pass whatever the normalize/1 returns to the put_versions/2 (partly, the goal of this extra step was to modify the File struct before creating versions).

So if I understand the question correctly, we do pass the renamed File struct

@achempion
Copy link
Member

@timfjord I meant, can you just call the normalize function before calling waffle? ex: User uploads a file and you can call the normalize to change the file name before passing the file struct to Waffle

@timfjord
Copy link
Author

timfjord commented Jan 1, 2026

Yeah, it is possible, but it makes the overall experience a bit inconsistent because there is no way to guarantee that the final file will have a unique name.
For example, in tests with something like https://github.com/beam-community/ex_machina in place, you need to duplicate this logic for every factory where a Waffle field is involved.
The same goes for some mix scripts, etc.
So basically every time you interact with a Waffle field, you need to remember to normalise whatever you pass there.

That's why it would be nice to have this normalisation option built into Waffle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants