From b7ce53e5abc45142fa09dd792296d01008ff70e7 Mon Sep 17 00:00:00 2001 From: Serdar Dogruyol Date: Tue, 21 Feb 2017 21:24:11 +0200 Subject: [PATCH] Crystal 0.21.0 support (#304) Crystal 0.21.0 support --- CHANGELOG.md | 17 +++++++++++++++++ shard.yml | 4 ---- spec/helpers_spec.cr | 4 ++-- src/kemal.cr | 1 - src/kemal/file_upload.cr | 21 ++++++++++++++++----- src/kemal/helpers/helpers.cr | 2 +- src/kemal/param_parser.cr | 14 +++++--------- src/kemal/route_handler.cr | 2 +- src/kemal/static_file_handler.cr | 4 ++-- 9 files changed, 44 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a2e214..0cd5ff9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,20 @@ +# 0.18.1 (21-02-2016) + +- Crystal 0.21.0 support +- Drop `multipart.cr` dependency. `multipart` support is now built-into Crystal <3 +- Since Crystal 0.21.0 comes built-in with `multipart` there are some improvements and deprecations. + +`meta` has been removed from `FileUpload` and it has the following properties + + + `tmpfile`: This is temporary file for file upload. Useful for saving the upload file. + + `filename`: File name of the file upload. (logo.png, images.zip e.g) + + `headers`: Headers for the file upload. + + `creation_time`: Creation time of the file upload. + + `modification_time`: Last Modification time of the file upload. + + `read_time`: Read time of the file upload. + + `size`: Size of the file upload. + + # 0.18.0 (11-02-2016) - Simpler file upload. File uploads can now be access from `HTTP::Server::Context` like `env.params.files["filename"]`. diff --git a/shard.yml b/shard.yml index cf2e7b0..5538fdb 100644 --- a/shard.yml +++ b/shard.yml @@ -8,9 +8,5 @@ dependencies: kilt: github: jeromegn/kilt version: 0.3.3 - multipart: - github: RX14/multipart.cr - version: 0.1.2 - author: - Serdar Dogruyol diff --git a/spec/helpers_spec.cr b/spec/helpers_spec.cr index 534701c..272ee6b 100644 --- a/spec/helpers_spec.cr +++ b/spec/helpers_spec.cr @@ -117,9 +117,9 @@ describe "Macros" do end describe "#gzip" do - it "adds HTTP::DeflateHandler to handlers" do + it "adds HTTP::CompressHandler to handlers" do gzip true - Kemal.config.handlers[4].should be_a(HTTP::DeflateHandler) + Kemal.config.handlers[4].should be_a(HTTP::CompressHandler) end end diff --git a/src/kemal.cr b/src/kemal.cr index ba02d5e..19ca562 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -1,5 +1,4 @@ require "http" -require "multipart" require "./kemal/*" require "./kemal/helpers/*" diff --git a/src/kemal/file_upload.cr b/src/kemal/file_upload.cr index c41b43b..d463f3f 100644 --- a/src/kemal/file_upload.cr +++ b/src/kemal/file_upload.cr @@ -2,13 +2,24 @@ module Kemal struct FileUpload getter tmpfile : Tempfile - getter tmpfile_path : String - getter filename : String - getter meta : HTTP::FormData::FileMetadata + getter filename : String? getter headers : HTTP::Headers + getter creation_time : Time? + getter modification_time : Time? + getter read_time : Time? + getter size : UInt64? - def initialize(@tmpfile, @tmpfile_path, @meta, @headers) - @filename = @meta.filename.not_nil! + def initialize(upload) + @tmpfile = Tempfile.new(filename) + ::File.open(@tmpfile.path, "w") do |file| + IO.copy(upload.body, file) + end + @filename = upload.filename + @headers = upload.headers + @creation_time = upload.creation_time + @modification_time = upload.modification_time + @read_time = upload.read_time + @size = upload.size end end end diff --git a/src/kemal/helpers/helpers.cr b/src/kemal/helpers/helpers.cr index 9825cff..de06d9e 100644 --- a/src/kemal/helpers/helpers.cr +++ b/src/kemal/helpers/helpers.cr @@ -70,5 +70,5 @@ end # output, either using gzip or deflate, depending on the `Accept-Encoding` request header. # It's disabled by default. def gzip(status : Bool = false) - add_handler HTTP::DeflateHandler.new if status + add_handler HTTP::CompressHandler.new if status end diff --git a/src/kemal/param_parser.cr b/src/kemal/param_parser.cr index afa9e76..bfd963a 100644 --- a/src/kemal/param_parser.cr +++ b/src/kemal/param_parser.cr @@ -70,17 +70,13 @@ module Kemal end private def parse_file_upload - HTTP::FormData.parse(@request) do |field, data, meta, headers| - next unless meta - filename = meta.filename + HTTP::FormData.parse(@request) do |upload| + next unless upload + filename = upload.filename if !filename.nil? - tempfile = Tempfile.new(filename) - ::File.open(tempfile.path, "w") do |file| - IO.copy(data, file) - end - @files[field] = FileUpload.new(tmpfile: tempfile, tmpfile_path: tempfile.path, meta: meta, headers: headers) + @files[upload.name] = FileUpload.new(upload: upload) else - @body[field] = data.gets_to_end + @body[upload.name] = upload.body.gets_to_end end end end diff --git a/src/kemal/route_handler.cr b/src/kemal/route_handler.cr index e428179..deb41a9 100644 --- a/src/kemal/route_handler.cr +++ b/src/kemal/route_handler.cr @@ -45,7 +45,7 @@ module Kemal private def remove_tmpfiles(context) context.params.files.each do |field, file| - File.delete(file.tmpfile_path) if ::File.exists?(file.tmpfile_path) + File.delete(file.tmpfile.path) if ::File.exists?(file.tmpfile.path) end end diff --git a/src/kemal/static_file_handler.cr b/src/kemal/static_file_handler.cr index 3b7f033..ce14209 100644 --- a/src/kemal/static_file_handler.cr +++ b/src/kemal/static_file_handler.cr @@ -61,12 +61,12 @@ module Kemal end if request_headers.includes_word?("Accept-Encoding", "gzip") && config.is_a?(Hash) && config["gzip"] == true && filesize > minsize && Utils.zip_types(file_path) context.response.headers["Content-Encoding"] = "gzip" - Zlib::Deflate.gzip(context.response) do |deflate| + Gzip::Writer.open(context.response) do |deflate| IO.copy(file, deflate) end elsif request_headers.includes_word?("Accept-Encoding", "deflate") && config.is_a?(Hash) && config["gzip"]? == true && filesize > minsize && Utils.zip_types(file_path) context.response.headers["Content-Encoding"] = "deflate" - Zlib::Deflate.new(context.response) do |deflate| + Flate::Writer.new(context.response) do |deflate| IO.copy(file, deflate) end else