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) # 0.18.0 (11-02-2016)
- Simpler file upload. File uploads can now be access from `HTTP::Server::Context` like `env.params.files["filename"]`. - 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: kilt:
github: jeromegn/kilt github: jeromegn/kilt
version: 0.3.3 version: 0.3.3
multipart:
github: RX14/multipart.cr
version: 0.1.2
author: author:
- Serdar Dogruyol <dogruyolserdar@gmail.com> - Serdar Dogruyol <dogruyolserdar@gmail.com>

View File

@ -117,9 +117,9 @@ describe "Macros" do
end end
describe "#gzip" do describe "#gzip" do
it "adds HTTP::DeflateHandler to handlers" do it "adds HTTP::CompressHandler to handlers" do
gzip true gzip true
Kemal.config.handlers[4].should be_a(HTTP::DeflateHandler) Kemal.config.handlers[4].should be_a(HTTP::CompressHandler)
end end
end end

View File

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

View File

@ -2,13 +2,24 @@
module Kemal module Kemal
struct FileUpload struct FileUpload
getter tmpfile : Tempfile getter tmpfile : Tempfile
getter tmpfile_path : String getter filename : String?
getter filename : String
getter meta : HTTP::FormData::FileMetadata
getter headers : HTTP::Headers 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) def initialize(upload)
@filename = @meta.filename.not_nil! @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 end
end end

View File

@ -70,5 +70,5 @@ end
# output, either using gzip or deflate, depending on the `Accept-Encoding` request header. # output, either using gzip or deflate, depending on the `Accept-Encoding` request header.
# It's disabled by default. # It's disabled by default.
def gzip(status : Bool = false) def gzip(status : Bool = false)
add_handler HTTP::DeflateHandler.new if status add_handler HTTP::CompressHandler.new if status
end end

View File

@ -70,17 +70,13 @@ module Kemal
end end
private def parse_file_upload private def parse_file_upload
HTTP::FormData.parse(@request) do |field, data, meta, headers| HTTP::FormData.parse(@request) do |upload|
next unless meta next unless upload
filename = meta.filename filename = upload.filename
if !filename.nil? if !filename.nil?
tempfile = Tempfile.new(filename) @files[upload.name] = FileUpload.new(upload: upload)
::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)
else else
@body[field] = data.gets_to_end @body[upload.name] = upload.body.gets_to_end
end end
end end
end end

View File

@ -45,7 +45,7 @@ module Kemal
private def remove_tmpfiles(context) private def remove_tmpfiles(context)
context.params.files.each do |field, file| 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
end end

View File

@ -61,12 +61,12 @@ module Kemal
end end
if request_headers.includes_word?("Accept-Encoding", "gzip") && config.is_a?(Hash) && config["gzip"] == true && filesize > minsize && Utils.zip_types(file_path) 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" context.response.headers["Content-Encoding"] = "gzip"
Zlib::Deflate.gzip(context.response) do |deflate| Gzip::Writer.open(context.response) do |deflate|
IO.copy(file, deflate) IO.copy(file, deflate)
end end
elsif request_headers.includes_word?("Accept-Encoding", "deflate") && config.is_a?(Hash) && config["gzip"]? == true && filesize > minsize && Utils.zip_types(file_path) 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" context.response.headers["Content-Encoding"] = "deflate"
Zlib::Deflate.new(context.response) do |deflate| Flate::Writer.new(context.response) do |deflate|
IO.copy(file, deflate) IO.copy(file, deflate)
end end
else else