Class: Tus::Storage::Gridfs
- Inherits:
-
Object
- Object
- Tus::Storage::Gridfs
- Defined in:
- lib/tus/storage/gridfs.rb
Defined Under Namespace
Classes: Response
Instance Attribute Summary collapse
-
#bucket ⇒ Object
readonly
Returns the value of attribute bucket.
-
#chunk_size ⇒ Object
readonly
Returns the value of attribute chunk_size.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Instance Method Summary collapse
- #concatenate(uid, part_uids, info = {}) ⇒ Object
- #create_file(uid, info = {}) ⇒ Object
- #delete_file(uid, info = {}) ⇒ Object
- #expire_files(expiration_date) ⇒ Object
- #get_file(uid, info = {}, range: nil) ⇒ Object
-
#initialize(client:, prefix: "fs", chunk_size: nil) ⇒ Gridfs
constructor
A new instance of Gridfs.
- #patch_file(uid, io, info = {}) ⇒ Object
- #read_info(uid) ⇒ Object
- #update_info(uid, info) ⇒ Object
Constructor Details
#initialize(client:, prefix: "fs", chunk_size: nil) ⇒ Gridfs
Returns a new instance of Gridfs.
13 14 15 16 17 18 19 |
# File 'lib/tus/storage/gridfs.rb', line 13 def initialize(client:, prefix: "fs", chunk_size: nil) @client = client @prefix = prefix @bucket = @client.database.fs(bucket_name: @prefix) @bucket.send(:ensure_indexes!) @chunk_size = chunk_size end |
Instance Attribute Details
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket.
11 12 13 |
# File 'lib/tus/storage/gridfs.rb', line 11 def bucket @bucket end |
#chunk_size ⇒ Object (readonly)
Returns the value of attribute chunk_size.
11 12 13 |
# File 'lib/tus/storage/gridfs.rb', line 11 def chunk_size @chunk_size end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
11 12 13 |
# File 'lib/tus/storage/gridfs.rb', line 11 def client @client end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
11 12 13 |
# File 'lib/tus/storage/gridfs.rb', line 11 def prefix @prefix end |
Instance Method Details
#concatenate(uid, part_uids, info = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/tus/storage/gridfs.rb', line 35 def concatenate(uid, part_uids, info = {}) file_infos = bucket.files_collection.find(filename: {"$in" => part_uids}).to_a file_infos.sort_by! { |file_info| part_uids.index(file_info[:filename]) } if file_infos.count != part_uids.count raise Tus::Error, "some parts for concatenation are missing" end chunk_sizes = file_infos.map { |file_info| file_info[:chunkSize] } if chunk_sizes[0..-2].uniq.count > 1 raise Tus::Error, "some parts have different chunk sizes, so they cannot be concatenated" end if chunk_sizes.uniq != [chunk_sizes.last] && bucket.chunks_collection.find(files_id: file_infos.last[:_id]).count > 1 raise Tus::Error, "last part has different chunk size and is composed of more than one chunk" end length = file_infos.inject(0) { |sum, file_info| sum + file_info[:length] } chunk_size = file_infos.first[:chunkSize] tus_info = Tus::Info.new(info) content_type = tus_info.["content_type"] file = Mongo::Grid::File.new("", filename: uid, metadata: {}, chunk_size: chunk_size, length: length, content_type: content_type, ) bucket.insert_one(file) file_infos.inject(0) do |offset, file_info| result = bucket.chunks_collection .find(files_id: file_info[:_id]) .update_many("$set" => {files_id: file.id}, "$inc" => {n: offset}) offset += result.modified_count end bucket.files_collection.delete_many(filename: {"$in" => part_uids}) # server requires us to return the size of the concatenated file length end |
#create_file(uid, info = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/tus/storage/gridfs.rb', line 21 def create_file(uid, info = {}) tus_info = Tus::Info.new(info) content_type = tus_info.["content_type"] file = Mongo::Grid::File.new("", filename: uid, metadata: {}, chunk_size: chunk_size, content_type: content_type, ) bucket.insert_one(file) end |
#delete_file(uid, info = {}) ⇒ Object
170 171 172 173 |
# File 'lib/tus/storage/gridfs.rb', line 170 def delete_file(uid, info = {}) file_info = bucket.files_collection.find(filename: uid).first bucket.delete(file_info.fetch("_id")) if file_info end |
#expire_files(expiration_date) ⇒ Object
175 176 177 178 179 180 181 |
# File 'lib/tus/storage/gridfs.rb', line 175 def expire_files(expiration_date) file_infos = bucket.files_collection.find(uploadDate: {"$lte" => expiration_date}).to_a file_info_ids = file_infos.map { |info| info[:_id] } bucket.files_collection.delete_many(_id: {"$in" => file_info_ids}) bucket.chunks_collection.delete_many(files_id: {"$in" => file_info_ids}) end |
#get_file(uid, info = {}, range: nil) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/tus/storage/gridfs.rb', line 123 def get_file(uid, info = {}, range: nil) file_info = bucket.files_collection.find(filename: uid).first raise Tus::NotFound if file_info.nil? filter = {files_id: file_info[:_id]} if range chunk_start = range.begin / file_info[:chunkSize] if range.begin chunk_stop = range.end / file_info[:chunkSize] if range.end filter[:n] = {} filter[:n].update("$gte" => chunk_start) if chunk_start filter[:n].update("$lte" => chunk_stop) if chunk_stop end chunks_view = bucket.chunks_collection.find(filter).read(bucket.read_preference).sort(n: 1) chunks = Enumerator.new do |yielder| chunks_view.each do |document| data = document[:data].data if document[:n] == chunk_start && document[:n] == chunk_stop byte_start = range.begin % file_info[:chunkSize] byte_stop = range.end % file_info[:chunkSize] elsif document[:n] == chunk_start byte_start = range.begin % file_info[:chunkSize] byte_stop = file_info[:chunkSize] - 1 elsif document[:n] == chunk_stop byte_start = 0 byte_stop = range.end % file_info[:chunkSize] end if byte_start && byte_stop partial_data = data[byte_start..byte_stop] yielder << partial_data partial_data.clear # deallocate chunk string else yielder << data end data.clear # deallocate chunk string end end Response.new(chunks: chunks, close: ->{chunks_view.close_query}) end |
#patch_file(uid, io, info = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/tus/storage/gridfs.rb', line 81 def patch_file(uid, io, info = {}) file_info = bucket.files_collection.find(filename: uid).first raise Tus::NotFound if file_info.nil? file_info[:md5] = Digest::MD5.new # hack for `Chunk.split` updating MD5 file_info[:chunkSize] ||= io.size file_info = Mongo::Grid::File::Info.new(Mongo::Options::Mapper.transform(file_info, Mongo::Grid::File::Info::MAPPINGS.invert)) tus_info = Tus::Info.new(info) last_chunk = (tus_info.length && io.size == tus_info.remaining_length) if io.size % file_info.chunk_size != 0 && !last_chunk raise Tus::Error, "Input has length #{io.size} but expected it to be a multiple of " \ "chunk size #{file_info.chunk_size} or for it to be the last chunk" end offset = bucket.chunks_collection.find(files_id: file_info.id).count chunks = Mongo::Grid::File::Chunk.split(io, file_info, offset) bucket.chunks_collection.insert_many(chunks) chunks.each { |chunk| chunk.data.data.clear } # deallocate strings bucket.files_collection.find(filename: uid).update_one("$set" => { length: file_info.length + io.size, uploadDate: Time.now.utc, chunkSize: file_info.chunk_size, }) end |
#read_info(uid) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/tus/storage/gridfs.rb', line 111 def read_info(uid) file_info = bucket.files_collection.find(filename: uid).first raise Tus::NotFound if file_info.nil? file_info.fetch("metadata") end |
#update_info(uid, info) ⇒ Object
118 119 120 121 |
# File 'lib/tus/storage/gridfs.rb', line 118 def update_info(uid, info) bucket.files_collection.find(filename: uid) .update_one("$set" => {metadata: info}) end |