Sha256: ef25bdbdda2f69bb19ef941367d0546f733e8f8ca38337937424d32727526096

Contents?: true

Size: 1007 Bytes

Versions: 11

Compression:

Stored size: 1007 Bytes

Contents

module Uploadbox
  module ImgHelper
    def s3_policy
      Base64.encode64(policy_data.to_json).gsub("\n", "")
    end


    def s3_signature
      Base64.encode64(
        OpenSSL::HMAC.digest(
          OpenSSL::Digest::Digest.new('sha1'),
          ENV['S3_SECRET'],
          s3_policy
        )
      ).gsub("\n", "")
    end

    def img(source, options={})
      if source.respond_to?(:url) and source.respond_to?(:width) and source.respond_to?(:height)
        image_tag(source.url, {width: source.width, height: source.height}.merge(options))
      else
        image_tag(source, options)
      end
    end

    private
      def policy_data
        {
          expiration: 10.hours.from_now.utc.iso8601,
          conditions: [
            ["starts-with", "$key", 'uploads/'],
            ["content-length-range", 0, 500.megabytes],
            ["starts-with","$content-type",""],
            {bucket: ENV['S3_BUCKET']},
            {acl: 'public-read'}
          ]
        }
      end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
uploadbox-0.0.22 app/helpers/uploadbox/img_helper.rb
uploadbox-0.0.21 app/helpers/uploadbox/img_helper.rb
uploadbox-0.0.20 app/helpers/uploadbox/img_helper.rb
uploadbox-0.0.19 app/helpers/uploadbox/img_helper.rb
uploadbox-0.0.18 app/helpers/uploadbox/img_helper.rb
uploadbox-0.0.17 app/helpers/uploadbox/img_helper.rb
uploadbox-0.0.16 app/helpers/uploadbox/img_helper.rb
uploadbox-0.0.15 app/helpers/uploadbox/img_helper.rb
uploadbox-0.0.14 app/helpers/uploadbox/img_helper.rb
uploadbox-0.0.13 app/helpers/uploadbox/img_helper.rb
uploadbox-0.0.12 app/helpers/uploadbox/img_helper.rb