Skip to content

Instantly share code, notes, and snippets.

@huacnlee
Created August 21, 2012 10:55
Show Gist options
  • Save huacnlee/3414491 to your computer and use it in GitHub Desktop.
Save huacnlee/3414491 to your computer and use it in GitHub Desktop.
UpYun 表单上传验证信息 helper 方法,用于生成 input tag
<form action="http://v0.api.upyun.com/rbtest/" method="post" enctype="multipart/form-data">
<%= upyun_form_input_tag(:return_url => new_post_path) %>
<input type="file" name="file" />
<button type="submit">上传图片</button>
</form>
def upyun_form_input_tag(opts = {})
form_api_key = "THE API KEY"
opts[:prefix] ||= ""
opts[:allow_exts] ||= "jpg,jpeg,gif,png"
opts[:min_length] ||= 0
opts[:max_length] ||= 200.kilobyte
opts[:return_url] ||= root_url
opts[:notify_url] ||= root_url
opts[:unsharp] ||= false
opts[:quality] ||= 95
hash = {
"bucket" => "rbtest",
"expiration" => 20.minutes.since.to_i,
"save-key" => [opts[:prefix],"{year}/{mon}/{filemd5}{.suffix}"].join("/"),
"allow-file-type" => opts[:allow_exts],
"content-length-range" => "#{opts[:min_length].to_i},#{opts[:max_length].to_i}",
"return-url" => opts[:return_url],
"notify-url" => opts[:notify_url],
"x-gmkerl-unsharp" => opts[:unsharp],
"x-gmkerl-quality" => opts[:quality].to_i
}
puts "JSON: #{hash.to_json}"
policy = Base64.encode64(hash.to_json).gsub("\n","")
sign = Digest::MD5.hexdigest([policy,form_api_key].join("&"))
raw [tag(:input, :type => "hidden", :name => "policy", :value => policy),tag(:input, :type => :hidden, :name => "signature",:value => sign)].join("\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment