Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple file uploads for params.files #700

Closed
wants to merge 1 commit into from

Conversation

sdogruyol
Copy link
Member

@sdogruyol sdogruyol commented Jan 13, 2025

Description of the Change

Currently you can't have an array of files for the given name.
This PR adds support for multiple file uploads for params.files

Benefits

Users can now access multiple file uploads with the given name in params.files

images = env.params.files["images[]"]?

# Loop through images and save each
if images
  images.each do |image|
    unless image.filename.not_nil!.empty?
      file = image.tempfile

      File.open(file_path, "w") do |f|
        f.write(file.gets_to_end.to_slice)
      end
    end
  end
end

Possible Drawbacks

This will break all the existing code as env.params.files["images[]"] was only returning a single file upload.
Users will now need to use env.params.files["images[]"][0] to have the same behavior.

@straight-shoota
Copy link
Contributor

straight-shoota commented Jan 13, 2025

Breaking the API in this way is a very drastic action. It would require a lot of changes in kemal applications which result in less ergonomic code. I'd try to avoid such a break and find an alternative means to implement accessing multiple file uploads instead.

The most common case is a single file per parameter. The API should provide a convenient access mechanism for that. files["foo"] works well for this.
Having to use files["foo"][0] everywhere seems silly for most use cases, where only a single file is expected anyway.
And it's even more troublesome for effectively handling the absence of a file, which would end up with something like files["foo"]?.try(&.[0]) instead of files["foo"]?.
The suggested change would be unoptimizing the API for the most common use case.

How about we add a new API for accessing multiple file uploads per parameter? Something like files.all_files("images[]"). Of course, naming isn't ideal. We'd have more freedom if we were designing this from scratch. But I think it's important to keep the existing #files API and add new features in a non-destructive way.

@sdogruyol
Copy link
Member Author

Thanks for the great reply @straight-shoota, I'm totally on the same page with you.

I've also thought about adding a all_files method to avoid breaking everyone's app.
If that sounds OK give a thumbs up and I'll implement it with a new PR

@sdogruyol
Copy link
Member Author

Superseded by #701

@sdogruyol sdogruyol closed this Jan 14, 2025
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