added Qiniu::RS.image_mogrify_save_as() and Qiniu::RS::Image.mogrify_preview_url()

This commit is contained in:
404
2012-07-23 17:10:17 +08:00
parent dddbf793e5
commit 313e368602
7 changed files with 102 additions and 9 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
qiniu-rs (2.1.2)
qiniu-rs (2.2.0)
json (~> 1.7.3)
mime-types (~> 1.19)
rest-client (~> 1.6.7)
+9
View File
@@ -118,6 +118,15 @@ module Qiniu
Image.preivew_url(url, spec)
end
def image_mogrify_preview_url(source_image_url, options)
Image.mogrify_preview_url(source_image_url, options)
end
def image_mogrify_save_as(bucket, key, source_image_url, options)
code, data = RS.image_mogrify_save_as(bucket, key, source_image_url, options)
code == StatusOK ? data : false
end
#def generate_upload_token(scope, expires_in, callback_url = nil, return_url = nil)
# Utils.generate_upload_token(scope, expires_in, callback_url, return_url)
#end
+18
View File
@@ -14,6 +14,24 @@ module Qiniu
url + '/imagePreview/' + spec.to_s
end
def mogrify_preview_url(source_image_url, options)
source_image_url + '?' + generate_mogrify_params_string(options)
end
def generate_mogrify_params_string(options = {})
opts = {}
options.each do |k, v|
opts[k.to_s] = v
end
params_string = ""
keys = ["thumbnail", "gravity", "crop", "quality", "rotate", "format"]
keys.each do |key|
params_string += %Q(/#{key}/#{opts[key]}) unless opts[key].nil?
end
params_string += '/auto-orient' unless opts["auto_orient"].nil?
'imageMogr' + params_string
end
end
end
end
+12
View File
@@ -57,6 +57,18 @@ module Qiniu
batch("delete", bucket, keys)
end
def save_as(bucket, key, source_url, op_params_string)
encoded_uri = encode_entry_uri(bucket, key)
save_as_string = '/save-as/' + encoded_uri
new_url = source_url + '?' + op_params_string + save_as_string
Auth.request new_url
end
def image_mogrify_save_as(bucket, key, source_image_url, options)
mogrify_params_string = Image.generate_mogrify_params_string(options)
save_as(bucket, key, source_image_url, mogrify_params_string)
end
end
end
end
+1 -1
View File
@@ -2,6 +2,6 @@
module Qiniu
module RS
VERSION = "2.1.2"
VERSION = "2.2.0"
end
end
+31 -6
View File
@@ -9,7 +9,7 @@ module Qiniu
module RS
describe Image do
before :each do
before :all do
=begin
code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test")
code.should == 200
@@ -20,11 +20,15 @@ module Qiniu
puts data.inspect
=end
@bucket = "test_images"
@bucket = "test_images_12345"
@key = "image_logo_for_test.png"
local_file = File.expand_path('../' + @key, __FILE__)
result = Qiniu::RS.drop(@bucket)
result.should_not be_false
puts result.inspect
put_url = Qiniu::RS.put_auth(10)
put_url.should_not be_false
put_url.should_not be_empty
@@ -32,21 +36,42 @@ module Qiniu
:file => local_file,
:bucket => @bucket,
:key => @key,
:mime_type => "image/png",
:enable_crc32_check => true
result.should be_true
result = Qiniu::RS.get(@bucket, @key)
result["url"].should_not be_empty
puts result.inspect
@source_image_url = result["url"]
@mogrify_options = {
:thumbnail => "!120x120r",
:gravity => "center",
:crop => "!120x120a0a0",
:quality => 85,
:rotate => 45,
:format => "jpg",
:auto_orient => true
}
end
context ".info" do
it "should works" do
result = Qiniu::RS.get(@bucket, @key)
result["url"].should_not be_empty
puts result.inspect
code, data = Qiniu::RS::Image.info(result["url"])
code, data = Qiniu::RS::Image.info(@source_image_url)
code.should == 200
puts data.inspect
end
end
context ".mogrify_preview_url" do
it "should works" do
mogrify_preview_url = Qiniu::RS::Image.mogrify_preview_url(@source_image_url, @mogrify_options)
puts mogrify_preview_url.inspect
end
end
end
end
end
+30 -1
View File
@@ -10,6 +10,9 @@ module Qiniu
@bucket = 'qiniu_rs_test'
@key = Digest::SHA1.hexdigest Time.now.to_s
@domain = 'cdn.example.com'
@test_image_bucket = 'test_images_12345'
@test_image_key = 'image_logo_for_test.png'
end
=begin
@@ -138,7 +141,7 @@ module Qiniu
context ".image_info" do
it "should works" do
data = Qiniu::RS.get("test_images", "image_logo_for_test.png")
data = Qiniu::RS.get(@test_image_bucket, @test_image_key)
data.should_not be_false
data.should_not be_empty
puts data.inspect
@@ -149,6 +152,32 @@ module Qiniu
end
end
context ".image_mogrify_save_as" do
it "should works" do
data = Qiniu::RS.get(@test_image_bucket, @test_image_key)
data.should_not be_false
data.should_not be_empty
puts data.inspect
dest_bucket = "test_thumbnails_bucket"
dest_key = "cropped-" + @test_image_key
src_img_url = data["url"]
mogrify_options = {
:thumbnail => "!120x120r",
:gravity => "center",
:crop => "!120x120a0a0",
:quality => 85,
:rotate => 45,
:format => "jpg",
:auto_orient => true
}
result = Qiniu::RS.image_mogrify_save_as(dest_bucket, dest_key, src_img_url, mogrify_options)
result.should_not be_false
result.should_not be_empty
puts result.inspect
end
end
context ".generate_upload_token" do
it "should works" do
data = Qiniu::RS.generate_upload_token({:scope => 'test_bucket', :expires_in => 3600})