From 7efe69ac313ef453d42bf0354175749b732c8f60 Mon Sep 17 00:00:00 2001 From: sdogruyol Date: Sat, 1 Oct 2016 18:18:28 +0300 Subject: [PATCH] Add multipart support <3 --- shard.yml | 4 ++++ spec/helpers_spec.cr | 2 +- src/kemal.cr | 1 + src/kemal/cli.cr | 2 +- src/kemal/helpers/helpers.cr | 17 +++++++++++++++++ 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/shard.yml b/shard.yml index f25532c..2cac778 100644 --- a/shard.yml +++ b/shard.yml @@ -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 diff --git a/spec/helpers_spec.cr b/spec/helpers_spec.cr index 94dfdc0..d1e4436 100644 --- a/spec/helpers_spec.cr +++ b/spec/helpers_spec.cr @@ -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 diff --git a/src/kemal.cr b/src/kemal.cr index 8555e63..9720b8d 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -1,4 +1,5 @@ require "http" +require "multipart" require "./kemal/*" require "./kemal/helpers/*" require "./kemal/middleware/*" diff --git a/src/kemal/cli.cr b/src/kemal/cli.cr index 0f7f772..41774ce 100644 --- a/src/kemal/cli.cr +++ b/src/kemal/cli.cr @@ -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 diff --git a/src/kemal/helpers/helpers.cr b/src/kemal/helpers/helpers.cr index fcfe260..f5f41a1 100644 --- a/src/kemal/helpers/helpers.cr +++ b/src/kemal/helpers/helpers.cr @@ -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