Created
August 21, 2012 10:55
-
-
Save huacnlee/3414491 to your computer and use it in GitHub Desktop.
UpYun 表单上传验证信息 helper 方法,用于生成 input tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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