diff --git a/lib/qiniu/rs.rb b/lib/qiniu/rs.rb index ae5e433..7fe6f9f 100755 --- a/lib/qiniu/rs.rb +++ b/lib/qiniu/rs.rb @@ -31,8 +31,15 @@ module Qiniu code == StatusOK ? data["url"] : false end - def upload(url, local_file, bucket = '', key = '', mime_type = '', custom_meta = '', callback_params = {}) - code, data = IO.put_file(url, local_file, bucket, key, mime_type, custom_meta, callback_params) + def upload opts = {} + code, data = IO.put_file(opts[:url], + 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/config.rb b/lib/qiniu/rs/config.rb index 077ab0d..7b1f6e4 100755 --- a/lib/qiniu/rs/config.rb +++ b/lib/qiniu/rs/config.rb @@ -21,9 +21,9 @@ module Qiniu :content_type => 'application/x-www-form-urlencoded', :auth_url => "https://acc.qbox.me/oauth2/token", :rs_host => "http://rs.qbox.me:10100", - :io_host => "http://io.qbox.me", - :client_id => "", - :client_secret => "", + :io_host => "http://iovip.qbox.me", + :client_id => "", + :client_secret => "", :auto_reconnect => true, :max_retry_times => 5 } diff --git a/lib/qiniu/rs/io.rb b/lib/qiniu/rs/io.rb index 9790979..8e2768a 100755 --- a/lib/qiniu/rs/io.rb +++ b/lib/qiniu/rs/io.rb @@ -20,17 +20,18 @@ module Qiniu Auth.request(url) end - def put_file(url, local_file, bucket = '', key = '', mime_type = '', custom_meta = '', callback_params = '') + def put_file(url, local_file, bucket = nil, key = nil, mime_type = nil, custom_meta = nil, callback_params = nil, enable_crc32_check = false) raise NoSuchFileError unless File.exist?(local_file) - key = Digest::SHA1.hexdigest(local_file + Time.now.to_s) if key.empty? + key = Digest::SHA1.hexdigest(local_file + Time.now.to_s) if key.nil? entry_uri = bucket + ':' + key - if mime_type.empty? + if mime_type.nil? 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.empty? - callback_params = {:bucket => bucket, :key => key, :mime_type => mime_type} if callback_params.empty? + 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) end diff --git a/lib/qiniu/rs/utils.rb b/lib/qiniu/rs/utils.rb index bc58aad..0009cde 100755 --- a/lib/qiniu/rs/utils.rb +++ b/lib/qiniu/rs/utils.rb @@ -2,6 +2,7 @@ require 'uri' require 'json' +require 'zlib' require 'base64' require 'rest_client' require 'qiniu/rs/exceptions' @@ -119,6 +120,10 @@ module Qiniu URI.escape(total_param.join("&")) end + def crc32checksum(filepath) + File.open(filepath, "rb") { |f| Zlib.crc32 f.read } + end + end end end diff --git a/lib/qiniu/rs/version.rb b/lib/qiniu/rs/version.rb index c39c329..4a7eaf0 100755 --- a/lib/qiniu/rs/version.rb +++ b/lib/qiniu/rs/version.rb @@ -2,6 +2,6 @@ module Qiniu module RS - VERSION = "1.0.4" + VERSION = "1.1.0" end end diff --git a/spec/qiniu/rs_spec.rb b/spec/qiniu/rs_spec.rb index 9ae0310..2ba7500 100755 --- a/spec/qiniu/rs_spec.rb +++ b/spec/qiniu/rs_spec.rb @@ -34,7 +34,11 @@ module Qiniu put_url.should_not be_false put_url.should_not be_empty puts put_url.inspect - result = Qiniu::RS.upload(put_url, __FILE__, @bucket, @key) + result = Qiniu::RS.upload :url => put_url, + :file => __FILE__, + :bucket => @bucket, + :key => @key, + :enable_crc32_check => true result.should be_true end end