From 3e7105cda67649d7c5011b38c3f20512d9e0c2e1 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 30 Nov 2011 21:26:00 +0000 Subject: [PATCH] store_file/store_content: wire these up to new new_file opts These allow us to specify Content-MD5 and checksums for use with the new and improved new_file interface without breaking existing apps. --- lib/mogilefs/mogilefs.rb | 10 ++++++---- test/test_mogilefs_integration.rb | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/mogilefs/mogilefs.rb b/lib/mogilefs/mogilefs.rb index 22dd39c..372eae0 100644 --- a/lib/mogilefs/mogilefs.rb +++ b/lib/mogilefs/mogilefs.rb @@ -181,18 +181,20 @@ class MogileFS::MogileFS < MogileFS::Client # either a path name (String or Pathname object) or an IO-like object that # responds to #read or #readpartial. Returns size of +file+ stored. # This atomically replaces existing data stored as +key+ - def store_file(key, klass, file) + def store_file(key, klass, file, opts = nil) raise MogileFS::ReadOnlyError if readonly? + (opts ||= {})[:class] = klass if String === klass - new_file(key, klass) { |mfp| mfp.big_io = file } + new_file(key, opts) { |mfp| mfp.big_io = file } end # Stores +content+ into +key+ in class +klass+, where +content+ is a String # This atomically replaces existing data stored as +key+ - def store_content(key, klass, content) + def store_content(key, klass, content, opts = nil) raise MogileFS::ReadOnlyError if readonly? + (opts ||= {})[:class] = klass if String === klass - new_file key, klass do |mfp| + new_file(key, opts) do |mfp| if content.is_a?(MogileFS::Util::StoreContent) mfp.streaming_io = content else diff --git a/test/test_mogilefs_integration.rb b/test/test_mogilefs_integration.rb index 57ea675..947bb34 100644 --- a/test/test_mogilefs_integration.rb +++ b/test/test_mogilefs_integration.rb @@ -230,4 +230,29 @@ class TestMogileFSIntegration < TestMogIntegration ensure r.close if r end + + def test_store_content_opts + b64digest = [ Digest::MD5.digest("HELLO") ].pack('m').strip + assert_nothing_raised do + @client.store_content("c", nil, "HELLO", :content_md5 => b64digest) + end + assert_raises(MogileFS::SizeMismatchError) do + @client.store_content("c", nil, "GOODBYE", :content_length => 2) + end + assert_equal "HELLO", @client.get_file_data("c") + end + + def test_store_file_opts + b64digest = [ Digest::MD5.digest("HELLO") ].pack('m').strip + io = StringIO.new("HELLO") + assert_nothing_raised do + @client.store_file("c", nil, io, :content_md5 => b64digest) + end + + io = StringIO.new("GOODBYE") + assert_raises(MogileFS::SizeMismatchError) do + @client.store_content("c", nil, io, :content_length => 2) + end + assert_equal "HELLO", @client.get_file_data("c") + end end -- 2.11.4.GIT