lib/mongo/grid/stream/write.rb in mongo-2.10.4 vs lib/mongo/grid/stream/write.rb in mongo-2.10.5
- old
+ new
@@ -80,23 +80,29 @@
@options.freeze
@filename = @options[:filename]
@open = true
end
- # Write to the GridFS bucket from the source stream.
+ # Write to the GridFS bucket from the source stream or a string.
#
# @example Write to GridFS.
# stream.write(io)
#
- # @param [ IO ] io The source io stream to upload from.
+ # @param [ String | IO ] io The string or IO object to upload from.
#
# @return [ Stream::Write ] self The write stream itself.
#
# @since 2.1.0
def write(io)
ensure_open!
@indexes ||= ensure_indexes!
- @length += io.size
+ @length += if io.respond_to?(:bytesize)
+ # String objects
+ io.bytesize
+ else
+ # IO objects
+ io.size
+ end
chunks = File::Chunk.split(io, file_info, @n)
@n += chunks.size
chunks_collection.insert_many(chunks) unless chunks.empty?
self
end