From 02523739540a35f089774449602640f8e86fb323 Mon Sep 17 00:00:00 2001 From: ahoward Date: Mon, 23 Jul 2012 22:38:03 -0600 Subject: [PATCH] in which some lame-ass libraries read(with_no_size) --- lib/mongoid-grid_fs.rb | 25 +++++++++++++++++++++++-- mongoid-grid_fs.gemspec | 2 +- 2 files changed, 24 insertions(+), 3 deletions(-) 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"