Class: Mongo::Grid::FSBucket::Stream::Write
- Inherits:
-
Object
- Object
- Mongo::Grid::FSBucket::Stream::Write
- Defined in:
- build/ruby-driver-v2.19/lib/mongo/grid/stream/write.rb
Overview
A stream that writes files to the FSBucket.
Instance Attribute Summary collapse
-
#file_id ⇒ Object
readonly
File_id The id of the file being uploaded.
-
#filename ⇒ String
readonly
Filename The name of the file being uploaded.
-
#fs ⇒ FSBucket
readonly
Fs The fs bucket to which this stream writes.
-
#options ⇒ Hash
readonly
Options The write stream options.
Instance Method Summary collapse
-
#abort ⇒ true
Abort the upload by deleting all chunks already inserted.
-
#close ⇒ BSON::ObjectId, Object
Close the write stream.
-
#closed? ⇒ true, false
Is the stream closed.
-
#initialize(fs, options) ⇒ Write
constructor
Create a stream for writing files to the FSBucket.
-
#write(io) ⇒ Stream::Write
Write to the GridFS bucket from the source stream or a string.
-
#write_concern ⇒ Mongo::WriteConcern
Get the write concern used when uploading.
Constructor Details
#initialize(fs, options) ⇒ Write
Create a stream for writing files to the FSBucket.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'build/ruby-driver-v2.19/lib/mongo/grid/stream/write.rb', line 70 def initialize(fs, ) @fs = fs @length = 0 @n = 0 @file_id = [:file_id] || BSON::ObjectId.new @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 |
Instance Attribute Details
#file_id ⇒ Object (readonly)
Returns file_id The id of the file being uploaded.
36 37 38 |
# File 'build/ruby-driver-v2.19/lib/mongo/grid/stream/write.rb', line 36 def file_id @file_id end |
#filename ⇒ String (readonly)
Returns filename The name of the file being uploaded.
41 42 43 |
# File 'build/ruby-driver-v2.19/lib/mongo/grid/stream/write.rb', line 41 def filename @filename end |
#fs ⇒ FSBucket (readonly)
Returns fs The fs bucket to which this stream writes.
31 32 33 |
# File 'build/ruby-driver-v2.19/lib/mongo/grid/stream/write.rb', line 31 def fs @fs end |
#options ⇒ Hash (readonly)
Returns options The write stream options.
46 47 48 |
# File 'build/ruby-driver-v2.19/lib/mongo/grid/stream/write.rb', line 46 def @options end |
Instance Method Details
#abort ⇒ true
Abort the upload by deleting all chunks already inserted.
168 169 170 171 |
# File 'build/ruby-driver-v2.19/lib/mongo/grid/stream/write.rb', line 168 def abort fs.chunks_collection.find({ :files_id => file_id }, @options).delete_many (@open = false) || true end |
#close ⇒ BSON::ObjectId, Object
Close the write stream.
124 125 126 127 128 129 130 |
# File 'build/ruby-driver-v2.19/lib/mongo/grid/stream/write.rb', line 124 def close ensure_open! update_length files_collection.insert_one(file_info, @options) @open = false file_id end |
#closed? ⇒ true, false
Is the stream closed.
156 157 158 |
# File 'build/ruby-driver-v2.19/lib/mongo/grid/stream/write.rb', line 156 def closed? !@open end |
#write(io) ⇒ Stream::Write
Write to the GridFS bucket from the source stream or a string.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'build/ruby-driver-v2.19/lib/mongo/grid/stream/write.rb', line 98 def write(io) ensure_open! @indexes ||= ensure_indexes! @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 |
#write_concern ⇒ Mongo::WriteConcern
Get the write concern used when uploading.
140 141 142 143 144 145 146 |
# File 'build/ruby-driver-v2.19/lib/mongo/grid/stream/write.rb', line 140 def write_concern @write_concern ||= if wco = @options[:write_concern] || @options[:write] WriteConcern.get(wco) else fs.write_concern end end |