added file uploading crc32 check

This commit is contained in:
404
2012-06-08 14:03:04 +08:00
parent 285b766819
commit 853c3b537d
6 changed files with 29 additions and 12 deletions
+9 -2
View File
@@ -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
+3 -3
View File
@@ -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 => "<YOUR_APP_CLIENT_ID>",
:client_secret => "<YOUR_APP_CLIENT_SECRET>",
:io_host => "http://iovip.qbox.me",
:client_id => "",
:client_secret => "",
:auto_reconnect => true,
:max_retry_times => 5
}
+6 -5
View File
@@ -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
+5
View File
@@ -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
+1 -1
View File
@@ -2,6 +2,6 @@
module Qiniu
module RS
VERSION = "1.0.4"
VERSION = "1.1.0"
end
end
+5 -1
View File
@@ -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