Skip to content

重构 Upyun::Form #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/upyun.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'upyun/version'
require 'upyun/utils'
require 'upyun/rest'
require 'upyun/form_base'
require 'upyun/form'

module Upyun
Expand Down
64 changes: 6 additions & 58 deletions lib/upyun/form.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
68 changes: 68 additions & 0 deletions lib/upyun/form_base.rb
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjason 抱歉看懂这里调整的目的是?另外 commit 信息不准确吧 from password -> form 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