diff --git a/lib/mongo/gridfs/grid_io.rb b/lib/mongo/gridfs/grid_io.rb index 5e86507..89af77d 100644 --- a/lib/mongo/gridfs/grid_io.rb +++ b/lib/mongo/gridfs/grid_io.rb @@ -316,21 +316,18 @@ module Mongo chunk end - def last_chunk_number - (@file_length / @chunk_size).to_i - end - # Read a file in its entirety. def read_all buf = '' if @current_chunk buf << @current_chunk['data'].to_s - while chunk = get_chunk(@current_chunk['n'] + 1) - buf << chunk['data'].to_s - @current_chunk = chunk + while buf.size < @file_length + @current_chunk = get_chunk(@current_chunk['n'] + 1) + break if @current_chunk.nil? + buf << @current_chunk['data'].to_s end + @file_position = @file_length end - @file_position = @file_length buf end diff --git a/test/grid_test.rb b/test/grid_test.rb index 2748071..f4b972c 100644 --- a/test/grid_test.rb +++ b/test/grid_test.rb @@ -30,6 +30,21 @@ class GridTest < Test::Unit::TestCase @chunks.remove end + context "A one-chunk grid-stored file" do + setup do + @data = "GRIDDATA" * 5 + @grid = Grid.new(@db, 'test-fs') + @id = @grid.put(@data, :filename => 'sample', + :metadata => {'app' => 'photos'}) + end + + should "retrieve the file" do + data = @grid.get(@id).data + assert_equal @data, data + end + + end + context "A basic grid-stored file" do setup do @data = "GRIDDATA" * 50000 @@ -55,7 +70,7 @@ class GridTest < Test::Unit::TestCase should "retrieve the stored data" do data = @grid.get(@id).data - assert_equal @data, data + assert_equal @data.length, data.length end should "have a unique index on chunks" do @@ -161,6 +176,11 @@ class GridTest < Test::Unit::TestCase end end + should "be equal in length" do + @io.rewind + assert_equal @io.read.length, @file.read.length + end + should "read the file" do read_data = "" @file.each do |chunk|