diff --git a/lib/mongoid-grid_fs.rb b/lib/mongoid-grid_fs.rb index e12e475..fe974ce 100644 --- a/lib/mongoid-grid_fs.rb +++ b/lib/mongoid-grid_fs.rb @@ -1,7 +1,7 @@ ## # class GridFS - const_set :Version, '1.1.2' + const_set :Version, '1.2.0' class << GridFS def version @@ -185,7 +185,7 @@ attributes[:contentType] ||= GridFS.extract_content_type(filename) || file.contentType - while((buf = io.read(chunkSize))) + GridFS.chunking(io, chunkSize) do |buf| md5 << buf length += buf.size chunk = file.chunks.build @@ -422,6 +422,27 @@ end end + def GridFS.chunking(io, chunk_size, &block) + if io.method(:read).arity == 0 + data = io.read + i = 0 + loop do + offset = i * chunk_size + length = i + chunk_size < data.size ? chunk_size : data.size - offset + + break if offset >= data.size + + buf = data[offset, length] + block.call(buf) + i += 1 + end + else + while((buf = io.read(chunk_size))) + block.call(buf) + end + end + end + def GridFS.rewind(io, &block) begin pos = io.pos diff --git a/mongoid-grid_fs.gemspec b/mongoid-grid_fs.gemspec index e389c8f..d7c7915 100644 --- a/mongoid-grid_fs.gemspec +++ b/mongoid-grid_fs.gemspec @@ -3,7 +3,7 @@ Gem::Specification::new do |spec| spec.name = "mongoid-grid_fs" - spec.version = "1.1.2" + spec.version = "1.2.0" spec.platform = Gem::Platform::RUBY spec.summary = "mongoid-grid_fs" spec.description = "a mongoid 3/moped compatible implementation of the grid_fs specification"