Crystal 0.21.0 support (#304)

Crystal 0.21.0 support
This commit is contained in:
Serdar Dogruyol 2017-02-21 21:24:11 +02:00 committed by GitHub
parent 54e501e964
commit b7ce53e5ab
9 changed files with 44 additions and 25 deletions

View file

@ -1,5 +1,4 @@
require "http"
require "multipart"
require "./kemal/*"
require "./kemal/helpers/*"

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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