Class: FroalaEditorSDK::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/froala-editor-sdk/s3.rb

Overview

Uploads files to S3/AWS

Class Method Summary collapse

Class Method Details

.data_hash(options = nil) ⇒ Object

Makes all the request in order and returns AWS hash response Params:

options

Configuration params to generate the AWS response.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/froala-editor-sdk/s3.rb', line 74

def self.data_hash (options = nil)
  options[:region] = 'us-east-1' if options[:region].nil? ||  options[:region] == 's3'
  options[:'date-string'] = Time.now.strftime("%Y%m%d")
  options[:'x-amz-algorithm'] = "AWS4-HMAC-SHA256"
  options[:'x-amz-credential'] = self.getXamzCredential(options)
  options[:'x-amz-date'] = options[:'date-string'] + "T000000Z"

  {
    :bucket => options[:bucket],           # Upload bucket
    :region =>  options[:region] != 'us-east-1' ? "s3-#{options[:region]}" : 's3', # Upload region
    :keyStart => options[:keyStart],       # Start key/folder
    :params => {
      :acl => options[:acl],                 # ACL property 'public-read'
      :policy => self.policy(options),       # Defined policy
      :'x-amz-algorithm' => options[:'x-amz-algorithm'], # Encrytion Algorithm
      :'x-amz-credential' => options[:'x-amz-credential'],     # Encrypted Credentials
      :'x-amz-date' => options[:'x-amz-date'],  # Current Date
      :'x-amz-signature' => self.signature(options), # Defined signature
    }
  }
end

.getXamzCredential(options = nil) ⇒ Object

Builds the amazon credential by appending access key, x-amz-date, region and ‘s3/aws4_request’ Params:

options

Configuration params to generate the AWS response



67
68
69
# File 'lib/froala-editor-sdk/s3.rb', line 67

def self.getXamzCredential(options = nil)
  "#{options[:accessKey]}#{"/"}#{options[:'date-string']}#{"/"}#{options[:region]}#{"/"}#{"s3/aws4_request"}"
end

.policy(options = nil) ⇒ Object

Encodes to Base64 the policy data and replaces new lines chars. Params:

options

The configuration params that are needed to compute the signature.



40
41
42
# File 'lib/froala-editor-sdk/s3.rb', line 40

def self.policy (options = nil)
  Base64.encode64(self.policy_data(options).to_json).gsub("\n", "")
end

.policy_data(options = nil) ⇒ Object

Sets policy params, bucket that will be used max file size and other params. Params:

options

Configuration params that are needed to set the policy



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/froala-editor-sdk/s3.rb', line 47

def self.policy_data (options = nil)
  {
      expiration: 10.hours.from_now.utc.iso8601,
      conditions: [
          ["starts-with", "$key", options[:keyStart]],       # Start key/folder
          ["starts-with", "$Content-type", ""],         # Content type
          {"x-requested-with": "xhr"},                  # Request type
          {"x-amz-algorithm": options[:'x-amz-algorithm']}, # Encrytion Algorithm
          {"x-amz-date": options[:'x-amz-date']},       # Current Date
          {"x-amz-credential": options[:'x-amz-credential']}, # Encrypted Credentials
          {bucket: options[:bucket]},                   # Bucket name
          {acl: options[:acl]},                         # ACL property
          {success_action_status: "201"}                # Response status 201 'file created'
      ]
  }
end

.sign(key, data) ⇒ Object

Builds a HMAC-SHA256 digest using key and data Params:

key

Key to use for creating the digest

data

Data to be used for creating the digest



33
34
35
# File 'lib/froala-editor-sdk/s3.rb', line 33

def self.sign(key, data)
  OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, key, data.force_encoding(Encoding::UTF_8))
end

.signature(options = nil) ⇒ Object

Builds a signature based on the options. Params:

options

The configuration params that are needed to compute the signature.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/froala-editor-sdk/s3.rb', line 9

def self.signature (options = nil)
  OpenSSL::HMAC.hexdigest(
    "SHA256",
    self.sign(
      self.sign(
        self.sign(
          self.sign(
            ("AWS4" + options[:secretKey]).force_encoding(Encoding::UTF_8),
            options[:'date-string']
          ),
          options[:region]
        ),
        "s3"
      ),
      "aws4_request"
    ), 
    self.policy(options)
  )
end