From 006df109a7d3891974bf83c689eb81b84311404c Mon Sep 17 00:00:00 2001 From: 404 Date: Sat, 28 Jul 2012 15:16:26 +0800 Subject: [PATCH] fixed Qiniu::RS.put_file() --- Gemfile.lock | 4 ++-- lib/qiniu/rs/auth.rb | 8 ++++---- lib/qiniu/rs/io.rb | 5 ++--- lib/qiniu/rs/utils.rb | 39 ++------------------------------------- lib/qiniu/rs/version.rb | 2 +- 5 files changed, 11 insertions(+), 47 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index b92a415..90998b9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - qiniu-rs (2.3.0) + qiniu-rs (2.3.1) json (~> 1.7.3) mime-types (~> 1.19) rest-client (~> 1.6.7) @@ -12,7 +12,7 @@ GEM specs: diff-lcs (1.1.3) fakeweb (1.3.0) - json (1.7.3) + json (1.7.4) mime-types (1.19) rake (0.9.2.2) rest-client (1.6.7) diff --git a/lib/qiniu/rs/auth.rb b/lib/qiniu/rs/auth.rb index 99400f0..f254013 100755 --- a/lib/qiniu/rs/auth.rb +++ b/lib/qiniu/rs/auth.rb @@ -58,17 +58,17 @@ module Qiniu [code, data] end - def call_with_signature(url, data, retry_times = 0) - code, data = http_request url, data, {:qbox_signature_token => generate_qbox_signature(url, data)} + def call_with_signature(url, data, retry_times = 0, options = {}) + code, data = http_request url, data, options.merge!({:qbox_signature_token => generate_qbox_signature(url, data)}) [code, data] end - def request(url, data = nil) + def request(url, data = nil, options = {}) begin if Config.settings[:access_key].empty? || Config.settings[:secret_key].empty? code, data = Auth.call_with_logged_in(url, data) else - code, data = Auth.call_with_signature(url, data) + code, data = Auth.call_with_signature(url, data, options) end rescue [MissingAccessToken, MissingRefreshToken, MissingUsernameOrPassword] => e Log.logger.error e diff --git a/lib/qiniu/rs/io.rb b/lib/qiniu/rs/io.rb index 54252ec..8eb5646 100755 --- a/lib/qiniu/rs/io.rb +++ b/lib/qiniu/rs/io.rb @@ -30,9 +30,8 @@ module Qiniu def put_file(local_file, bucket, key = nil, mime_type = nil, custom_meta = nil, enable_crc32_check = false) action_params = _generate_action_params(local_file, bucket, key, mime_type, custom_meta, enable_crc32_check) url = Config.settings[:io_host] + action_params - post_data = {:file => File.new(local_file, 'rb'), :multipart => true} - options = {:qbox_signature_token => Utils.generate_qbox_signature(url, nil)} - Utils.send_multipart_request url, post_data, options + options = {:content_type => 'application/octet-stream'} + Auth.request url, ::IO.read(local_file), options end private diff --git a/lib/qiniu/rs/utils.rb b/lib/qiniu/rs/utils.rb index 700e1bb..b5d4741 100755 --- a/lib/qiniu/rs/utils.rb +++ b/lib/qiniu/rs/utils.rb @@ -91,49 +91,14 @@ module Qiniu end end - def send_multipart_request(url, post_data, options = {}) - code, data = 0, {} - begin - header_options = { - :accept => :json, - :user_agent => Config.settings[:user_agent] - } - auth_token = nil - if !options[:qbox_signature_token].nil? && !options[:qbox_signature_token].empty? - auth_token = 'QBox ' + options[:qbox_signature_token] - #elsif !options[:upload_signature_token].nil? && !options[:upload_signature_token].empty? - #auth_token = 'UpToken ' + options[:upload_signature_token] - elsif options[:access_token] - auth_token = 'Bearer ' + options[:access_token] - end - header_options.merge!('Authorization' => auth_token) unless auth_token.nil? - response = RestClient.post url, post_data, header_options - body = response.respond_to?(:body) ? response.body : "" - data = safe_json_parse(body) unless body.empty? - code = response.code.to_i if response.respond_to?(:code) - rescue Errno::ECONNRESET => err - Log.logger.error err - rescue => e - Log.logger.warn "#{e.message} => Utils.http_request('#{url}')" - res = e.response - if e.respond_to? :response - res = e.response - code = res.code.to_i if res.respond_to? :code - body = res.respond_to?(:body) ? res.body : "" - data = safe_json_parse(body) unless body.empty? - end - end - [code, data] - end - def upload_multipart_data(url, filepath, action_string, callback_query_string = '') post_data = { - :file => File.new(filepath, 'rb'), :params => callback_query_string, :action => action_string, + :file => File.new(filepath, 'rb'), :multipart => true } - send_multipart_request(url, post_data) + http_request url, post_data end def generate_query_string(params) diff --git a/lib/qiniu/rs/version.rb b/lib/qiniu/rs/version.rb index 43cf2ae..162a3bc 100755 --- a/lib/qiniu/rs/version.rb +++ b/lib/qiniu/rs/version.rb @@ -2,6 +2,6 @@ module Qiniu module RS - VERSION = "2.3.0" + VERSION = "2.3.1" end end