diff --git a/lib/upyun.rb b/lib/upyun.rb index b900b9f..614cdeb 100644 --- a/lib/upyun.rb +++ b/lib/upyun.rb @@ -1,6 +1,7 @@ require 'upyun/version' require 'upyun/utils' require 'upyun/rest' +require 'upyun/form_base' require 'upyun/form' module Upyun diff --git a/lib/upyun/form.rb b/lib/upyun/form.rb index 4dc90e6..451bc63 100644 --- a/lib/upyun/form.rb +++ b/lib/upyun/form.rb @@ -1,60 +1,24 @@ # encoding: utf-8 require 'restclient' -require 'base64' require 'json' -require 'active_support/hash_with_indifferent_access' module Upyun - class Form - include Utils + class Form < FormBase - 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 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 @@ -81,24 +45,8 @@ def upload(file, opts={}) end end - private - 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 + def rest_client + @rest_clint ||= RestClient::Resource.new("http://#{@endpoint}/#{@bucket}", options) + end end end diff --git a/lib/upyun/form_base.rb b/lib/upyun/form_base.rb new file mode 100644 index 0000000..c867107 --- /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 :password, :api_secret + + 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