mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Yield UploadFile in parse_multipart (#224)
This commit is contained in:
parent
450dab9f81
commit
3342007701
1 changed files with 20 additions and 7 deletions
|
@ -79,19 +79,32 @@ 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.
|
||||
# nodoc:
|
||||
struct UploadFile
|
||||
getter field : String
|
||||
getter data : IO::Delimited
|
||||
getter meta : HTTP::FormData::FileMetadata
|
||||
getter headers : HTTP::Headers
|
||||
|
||||
def initialize(@field, @data, @meta, @headers)
|
||||
end
|
||||
end
|
||||
|
||||
# Parses a multipart/form-data request. Yields an `UploadFile` object with `field`, `data`, `meta`, `headers` fields.
|
||||
# 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.
|
||||
# for each file you can use simple `if/switch` conditionals.
|
||||
#
|
||||
# post "/upload" do |env|
|
||||
# parse_multipart(env) do |field, data|
|
||||
# image1 = data if field == "image1"
|
||||
# image2 = data if field == "image2"
|
||||
# parse_multipart(env) do |f|
|
||||
# image1 = f.data if f.field == "image1"
|
||||
# image2 = f.data if f.field == "image2"
|
||||
# puts f.meta
|
||||
# puts f.headers
|
||||
# "Upload complete"
|
||||
# end
|
||||
# end
|
||||
def parse_multipart(env)
|
||||
HTTP::FormData.parse(env.request) do |field, data|
|
||||
yield field, data
|
||||
HTTP::FormData.parse(env.request) do |field, data, meta, headers|
|
||||
yield UploadFile.new field, data, meta, headers
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue