Module: Protocol::Multipart::Readable
- Included in:
- FormData::Upload, Parser::Part
- Defined in:
- lib/protocol/multipart/readable.rb
Overview
Common operations for streaming readable multipart content.
Instance Method Summary collapse
-
#copy_to(output, chunk_size = 8192) ⇒ Object
Copy the content to a writable object.
-
#save(path, permissions: 0o600, chunk_size: 8192) ⇒ Object
Save the content to a new local file.
Instance Method Details
#copy_to(output, chunk_size = 8192) ⇒ Object
Copy the content to a writable object.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/protocol/multipart/readable.rb', line 14 def copy_to(output, chunk_size = 8192) size = 0 each(chunk_size) do |chunk| offset = 0 # A writable object may consume only part of a chunk: while offset < chunk.bytesize written = output.write(chunk.byteslice(offset, chunk.bytesize - offset)) unless written && written > 0 raise IOError, "Could not make progress while copying multipart content!" end offset += written end size += chunk.bytesize end return size end |
#save(path, permissions: 0o600, chunk_size: 8192) ⇒ Object
Save the content to a new local file.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/protocol/multipart/readable.rb', line 42 def save(path, permissions: 0o600, chunk_size: 8192) flags = File::WRONLY | File::CREAT | File::EXCL created = false begin File.open(path, flags, ) do |file| created = true file.binmode return copy_to(file, chunk_size) end rescue # Remove a partial destination created by this operation: if created File.unlink(path) end raise end end |