From 7df449cc736365770302d788b73d5d61c09fa7c0 Mon Sep 17 00:00:00 2001 From: 404 Date: Tue, 24 Jul 2012 16:54:09 +0800 Subject: [PATCH] added Qiniu::RS.put_file() --- lib/qiniu/rs.rb | 16 +++++++++++++--- lib/qiniu/rs/io.rb | 24 +++++++++++++++++++----- lib/qiniu/rs/utils.rb | 27 ++++++++++++++++++++------- lib/qiniu/rs/version.rb | 2 +- spec/qiniu/rs/io_spec.rb | 23 ++++++++++++++--------- spec/qiniu/rs/rs_spec.rb | 4 ++-- spec/qiniu/rs_spec.rb | 12 ++++++++++++ 7 files changed, 81 insertions(+), 27 deletions(-) diff --git a/lib/qiniu/rs.rb b/lib/qiniu/rs.rb index 08ccd60..0335d64 100755 --- a/lib/qiniu/rs.rb +++ b/lib/qiniu/rs.rb @@ -35,13 +35,23 @@ module Qiniu end def upload opts = {} - code, data = IO.put_file(opts[:url], - opts[:file], + code, data = IO.upload_file(opts[:url], + opts[:file], + opts[:bucket], + opts[:key], + opts[:mime_type], + opts[:note], + opts[:callback_params], + opts[:enable_crc32_check]) + code == StatusOK + end + + def put_file opts = {} + code, data = IO.put_file(opts[:file], opts[:bucket], opts[:key], opts[:mime_type], opts[:note], - opts[:callback_params], opts[:enable_crc32_check]) code == StatusOK end diff --git a/lib/qiniu/rs/io.rb b/lib/qiniu/rs/io.rb index fbc0499..54252ec 100755 --- a/lib/qiniu/rs/io.rb +++ b/lib/qiniu/rs/io.rb @@ -20,20 +20,34 @@ module Qiniu Auth.request(url) end - def put_file(url, local_file, bucket = nil, key = nil, mime_type = nil, custom_meta = nil, callback_params = nil, enable_crc32_check = false) + def upload_file(url, local_file, bucket, key = nil, mime_type = nil, custom_meta = nil, callback_params = nil, enable_crc32_check = false) + action_params = _generate_action_params(local_file, bucket, key, mime_type, custom_meta, enable_crc32_check) + callback_params = {:bucket => bucket, :key => key, :mime_type => mime_type} if callback_params.nil? + callback_query_string = Utils.generate_query_string(callback_params) + Utils.upload_multipart_data(url, local_file, action_params, callback_query_string) + end + + 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 + end + + private + def _generate_action_params(local_file, bucket, key = nil, mime_type = nil, custom_meta = nil, enable_crc32_check = false) raise NoSuchFileError, local_file unless File.exist?(local_file) key = Digest::SHA1.hexdigest(local_file + Time.now.to_s) if key.nil? entry_uri = bucket + ':' + key - if mime_type.nil? + if mime_type.nil? || mime_type.empty? mime = MIME::Types.type_for local_file mime_type = mime.empty? ? 'application/octet-stream' : mime[0].content_type end action_params = '/rs-put/' + Utils.urlsafe_base64_encode(entry_uri) + '/mimeType/' + Utils.urlsafe_base64_encode(mime_type) action_params += '/meta/' + Utils.urlsafe_base64_encode(custom_meta) unless custom_meta.nil? action_params += '/crc32/' + Utils.crc32checksum(local_file).to_s if enable_crc32_check - callback_params = {:bucket => bucket, :key => key, :mime_type => mime_type} if callback_params.nil? - callback_query_string = Utils.generate_query_string(callback_params) - Utils.upload_multipart_data(url, local_file, action_params, callback_query_string) + action_params end end diff --git a/lib/qiniu/rs/utils.rb b/lib/qiniu/rs/utils.rb index 272115b..700e1bb 100755 --- a/lib/qiniu/rs/utils.rb +++ b/lib/qiniu/rs/utils.rb @@ -91,19 +91,22 @@ module Qiniu end end - def upload_multipart_data(url, filepath, action_string, callback_query_string = '') + def send_multipart_request(url, post_data, options = {}) code, data = 0, {} begin header_options = { :accept => :json, :user_agent => Config.settings[:user_agent] } - post_data = { - :file => File.new(filepath, 'rb'), - :params => callback_query_string, - :action => action_string, - :multipart => true - } + 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? @@ -123,6 +126,16 @@ module Qiniu [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, + :multipart => true + } + send_multipart_request(url, post_data) + end + def generate_query_string(params) return params if params.is_a?(String) total_param = params.map { |key, value| key.to_s+"="+value.to_s } diff --git a/lib/qiniu/rs/version.rb b/lib/qiniu/rs/version.rb index cf22346..43cf2ae 100755 --- a/lib/qiniu/rs/version.rb +++ b/lib/qiniu/rs/version.rb @@ -2,6 +2,6 @@ module Qiniu module RS - VERSION = "2.2.0" + VERSION = "2.3.0" end end diff --git a/spec/qiniu/rs/io_spec.rb b/spec/qiniu/rs/io_spec.rb index 0563ca8..fd88969 100755 --- a/spec/qiniu/rs/io_spec.rb +++ b/spec/qiniu/rs/io_spec.rb @@ -19,21 +19,26 @@ module Qiniu data["refresh_token"].should_not be_empty puts data.inspect =end - - code2, data2 = Qiniu::RS::IO.put_auth() - code2.should == 200 - data2["url"].should_not be_empty - data2["expiresIn"].should_not be_zero - puts data2.inspect - - @put_url = data2["url"] @bucket = "test" @key = Digest::SHA1.hexdigest (Time.now.to_i+rand(100)).to_s end + context ".upload_file" do + it "should works" do + code, data = Qiniu::RS::IO.put_auth() + code.should == 200 + data["url"].should_not be_empty + data["expiresIn"].should_not be_zero + puts data.inspect + code2, data2 = Qiniu::RS::IO.upload_file(data["url"], __FILE__, @bucket, @key) + code2.should == 200 + puts data2.inspect + end + end + context ".put_file" do it "should works" do - code, data = Qiniu::RS::IO.put_file(@put_url, __FILE__, @bucket, @key) + code, data = Qiniu::RS::IO.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true) code.should == 200 puts data.inspect end diff --git a/spec/qiniu/rs/rs_spec.rb b/spec/qiniu/rs/rs_spec.rb index 960ab85..6d2af22 100755 --- a/spec/qiniu/rs/rs_spec.rb +++ b/spec/qiniu/rs/rs_spec.rb @@ -33,9 +33,9 @@ module Qiniu @domain = 'cdn.example.com' end - context "IO.put_file" do + context "IO.upload_file" do it "should works" do - code, data = Qiniu::RS::IO.put_file(@put_url, __FILE__, @bucket, @key) + code, data = Qiniu::RS::IO.upload_file(@put_url, __FILE__, @bucket, @key) code.should == 200 puts data.inspect end diff --git a/spec/qiniu/rs_spec.rb b/spec/qiniu/rs_spec.rb index 732be23..d033d5e 100755 --- a/spec/qiniu/rs_spec.rb +++ b/spec/qiniu/rs_spec.rb @@ -43,11 +43,23 @@ module Qiniu :file => __FILE__, :bucket => @bucket, :key => @key, + :mime_type => 'application/x-ruby', :enable_crc32_check => true result.should be_true end end + context ".put_file" do + it "should works" do + result = Qiniu::RS.put_file :file => __FILE__, + :bucket => @bucket, + :key => @key, + :mime_type => 'application/x-ruby', + :enable_crc32_check => true + result.should be_true + end + end + context ".stat" do it "should works" do result = Qiniu::RS.stat(@bucket, @key)