diff --git a/lib/mongo/gridfs/grid_io.rb b/lib/mongo/gridfs/grid_io.rb index e79886e..ce3dcab 100644 --- a/lib/mongo/gridfs/grid_io.rb +++ b/lib/mongo/gridfs/grid_io.rb @@ -78,6 +78,7 @@ module Mongo # @return [String] # the data in the file def read(length=nil) + return '' if @file_length.zero? if length == 0 return '' elsif length.nil? && @file_position.zero? @@ -165,6 +166,9 @@ module Mongo # @return [True] def close if @mode[0] == ?w + if @current_chunk['n'].zero? && @chunk_position.zero? + warn "Warning: Storing a file with zero length." + end @upload_date = Time.now.utc @files.insert(to_mongo_object) end diff --git a/test/grid_test.rb b/test/grid_test.rb index fa25d15..4dce634 100644 --- a/test/grid_test.rb +++ b/test/grid_test.rb @@ -45,6 +45,18 @@ class GridTest < Test::Unit::TestCase end end + context "Storing data with a length of zero" do + setup do + @grid = Grid.new(@db, 'test-fs') + @id = @grid.put('', 'sample', :metadata => {'app' => 'photos'}) + end + + should "return the zero length" do + data = @grid.get(@id) + assert_equal 0, data.read.length + end + end + context "Streaming: " do || {} setup do def read_and_write_stream(filename, read_length, opts={})