From 84157d35fa789a56a17c9b2b0f8c7a59c8ed43e2 Mon Sep 17 00:00:00 2001 From: 404 Date: Mon, 23 Jul 2012 17:46:10 +0800 Subject: [PATCH 1/2] updated docs/README.md --- Gemfile.lock | 2 +- docs/README.md | 110 ++++++++++++++++++++++++++++++++++++++++ lib/qiniu/rs/version.rb | 2 +- 3 files changed, 112 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index a86ed17..fb6af3e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - qiniu-rs (2.2.0) + qiniu-rs (2.2.1) json (~> 1.7.3) mime-types (~> 1.19) rest-client (~> 1.6.7) diff --git a/docs/README.md b/docs/README.md index 317143b..e1fe354 100644 --- a/docs/README.md +++ b/docs/README.md @@ -30,6 +30,8 @@ title: Ruby SDK 使用指南 | 七牛云存储 - [图像处理](#op-image) - [查看图片属性信息](#image_info) - [获取指定规格的缩略图预览地址](#image_preview_url) + - [高级图像处理(缩略、裁剪、旋转、转化)](#image_mogrify_preview_url) + - [高级图像处理(缩略、裁剪、旋转、转化)并持久化](#image_mogrify_save_as) - [贡献代码](#Contributing) - [许可证](#License) @@ -500,6 +502,114 @@ spec 返回一个字符串类型的缩略图 URL + + +#### 高级图像处理(缩略、裁剪、旋转、转化) + +`Qiniu::RS.image_mogrify_preview_url()` 方法支持将一个存储在七牛云存储的图片进行缩略、裁剪、旋转和格式转化处理,该方法返回一个可以直接预览缩略图的URL。 + + image_mogrify_preview_url = Qiniu::RS.image_mogrify_preview_url(source_image_url, mogrify_options) + +**参数** + +source_image_url +: 必须,字符串类型(string),指定原始图片的下载链接,可以根据 rs.get() 获取到。 + +mogrify_options +: 必须,Hash Map 格式的图像处理参数。 + +`mogrify_options` 对象具体的规格如下: + + mogrify_options = { + :thumbnail => , + :gravity => , =NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast + :crop => , + :quality => , + :rotate => , + :format => , =jpg, gif, png, tif, etc. + :auto_orient => + } + +`Qiniu::RS.image_mogrify_preview_url()` 方法是对七牛云存储图像处理高级接口的完整包装,关于 `mogrify_options` 参数里边的具体含义和使用方式,可以参考文档:[图像处理高级接口](#/v2/api/foimg/#fo-imageMogr)。 + +**返回值** + +返回一个可以预览最终缩略图的URL,String 类型。 + + + + +#### 高级图像处理(缩略、裁剪、旋转、转化)并持久化存储处理结果 + +`Qiniu::RS.image_mogrify_save_as()` 方法支持将一个存储在七牛云存储的图片进行缩略、裁剪、旋转和格式转化处理,并且将处理后的缩略图作为一个新文件持久化存储到七牛云存储服务器上,这样就可以供后续直接使用而不用每次都传入参数进行图像处理。 + + result = Qiniu::RS.image_mogrify_save_as(target_bucket, target_key, src_img_url, mogrify_options) + +**参数** + +target_bucket +: 必须,字符串类型(string),指定最终缩略图要存放的 bucket 。 + +target_key +: 必须,字符串类型(string),指定最终缩略图存放在云存储服务端的唯一文件ID。 + +src_img_url +: 必须,字符串类型(string),指定原始图片的下载链接,可以根据 rs.get() 获取到。 + +mogrify_options +: 必须,Hash Map 格式的图像处理参数。 + +`mogrify_options` 对象具体的规格如下: + + mogrify_options = { + :thumbnail => , + :gravity => , =NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast + :crop => , + :quality => , + :rotate => , + :format => , =jpg, gif, png, tif, etc. + :auto_orient => + } + +`Qiniu::RS::Image.mogrify_preview_url()` 方法是对七牛云存储图像处理高级接口的完整包装,关于 `mogrify_options` 参数里边的具体含义和使用方式,可以参考文档:[图像处理高级接口](#/v2/api/foimg/#fo-imageMogr)。 + +**返回值** + +如果请求失败,返回 `false`;否则,返回如下一个 `Hash` 类型的结构: + + {"hash" => "FrOXNat8VhBVmcMF3uGrILpTu8Cs"} + +示例代码: + + data = Qiniu::RS.get("", "") + src_img_url = data["url"] + + target_bucket = "test_thumbnails_bucket" + target_key = "cropped-" + @test_image_key + + mogrify_options = { + :thumbnail => "!120x120r", + :gravity => "center", + :crop => "!120x120a0a0", + :quality => 85, + :rotate => 45, + :format => "jpg", + :auto_orient => true + } + + result = Qiniu::RS.image_mogrify_save_as(target_bucket, target_key, src_img_url, mogrify_options) + if result + thumbnail = Qiniu::RS.get(target_bucket, target_key) + puts thumbnail["url"] + + # 您可以选择将存放缩略图的 bucket 公开,这样就可以直接以外链的形式访问到缩略图,而不用走API获取下载URL。 + result = Qiniu::RS.publish("pic.example.com", target_bucket) + + # 然后将 pic.example.com CNAME 到 iovip.qbox.me ,就可以直接以如下方式访问缩略图 + # [GET] http://pic.example.com/ + end + + ## 贡献代码 diff --git a/lib/qiniu/rs/version.rb b/lib/qiniu/rs/version.rb index cf22346..2f080cf 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.2.1" end end From 7df449cc736365770302d788b73d5d61c09fa7c0 Mon Sep 17 00:00:00 2001 From: 404 Date: Tue, 24 Jul 2012 16:54:09 +0800 Subject: [PATCH 2/2] 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)