lib/upyun/rest.rb in upyun-1.0.3 vs lib/upyun/rest.rb in upyun-1.0.4

- old
+ new

@@ -15,17 +15,13 @@ @options = options @endpoint = endpoint end def put(path, file, headers={}) - raise ArgumentError, "'file' is not an instance of String" unless file.is_a?(String) headers = headers.merge({'mkdir' => true}) unless headers.key?('mkdir') - options = if File.file?(file) - {body: File.read(file), length: File.size(file), headers: headers} - else - {body: file, length: file.length, headers: headers} - end + body = file.respond_to?(:read) ? IO.binread(file) : file + options = {body: body, length: size(file), headers: headers} # If the type of current bucket is Picture, # put an image maybe return a set of headers # represent the image's metadata # x-upyun-width @@ -37,10 +33,12 @@ memo.merge!({k[8..-1].to_sym => /^\d+$/.match(v) ? v.to_i : v}) end end res == {} ? true : res + ensure + file.close if file.respond_to?(:close) end def get(path, savepath=nil) res = request(:get, path) return res if res.is_a?(Hash) || !savepath @@ -159,8 +157,16 @@ end def sign(method, date, path, length) sign = "#{method.to_s.upcase}&#{path}&#{date}&#{length}&#{@password}" "UpYun #{@operator}:#{md5(sign)}" + end + + def size(param) + if param.respond_to?(:size) + param.size + elsif param.is_a?(IO) + param.stat.size + end end end end