Skip to content

Commit

Permalink
Pass in if form is multipart
Browse files Browse the repository at this point in the history
It's possible to have a multipart form and not have or supply a file field. Avram bombed because it looks for a file and then raises an error because the form was not multipart encoded
  • Loading branch information
matthewmcgarvey committed Jan 27, 2022
1 parent 6109049 commit bd69f8b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/webless/request_builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class Webless::RequestBuilder
clone.tap(&.params[key] = value)
end

def form(form : Hash(String, _) | NamedTuple) : RequestBuilder
result = FormHandler.handle(form)
def form(form : Hash(String, _) | NamedTuple, multipart : Bool = false) : RequestBuilder
result = FormHandler.handle(form, multipart)
body(result[:body]).content_type(result[:content_type])
end

Expand Down
15 changes: 5 additions & 10 deletions src/webless/request_builder/form_handler.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Webless::RequestBuilder::FormHandler
alias FormType = Symbol | String | Int32 | Int64 | Float64 | Bool | File | Array(FormType) | Hash(String, FormType)

def self.handle(form : Hash(String, _) | NamedTuple) : NamedTuple(body: String, content_type: String)
new(cast(form).as(Hash(String, FormType))).handle
def self.handle(form : Hash(String, _) | NamedTuple, multipart : Bool) : NamedTuple(body: String, content_type: String)
new(cast(form).as(Hash(String, FormType)), multipart).handle
end

private def self.cast(raw : Array) : FormType
Expand All @@ -28,12 +28,13 @@ class Webless::RequestBuilder::FormHandler
end

@form : Hash(String, FormType)
@multipart : Bool

def initialize(@form)
def initialize(@form, @multipart)
end

def handle : NamedTuple(body: String, content_type: String)
if multipart?
if @multipart
io = IO::Memory.new
builder = HTTP::FormData::Builder.new(io)
@form.each { |k, v| apply_multipart(builder, k, v) }
Expand All @@ -46,12 +47,6 @@ class Webless::RequestBuilder::FormHandler
end
end

private def multipart? : Bool
@form.any? do |_, v|
v.is_a?(File) || (v.is_a?(Array) && v.any?(File))
end
end

private def apply_multipart(builder : HTTP::FormData::Builder, k : String, v : FormType)
if v.is_a?(File)
builder.file(k, v.as(IO), HTTP::FormData::FileMetadata.new(filename: File.basename(v.path)))
Expand Down

0 comments on commit bd69f8b

Please sign in to comment.