From eed97877a7ad28eabbf6fd7fa352fe62ade0ddb6 Mon Sep 17 00:00:00 2001 From: Serdar Dogruyol Date: Wed, 27 Jun 2018 23:33:28 +0300 Subject: [PATCH] Simplify file upload parsing and support multiple uploads with same name (#458) --- CHANGELOG.md | 20 ++++++++++++++++++++ src/kemal/param_parser.cr | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 497bd09..c1062d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,23 @@ +# Next + +- `env.params.files` is now an `Array(FileUpload)`. You can iterate over to access the images. + +```ruby +env.params.files.each do |file| + + filename = file.filename + + if !filename.is_a?(String) + "No filename included in upload" + else + file_path = ::File.join [Kemal.config.public_folder, "uploads/", filename] + File.open(file_path, "w") do |f| + IO.copy(file.tmpfile, f) + end + "Upload OK" +end +``` + # 0.23.0 (17-06-2018) - Crystal 0.25.0 support 🎉 diff --git a/src/kemal/param_parser.cr b/src/kemal/param_parser.cr index 188eb4f..65834e4 100644 --- a/src/kemal/param_parser.cr +++ b/src/kemal/param_parser.cr @@ -16,7 +16,7 @@ module Kemal @query = HTTP::Params.new({} of String => Array(String)) @body = HTTP::Params.new({} of String => Array(String)) @json = {} of String => AllParamTypes - @files = {} of String => FileUpload + @files = [] of FileUpload @url_parsed = false @query_parsed = false @body_parsed = false @@ -71,7 +71,7 @@ module Kemal next unless upload filename = upload.filename if !filename.nil? - @files[upload.name] = FileUpload.new(upload: upload) + @files << FileUpload.new(upload: upload) else @body.add(upload.name, upload.body.gets_to_end) end