Merge pull request #216 from sdogruyol/multipart

Add multipart support <3
This commit is contained in:
Serdar Dogruyol 2016-10-01 18:43:04 +03:00 committed by GitHub
commit d33ca9f16b
5 changed files with 24 additions and 2 deletions

View file

@ -8,5 +8,9 @@ dependencies:
kilt:
github: jeromegn/kilt
version: 0.3.3
multipart:
github: RX14/multipart.cr
version: 0.1.0
author:
- Serdar Dogruyol <dogruyolserdar@gmail.com>

View file

@ -135,7 +135,7 @@ describe "Macros" do
end
describe "#serve_static" do
it "should disble static file hosting" do
it "should disable static file hosting" do
serve_static false
Kemal.config.serve_static.should eq false
end

View file

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

View file

@ -39,7 +39,7 @@ module Kemal
end
def configure_ssl
{% if ! flag?(:without_openssl) %}
{% if !flag?(:without_openssl) %}
if @ssl_enabled
puts "SSL Key Not Found"; exit unless @key_file
puts "SSL Certificate Not Found"; exit unless @cert_file

View file

@ -78,3 +78,20 @@ end
def gzip(status : Bool = false)
add_handler HTTP::DeflateHandler.new if status
end
# Parses a multipart/form-data request. This is really useful for file uploads.
# Consider the example below taking two image uploads as image1, image2. To get the relevant data
# for each field you can use simple `if/switch` conditional.
#
# post "/upload" do |env|
# parse_multipart(env) do |field, data|
# image1 = data if field == "image1"
# image2 = data if field == "image2"
# "Upload complete"
# end
# end
def parse_multipart(env)
HTTP::FormData.parse(env.request) do |field, data|
yield field, data
end
end