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,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"]`.

View file

@ -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 <dogruyolserdar@gmail.com>

View file

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

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