about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-12-06 20:54:07 +0000
committerEric Wong <normalperson@yhbt.net>2011-12-06 13:09:02 -0800
commit296977523d04e184b954f5a00e9463dea0d8e8d6 (patch)
treea894a61d678201d14dee621044ae3efb647b87fc
parent2c4dc76c6a4840865c83177ceeb217b859e20df8 (diff)
downloadmogilefs-client-296977523d04e184b954f5a00e9463dea0d8e8d6.tar.gz
These allow us to specify Content-MD5 and checksums for use
with the new and improved new_file interface without breaking
existing apps.
-rw-r--r--lib/mogilefs/mogilefs.rb10
-rw-r--r--test/test_mogilefs_integration.rb25
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