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 all_files to support multiple file uploads in parameter names ending with [] #701

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/kemal/param_parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ module Kemal
PARTS = %w(url query body json files)
# :nodoc:
alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Any) | Array(JSON::Any)
getter files
getter files, all_files

def initialize(@request : HTTP::Request, @url : Hash(String, String) = {} of String => String)
@query = HTTP::Params.new({} of String => Array(String))
@body = HTTP::Params.new({} of String => Array(String))
@json = {} of String => AllParamTypes
@files = {} of String => FileUpload
@all_files = {} of String => Array(FileUpload)
@url_parsed = false
@query_parsed = false
@body_parsed = false
Expand Down Expand Up @@ -71,11 +72,17 @@ module Kemal
next unless upload

filename = upload.filename
name = upload.name

if !filename.nil?
@files[upload.name] = FileUpload.new(upload)
if name.ends_with?("[]")
@all_files[name] ||= [] of FileUpload
@all_files[name] << FileUpload.new(upload)
else
@files[name] = FileUpload.new(upload)
end
sdogruyol marked this conversation as resolved.
Show resolved Hide resolved
else
@body.add(upload.name, upload.body.gets_to_end)
@body.add(name, upload.body.gets_to_end)
end
end

Expand Down
Loading