Skip to content

Instantly share code, notes, and snippets.

@ffullenk
Forked from ryantownsend/carrierwave_direct.rb
Last active August 29, 2015 14:24
Show Gist options
  • Save ffullenk/f2cc940ebd29edadb314 to your computer and use it in GitHub Desktop.
Save ffullenk/f2cc940ebd29edadb314 to your computer and use it in GitHub Desktop.
# config/initializers/carrierwave_direct.rb
require "carrierwave_direct"
module CarrierWaveDirect
module SignatureFixMonkeyPatch
def policy(options = {})
options[:expiration] ||= upload_expiration
options[:min_file_size] ||= min_file_size
options[:max_file_size] ||= max_file_size
@policy ||= generate_policy(options)
end
def clear_policy!
@policy = nil
end
private
def generate_policy(options)
conditions = [
["starts-with", "$utf8", ""],
["starts-with", "$key", key.sub(/#{Regexp.escape(FILENAME_WILDCARD)}\z/, "")]
]
conditions << ["starts-with", "$Content-Type", ""] if will_include_content_type
conditions << {"bucket" => fog_directory}
conditions << {"acl" => acl}
if use_action_status
conditions << {"success_action_status" => success_action_status}
else
conditions << {"success_action_redirect" => success_action_redirect}
end
conditions << ["content-length-range", options[:min_file_size], options[:max_file_size]]
Base64.encode64(
{
'expiration' => Time.now.utc + options[:expiration],
'conditions' => conditions
}.to_json
).gsub("\n","")
end
end
end
CarrierWaveDirect::Uploader.send(:include, CarrierWaveDirect::SignatureFixMonkeyPatch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment