1
0
Fork 0
mirror of https://github.com/fog/fog-aws.git synced 2022-11-09 13:50:52 -05:00

Remove code duplication

This commit is contained in:
Rocco Galluzzo 2015-03-04 19:28:01 +01:00
parent 0edf8350f0
commit e9f2d6779f

View file

@ -1,7 +1,7 @@
module Fog
module Storage
class AWS
class Real
module PostObjectHiddenFields
# Get a hash of hidden fields for form uploading to S3, in the form {:field_name => :field_value}
# Form should look like: <form action="http://#{bucket_name}.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
# These hidden fields should then appear, followed by a field named 'file' which is either a textarea or file input.
@ -22,34 +22,6 @@ module Fog
#
# @see http://docs.amazonwebservices.com/AmazonS3/latest/dev/HTTPPOSTForms.html
#
def post_object_hidden_fields(options = {})
options = options.dup
if policy = options['policy']
date = Fog::Time.now
credential = "#{@aws_access_key_id}/#{@signer.credential_scope(date)}"
extra_conditions = [
{'x-amz-date' => date.to_iso8601_basic},
{'x-amz-credential' => credential},
{'x-amz-algorithm' => Fog::AWS::SignatureV4::ALGORITHM}
]
extra_conditions << {'x-amz-security-token' => @aws_session_token } if @aws_session_token
policy_with_auth_fields = policy.merge('conditions' => policy['conditions'] + extra_conditions)
options['policy'] = Base64.encode64(Fog::JSON.encode(policy_with_auth_fields)).gsub("\n", "")
options['X-Amz-Credential'] = credential
options['X-Amz-Date'] = date.to_iso8601_basic
options['X-Amz-Algorithm'] = Fog::AWS::SignatureV4::ALGORITHM
if @aws_session_token
options['X-Amz-Security-Token'] = @aws_session_token
end
options['X-Amz-Signature'] = @signer.derived_hmac(date).sign(options['policy']).unpack('H*').first
end
options
end
end
class Mock
def post_object_hidden_fields(options = {})
options = options.dup
if policy = options['policy']
@ -77,6 +49,12 @@ module Fog
options
end
end
class Real
include PostObjectHiddenFields
end
class Mock
include PostObjectHiddenFields
end
end
end
end