From 16c8170498dfe0995ffbca5a43fdeadc5c3c6629 Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Wed, 18 Feb 2009 14:11:22 -0500 Subject: [PATCH] md5 for ruby gridfs too --- lib/mongo/gridfs/grid_store.rb | 11 +++++++++-- tests/test_grid_store.rb | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/mongo/gridfs/grid_store.rb b/lib/mongo/gridfs/grid_store.rb index d7e80be..467b282 100644 --- a/lib/mongo/gridfs/grid_store.rb +++ b/lib/mongo/gridfs/grid_store.rb @@ -67,6 +67,8 @@ module XGen attr_accessor :lineno + attr_reader :md5 + class << self def exist?(db, name, root_collection=DEFAULT_ROOT_COLLECTION) @@ -142,6 +144,7 @@ module XGen @aliases = doc['aliases'] @length = doc['length'] @metadata = doc['metadata'] + @md5 = doc['md5'] else @files_id = XGen::Mongo::Driver::ObjectID.new @content_type = DEFAULT_CONTENT_TYPE @@ -370,7 +373,7 @@ module XGen when IO::SEEK_SET pos end - + new_chunk_number = (target_pos / @chunk_size).to_i if new_chunk_number != @curr_chunk.chunk_number @curr_chunk.save if @mode[0] == ?w @@ -405,7 +408,7 @@ module XGen end @db = nil end - + def closed? @db == nil end @@ -426,6 +429,10 @@ module XGen h['uploadDate'] = @upload_date h['aliases'] = @aliases h['metadata'] = @metadata + md5_command = OrderedHash.new + md5_command['filemd5'] = @files_id + md5_command['root'] = @root + h['md5'] = @db.db_command(md5_command)['md5'] h end diff --git a/tests/test_grid_store.rb b/tests/test_grid_store.rb index 19550c8..21f6b69 100644 --- a/tests/test_grid_store.rb +++ b/tests/test_grid_store.rb @@ -208,6 +208,23 @@ class GridStoreTest < Test::Unit::TestCase } end + def test_md5 + GridStore.open(@@db, 'new-file', 'w') { |f| f.write("hello world\n")} + GridStore.open(@@db, 'new-file', 'r') { |f| + assert f.md5 == '6f5902ac237024bdd0c176cb93063dc4' + begin + f.md5 = 'cant do this' + fail "should have seen error" + rescue => ex + true + end + } + GridStore.open(@@db, 'new-file', 'w') {} + GridStore.open(@@db, 'new-file', 'r') { |f| + assert f.md5 == 'd41d8cd98f00b204e9800998ecf8427e' + } + end + def test_upload_date now = Time.now orig_file_upload_date = nil