diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mogilefs/copy_stream.rb | 12 | ||||
-rw-r--r-- | lib/mogilefs/mogilefs.rb | 16 |
2 files changed, 20 insertions, 8 deletions
diff --git a/lib/mogilefs/copy_stream.rb b/lib/mogilefs/copy_stream.rb index b928031..7531f54 100644 --- a/lib/mogilefs/copy_stream.rb +++ b/lib/mogilefs/copy_stream.rb @@ -3,24 +3,26 @@ # internal compatibility class for older Rubies module MogileFS::CopyStream # :nodoc: def self.copy_stream(src, dst) - src_io = String === src ? File.open(src) : src + src_io = src.respond_to?(:to_str) ? File.open(src) : src + dst_io = dst.respond_to?(:to_str) ? File.open(dst, "w") : dst buf = "" written = 0 if src_io.respond_to?(:readpartial) begin src_io.readpartial(0x4000, buf) - written += dst.write(buf) + written += dst_io.write(buf) rescue EOFError break end while true else while src_io.read(0x4000, buf) - written += dst.write(buf) + written += dst_io.write(buf) end end - dst.flush if dst.respond_to?(:flush) + dst_io.flush if dst_io.respond_to?(:flush) written ensure - src_io.close if String === src + src_io.close if src_io != src + dst_io.close if dst_io != dst end end diff --git a/lib/mogilefs/mogilefs.rb b/lib/mogilefs/mogilefs.rb index 2b02fb7..625e26f 100644 --- a/lib/mogilefs/mogilefs.rb +++ b/lib/mogilefs/mogilefs.rb @@ -52,12 +52,22 @@ class MogileFS::MogileFS < MogileFS::Client end ## - # Retrieves the contents of +key+. + # Retrieves the contents of +key+. If +dest+ is specified, +dest+ + # should be an IO-like object capable of receiving the +write+ method + # or a path name. - def get_file_data(key) + def get_file_data(key, dest = nil) paths = get_paths(key) sock = MogileFS::HTTPReader.first(paths, "GET", @get_file_data_timeout) - block_given? ? yield(sock) : sock.to_s + if dest + MogileFS::X.copy_stream(sock, dest) + elsif block_given? + yield(sock) + else + sock.to_s + end + ensure + sock.close unless sock.closed? end ## |