Class: Anaconda::UploadHelper::S3Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/anaconda/upload_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ S3Uploader

Returns a new instance of S3Uploader.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/anaconda/upload_helper.rb', line 24

def initialize(options)
  @options = options.reverse_merge(
    id: "fileupload",
    aws_access_key_id: Anaconda.aws[:aws_access_key],
    aws_secret_access_key: Anaconda.aws[:aws_secret_key],
    bucket: Anaconda.aws[:aws_bucket],
    acl: "public-read",
    expiration: 10.hours.from_now,
    max_file_size: 500.megabytes,
    as: "file"
  )
end

Instance Method Details

#base_keyObject



65
66
67
# File 'lib/anaconda/upload_helper.rb', line 65

def base_key
  @options[:base_key]
end

#fieldsObject



51
52
53
54
55
56
57
58
59
# File 'lib/anaconda/upload_helper.rb', line 51

def fields
  {
    :key => key,
    :acl => @options[:acl],
    :policy => policy,
    :signature => signature,
    "AWSAccessKeyId" => @options[:aws_access_key_id],
  }
end

#form_optionsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/anaconda/upload_helper.rb', line 37

def form_options
  {
    id: @options[:id],
    method: "post",
    authenticity_token: false,
    multipart: true,
    data: {
      post: @options[:post],
      as: @options[:as],
      base_key: base_key
    }
  }
end

#keyObject



61
62
63
# File 'lib/anaconda/upload_helper.rb', line 61

def key
  @key ||= "#{base_key}/${filename}"
end

#policyObject



73
74
75
# File 'lib/anaconda/upload_helper.rb', line 73

def policy
  Base64.encode64(policy_data.to_json).gsub("\n", "")
end

#policy_dataObject



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/anaconda/upload_helper.rb', line 77

def policy_data
  {
    expiration: @options[:expiration],
    conditions: [
      ["starts-with", "$utf8", ""],
      ["starts-with", "$key", base_key],
      ["content-length-range", 1, @options[:max_file_size]],
      {bucket: @options[:bucket]},
      {acl: @options[:acl]}
    ]
  }
end

#signatureObject



90
91
92
93
94
95
96
97
# File 'lib/anaconda/upload_helper.rb', line 90

def signature
  Base64.encode64(
    OpenSSL::HMAC.digest(
      OpenSSL::Digest::Digest.new('sha1'),
      @options[:aws_secret_access_key], policy
    )
  ).gsub("\n", "")
end

#urlObject



69
70
71
# File 'lib/anaconda/upload_helper.rb', line 69

def url
  "https://s3.amazonaws.com/#{@options[:bucket]}/"
end