Sha256: 8247bb9835a38b43b6a4f1afe6399e5ef475f0e012c272ce6b3fc2f94d7b7147

Contents?: true

Size: 1.17 KB

Versions: 7

Compression:

Stored size: 1.17 KB

Contents

module AdminIt
  # Sign urls for s3
  class SignedUrlController < AdminIt.config.controller
    before_filter :authenticate_user!

    def index
      render json: {
        policy: s3_upload_policy_document,
        signature: s3_upload_signature,
        key: "uploads/#{SecureRandom.uuid}/#{params[:doc][:title]}",
        success_action_redirect: '/'
      }
    end

    private

    # generate the policy document that amazon is expecting.
    def s3_upload_policy_document
      Base64.encode64(
        {
          expiration: 15.minutes.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z'),
          conditions: [
            { bucket: AdminIt.config.s3[:bucket] },
            { acl: 'public-read' },
            ['starts-with', '$key', 'uploads/'],
            { success_action_status: '201' }
          ]
        }.to_json
      ).gsub(/\n|\r/, '')
    end

    # sign our request by Base64 encoding the policy document.
    def s3_upload_signature
      Base64.encode64(
        OpenSSL::HMAC.digest(
          OpenSSL::Digest::Digest.new('sha1'),
          AdminIt.config.s3[:secret_access_key],
          s3_upload_policy_document
        )
      ).gsub(/\n/, '')
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
admin_it-1.2.6 app/controllers/admin_it/signed_url_controller.rb
admin_it-1.2.5 app/controllers/admin_it/signed_url_controller.rb
admin_it-1.2.4 app/controllers/admin_it/signed_url_controller.rb
admin_it-1.2.3 app/controllers/admin_it/signed_url_controller.rb
admin_it-1.2.2 app/controllers/admin_it/signed_url_controller.rb
admin_it-1.2.1 app/controllers/admin_it/signed_url_controller.rb
admin_it-1.2.0 app/controllers/admin_it/signed_url_controller.rb