File: aliyun_oss_helper.rb

package info (click to toggle)
ruby-aliyun-sdk 0.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 792 kB
  • sloc: ruby: 7,909; ansic: 204; makefile: 4
file content (53 lines) | stat: -rw-r--r-- 1,922 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Put it under: RAILS-APP/app/helpers/

module AliyunOssHelper
  ##
  # Generate a form for upload file to OSS
  # @param [OSS::Bucket] bucket the bucket
  # @param [Hash] opts custom options
  # @option opts [String] :prefix the prefix of the result object
  # @option opts [String] :save_as the object key without prefix of
  #  the result object
  # @option opts [ActiveSupport::Duration] :expiry the expiration time
  #  of this form. Defaults to 60 seconds
  # @option opts [String] :redirect the page after successfully upload
  #  the object
  # @example in controller
  #   @bucket = OSS.client.get_bucket('my-bucket')
  #   @options = {:prefix => '/foo/', :expiry => 60.seconds,
  #               :redirect => 'http://my-domain.com'}
  # @example in views
  #   <%= upload_form(@bucket, @options) do %>
  #   <input type="file" name="file" style="display:inline" />
  #   <button type="submit">Upload</button>
  def upload_form(bucket, opts, &block)
    content = ActiveSupport::SafeBuffer.new

    content.safe_concat(
      form_tag_html(
        html_options_for_form(bucket.bucket_url, multipart: true)))

    key = if opts[:save_as]
            opts[:save_as]
          else
            "${filename}"
          end
    content << hidden_field_tag(:key, "#{opts[:prefix]}#{key}")
    content << hidden_field_tag(:OSSAccessKeyId, bucket.access_key_id)
    expiry = opts[:expiry] || 60.seconds
    policy = {
      'expiration' => (Time.now + expiry).utc.iso8601.sub('Z', '.000Z'),
      'conditions' => [{'bucket' => bucket.name}]
    }
    policy_string = Base64.strict_encode64(policy.to_json)
    content << hidden_field_tag(:policy, policy_string)
    content << hidden_field_tag(:Signature, bucket.sign(policy_string))
    if opts[:redirect]
      content << hidden_field_tag(:success_action_redirect, opts[:redirect])
    end

    content << capture(&block)

    content.safe_concat("</form>")
  end
end