From 9eb9c307ab3e8118c8089a645b5a8e986efd9fc1 Mon Sep 17 00:00:00 2001 From: mj Date: Sat, 23 Jul 2016 01:07:14 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0from=5Fsupport=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/upyun.rb | 1 + lib/upyun/form.rb | 60 +++++++++++---------------------------- lib/upyun/form_support.rb | 38 +++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 44 deletions(-) create mode 100644 lib/upyun/form_support.rb diff --git a/lib/upyun.rb b/lib/upyun.rb index b900b9f..30aa3ac 100644 --- a/lib/upyun.rb +++ b/lib/upyun.rb @@ -2,6 +2,7 @@ require 'upyun/utils' require 'upyun/rest' require 'upyun/form' +require 'upyun/form_support' module Upyun DOMAIN = 'api.upyun.com' diff --git a/lib/upyun/form.rb b/lib/upyun/form.rb index 4dc90e6..738e840 100644 --- a/lib/upyun/form.rb +++ b/lib/upyun/form.rb @@ -5,39 +5,12 @@ require 'active_support/hash_with_indifferent_access' module Upyun - class Form + class Form < FormSupport include Utils - - VALID_PARAMS = %w( - bucket - save-key - expiration - allow-file-type - content-length-range - content-md5 - content-secret - content-type - image-width-range - image-height-range - notify-url - return-url - x-gmkerl-thumbnail - x-gmkerl-type - x-gmkerl-value - x-gmkerl-quality - x-gmkerl-unsharp - x-gmkerl-rotate - x-gmkerl-crop - x-gmkerl-exif-switch - ext-param - ) - - attr_accessor :bucket, :password attr_reader :options def initialize(password, bucket, options={timeout: 60}) - @password = password - @bucket = bucket + super(api_secret: password, bucket: bucket) @options = options @endpoint = ED_AUTO end @@ -81,24 +54,23 @@ def upload(file, opts={}) end end - private - def policy(opts) - @_policy = Base64.strict_encode64(policy_json(opts)) - end + def policy(opts) + @_policy = Base64.strict_encode64(policy_json(opts)) + end - def signature - md5("#{@_policy}&#{@password}") - end + def signature + md5("#{@_policy}&#{@password}") + end - def policy_json(opts) - policies = VALID_PARAMS.reduce({}) do |memo, e| - (v = opts[e]) ? memo.merge!({e => v}) : memo - end - policies.to_json + def policy_json(opts) + policies = VALID_PARAMS.reduce({}) do |memo, e| + (v = opts[e]) ? memo.merge!({e => v}) : memo end + policies.to_json + end - def rest_client - @rest_clint ||= RestClient::Resource.new("http://#{@endpoint}/#{@bucket}", options) - end + def rest_client + @rest_clint ||= RestClient::Resource.new("http://#{@endpoint}/#{@bucket}", options) + end end end diff --git a/lib/upyun/form_support.rb b/lib/upyun/form_support.rb new file mode 100644 index 0000000..aec9afe --- /dev/null +++ b/lib/upyun/form_support.rb @@ -0,0 +1,38 @@ +# encoding: utf-8 +module Upyun + class FormSupport + + VALID_PARAMS = %w( + bucket + save-key + expiration + allow-file-type + content-length-range + content-md5 + content-secret + content-type + image-width-range + image-height-range + notify-url + return-url + x-gmkerl-thumbnail + x-gmkerl-type + x-gmkerl-value + x-gmkerl-quality + x-gmkerl-unsharp + x-gmkerl-rotate + x-gmkerl-crop + x-gmkerl-exif-switch + ext-param + ) + + attr_accessor :api_secret, :bucket, :password + attr_reader :params + alias :api_secret, :password + + def initialize(api_secret:, bucket:, params: {}) + @api_secret, @bucket, @params = api_secret, bucket, params + end + + end +end \ No newline at end of file From 3670ffec7dd1bba4c441575b1ebdf65da6d4e139 Mon Sep 17 00:00:00 2001 From: mj Date: Sat, 23 Jul 2016 01:33:41 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E9=87=8D=E6=9E=84form=20=E4=BD=BF=E6=9C=9F?= =?UTF-8?q?=E8=83=BD=E5=A4=9F=E6=8F=90=E4=BE=9Bsignature,policy=E7=BB=99?= =?UTF-8?q?=E5=89=8D=E7=AB=AF=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/upyun.rb | 2 +- lib/upyun/form.rb | 30 ++--------------- lib/upyun/form_base.rb | 68 +++++++++++++++++++++++++++++++++++++++ lib/upyun/form_support.rb | 38 ---------------------- 4 files changed, 72 insertions(+), 66 deletions(-) create mode 100644 lib/upyun/form_base.rb delete mode 100644 lib/upyun/form_support.rb diff --git a/lib/upyun.rb b/lib/upyun.rb index 30aa3ac..614cdeb 100644 --- a/lib/upyun.rb +++ b/lib/upyun.rb @@ -1,8 +1,8 @@ require 'upyun/version' require 'upyun/utils' require 'upyun/rest' +require 'upyun/form_base' require 'upyun/form' -require 'upyun/form_support' module Upyun DOMAIN = 'api.upyun.com' diff --git a/lib/upyun/form.rb b/lib/upyun/form.rb index 738e840..451bc63 100644 --- a/lib/upyun/form.rb +++ b/lib/upyun/form.rb @@ -1,12 +1,10 @@ # encoding: utf-8 require 'restclient' -require 'base64' require 'json' -require 'active_support/hash_with_indifferent_access' module Upyun - class Form < FormSupport - include Utils + class Form < FormBase + attr_reader :options def initialize(password, bucket, options={timeout: 60}) @@ -16,18 +14,11 @@ def initialize(password, bucket, options={timeout: 60}) end def upload(file, opts={}) - base_opts = HashWithIndifferentAccess.new({ - 'bucket' => @bucket, - 'save-key' => '/{year}/{mon}/{day}/{filename}{.suffix}', - 'expiration' => Time.now.to_i + 600 - }) - payload = { - policy: policy(base_opts.merge(opts)), + policy: policy(opts), signature: signature, file: file.is_a?(File) ? file : File.new(file, 'rb') } - rest_client.post(payload, {'User-Agent' => "Upyun-Ruby-SDK-#{VERSION}"}) do |res| case res.code when 302 @@ -54,21 +45,6 @@ def upload(file, opts={}) end end - def policy(opts) - @_policy = Base64.strict_encode64(policy_json(opts)) - end - - def signature - md5("#{@_policy}&#{@password}") - end - - def policy_json(opts) - policies = VALID_PARAMS.reduce({}) do |memo, e| - (v = opts[e]) ? memo.merge!({e => v}) : memo - end - policies.to_json - end - def rest_client @rest_clint ||= RestClient::Resource.new("http://#{@endpoint}/#{@bucket}", options) end diff --git a/lib/upyun/form_base.rb b/lib/upyun/form_base.rb new file mode 100644 index 0000000..9f4c9bd --- /dev/null +++ b/lib/upyun/form_base.rb @@ -0,0 +1,68 @@ +# encoding: utf-8 +require 'base64' +require 'json' +require 'active_support/hash_with_indifferent_access' + +module Upyun + class FormBase + include Utils + + VALID_PARAMS = %w( + bucket + save-key + expiration + allow-file-type + content-length-range + content-md5 + content-secret + content-type + image-width-range + image-height-range + notify-url + return-url + x-gmkerl-thumbnail + x-gmkerl-type + x-gmkerl-value + x-gmkerl-quality + x-gmkerl-unsharp + x-gmkerl-rotate + x-gmkerl-crop + x-gmkerl-exif-switch + ext-param + ) + + attr_accessor :api_secret, :bucket, :password, :params + alias_method :api_secret, :password + + def initialize(api_secret:, bucket:, params: {}) + @api_secret, @bucket, @params = api_secret, bucket, params + end + + def policy(params={}) + opts = default_params.merge params + @_policy = Base64.strict_encode64(policy_json opts) + end + + def signature + md5("#{@_policy}&#{@api_secret}") + end + + def policy_json(opts) + policies = VALID_PARAMS.reduce({}) do |memo, e| + (v = opts[e]) ? memo.merge!({e => v}) : memo + end + policies.to_json + end + + ## + # 默认参数 + def default_params + HashWithIndifferentAccess.new({ + 'bucket' => @bucket, + 'save-key' => '/{year}/{mon}/{day}/{filename}{.suffix}', + 'expiration' => Time.now.to_i + 600 + }).merge @params + end + + end +end \ No newline at end of file diff --git a/lib/upyun/form_support.rb b/lib/upyun/form_support.rb deleted file mode 100644 index aec9afe..0000000 --- a/lib/upyun/form_support.rb +++ /dev/null @@ -1,38 +0,0 @@ -# encoding: utf-8 -module Upyun - class FormSupport - - VALID_PARAMS = %w( - bucket - save-key - expiration - allow-file-type - content-length-range - content-md5 - content-secret - content-type - image-width-range - image-height-range - notify-url - return-url - x-gmkerl-thumbnail - x-gmkerl-type - x-gmkerl-value - x-gmkerl-quality - x-gmkerl-unsharp - x-gmkerl-rotate - x-gmkerl-crop - x-gmkerl-exif-switch - ext-param - ) - - attr_accessor :api_secret, :bucket, :password - attr_reader :params - alias :api_secret, :password - - def initialize(api_secret:, bucket:, params: {}) - @api_secret, @bucket, @params = api_secret, bucket, params - end - - end -end \ No newline at end of file From 880b06558b9dd50ae700330dab093a1f92e3af32 Mon Sep 17 00:00:00 2001 From: mj Date: Sat, 23 Jul 2016 01:41:50 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E8=A7=A3=E5=86=B3from=20password=20?= =?UTF-8?q?=E4=B8=BAnil=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/upyun/form_base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/upyun/form_base.rb b/lib/upyun/form_base.rb index 9f4c9bd..c867107 100644 --- a/lib/upyun/form_base.rb +++ b/lib/upyun/form_base.rb @@ -32,7 +32,7 @@ class FormBase ) attr_accessor :api_secret, :bucket, :password, :params - alias_method :api_secret, :password + alias_method :password, :api_secret def initialize(api_secret:, bucket:, params: {}) @api_secret, @bucket, @params = api_secret, bucket, params