class Mongo::Grid::FSBucket::Stream::Write
A stream that writes files to the FSBucket
.
@since 2.1.0
Attributes
@return [ Object ] file_id
The id of the file being uploaded.
@since 2.1.0
@return [ String ] filename The name of the file being uploaded.
@since 2.1.0
@return [ FSBucket
] fs The fs bucket to which this stream writes.
@since 2.1.0
@return [ Hash ] options The write stream options.
@since 2.1.0
Public Class Methods
Create a stream for writing files to the FSBucket
.
@example Create the stream.
Stream::Write.new(fs, options)
@param [ FSBucket
] fs The GridFS bucket object. @param [ Hash ] options The write stream options.
@option options [ Object ] :file_id The file id. An ObjectId
is generated if the file id is not provided.
@option opts [ Integer ] :chunk_size Override the default chunk size. @option opts [ Hash ] :metadata User data for the 'metadata' field of the files collection document. @option opts [ String ] :content_type The content type of the file.
Deprecated, please use the metadata document instead.
@option opts [ Array<String> ] :aliases A list of aliases.
Deprecated, please use the metadata document instead.
@option options [ Hash ] :write Deprecated. Equivalent to :write_concern
option.
@option options [ Hash ] :write_concern The write concern options.
Can be :w => Integer|String, :fsync => Boolean, :j => Boolean.
@since 2.1.0
# File lib/mongo/grid/stream/write.rb, line 67 def initialize(fs, options) @fs = fs @length = 0 @n = 0 @file_id = options[:file_id] || BSON::ObjectId.new @options = options.dup =begin WriteConcern object support if @options[:write_concern].is_a?(WriteConcern::Base) # Cache the instance so that we do not needlessly reconstruct it. @write_concern = @options[:write_concern] @options[:write_concern] = @write_concern.options end =end @options.freeze @filename = @options[:filename] @open = true end
Public Instance Methods
Abort the upload by deleting all chunks already inserted.
@example Abort the write operation.
stream.abort
@return [ true ] True if the operation was aborted and the stream is closed.
@since 2.1.0
# File lib/mongo/grid/stream/write.rb, line 159 def abort fs.chunks_collection.find({ :files_id => file_id }, @options).delete_many (@open = false) || true end
Close the write stream.
@example Close the stream.
stream.close
@return [ BSON::ObjectId, Object ] The file id.
@raise [ Error::ClosedStream
] If the stream is already closed.
@since 2.1.0
# File lib/mongo/grid/stream/write.rb, line 115 def close ensure_open! update_length files_collection.insert_one(file_info, @options) @open = false file_id end
Is the stream closed.
@example Is the stream closed.
stream.closed?
@return [ true, false ] Whether the stream is closed.
@since 2.1.0
# File lib/mongo/grid/stream/write.rb, line 147 def closed? !@open end
Write
to the GridFS bucket from the source stream.
@example Write
to GridFS.
stream.write(io)
@param [ IO ] io The source io stream to upload from.
@return [ Stream::Write
] self The write stream itself.
@since 2.1.0
# File lib/mongo/grid/stream/write.rb, line 95 def write(io) ensure_open! @indexes ||= ensure_indexes! @length += io.size chunks = File::Chunk.split(io, file_info, @n) @n += chunks.size chunks_collection.insert_many(chunks) unless chunks.empty? self end
Get the write concern used when uploading.
@example Get the write concern.
stream.write_concern
@return [ Mongo::WriteConcern
] The write concern.
@since 2.1.0
# File lib/mongo/grid/stream/write.rb, line 131 def write_concern @write_concern ||= if wco = @options[:write_concern] || @options[:write] WriteConcern.get(wco) else fs.write_concern end end
Private Instance Methods
# File lib/mongo/grid/stream/write.rb, line 166 def chunks_collection with_write_concern(fs.chunks_collection) end
# File lib/mongo/grid/stream/write.rb, line 193 def ensure_indexes! fs.send(:ensure_indexes!) end
# File lib/mongo/grid/stream/write.rb, line 197 def ensure_open! raise Error::ClosedStream.new if closed? end
# File lib/mongo/grid/stream/write.rb, line 188 def file_info doc = { length: @length, _id: file_id, filename: filename } @file_info ||= File::Info.new(options.merge(doc)) end
# File lib/mongo/grid/stream/write.rb, line 170 def files_collection with_write_concern(fs.files_collection) end
# File lib/mongo/grid/stream/write.rb, line 184 def update_length file_info.document[:length] = @length end
# File lib/mongo/grid/stream/write.rb, line 174 def with_write_concern(collection) if write_concern.nil? || (collection.write_concern && collection.write_concern.options == write_concern.options) then collection else collection.with(write: write_concern.options) end end