about summary refs log tree commit homepage
path: root/lib/mogilefs/mogilefs.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/mogilefs/mogilefs.rb')
-rw-r--r--lib/mogilefs/mogilefs.rb42
1 files changed, 24 insertions, 18 deletions
diff --git a/lib/mogilefs/mogilefs.rb b/lib/mogilefs/mogilefs.rb
index 40ee1ca..6059f5f 100644
--- a/lib/mogilefs/mogilefs.rb
+++ b/lib/mogilefs/mogilefs.rb
@@ -129,16 +129,21 @@ class MogileFS::MogileFS < MogileFS::Client
   # Consider using store_file instead of this method for large files.
   # This requires a block passed to it and operates like File.open.
   # This atomically replaces existing data stored as +key+ when
-  def new_file(key, klass = nil, bytes = 0) # :yields: file
+  def new_file(key, args = nil, bytes = 0) # :yields: file
     raise MogileFS::ReadOnlyError if readonly?
-    opts = { :domain => @domain, :key => key, :multi_dest => 1 }
-    opts[:class] = klass if klass && klass != "default"
+    opts = { :key => key, :multi_dest => 1 }
+    case args
+    when Hash
+      opts[:domain] = args[:domain]
+      klass = args[:class] and "default" != klass and opts[:class] = klass
+    when String
+      opts[:class] = args if "default" != args
+    end
+    opts[:domain] ||= @domain
     res = @backend.create_open(opts)
 
     dests = if dev_count = res['dev_count'] # multi_dest succeeded
-      (1..dev_count.to_i).map do |i|
-        [res["devid_#{i}"], res["path_#{i}"]]
-      end
+      (1..dev_count.to_i).map { |i| [res["devid_#{i}"], res["path_#{i}"]] }
     else # single destination returned
       # 0x0040:  d0e4 4f4b 2064 6576 6964 3d31 2666 6964  ..OK.devid=1&fid
       # 0x0050:  3d33 2670 6174 683d 6874 7470 3a2f 2f31  =3&path=http://1
@@ -149,19 +154,20 @@ class MogileFS::MogileFS < MogileFS::Client
       [[res['devid'], res['path']]]
     end
 
+    opts.merge!(args) if Hash === args
+    opts[:backend] = @backend
+    opts[:fid] = res['fid']
+
     case (dests[0][1] rescue nil)
-    when /^http:\/\// then
-      http_file = MogileFS::HTTPFile.new(dests, bytes)
-      yield http_file
-      rv = http_file.commit
-      @backend.create_close(:fid => res['fid'],
-                            :devid => http_file.devid,
-                            :domain => @domain,
-                            :key => key,
-                            :path => http_file.uri.to_s,
-                            :size => rv)
-      rv
-    when nil, '' then
+    when %r{\Ahttp://}
+      http_file = MogileFS::HTTPFile.new(dests, opts)
+      if block_given?
+        yield http_file
+        return http_file.commit # calls create_close
+      else
+        return http_file
+      end
+    when nil, ''
       raise MogileFS::EmptyPathError,
             "Empty path for mogile upload res=#{res.inspect}"
     else