Method: Paperclip::Storage::S3.extended

Defined in:
lib/paperclip/storage/s3.rb

.extended(base) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/paperclip/storage/s3.rb', line 124

def self.extended base
  begin
    require "aws-sdk-s3"
  rescue LoadError => e
    e.message << " (You may need to install the aws-sdk-s3 gem)"
    raise e
  end

  base.instance_eval do
    @s3_options     = @options[:s3_options]     || {}
    @s3_permissions = set_permissions(@options[:s3_permissions])
    @s3_protocol    = @options[:s3_protocol] || "".freeze
     = @options[:s3_metadata] || {}
    @s3_headers = {}
    merge_s3_headers(@options[:s3_headers], @s3_headers, )

    @s3_storage_class = set_storage_class(@options[:s3_storage_class])

    @s3_server_side_encryption = "AES256"
    if @options[:s3_server_side_encryption].blank?
      @s3_server_side_encryption = false
    end
    if @s3_server_side_encryption
      @s3_server_side_encryption = @options[:s3_server_side_encryption]
    end

    unless @options[:url].to_s.match(/\A:s3.*url\z/) || @options[:url] == ":asset_host".freeze
      @options[:path] = path_option.gsub(/:url/, @options[:url]).sub(/\A:rails_root\/public\/system/, "".freeze)
      @options[:url]  = ":s3_path_url".freeze
    end
    @options[:url] = @options[:url].inspect if @options[:url].is_a?(Symbol)

    @http_proxy = @options[:http_proxy] || nil

    @use_accelerate_endpoint = @options[:use_accelerate_endpoint]
  end

  Paperclip.interpolates(:s3_alias_url) do |attachment, style|
    protocol = attachment.s3_protocol(style, true)
    host = attachment.s3_host_alias
    path = attachment.path(style).
      split("/")[attachment.s3_prefixes_in_alias..-1].
      join("/").
      sub(%r{\A/}, "".freeze)
    "#{protocol}//#{host}/#{path}"
  end unless Paperclip::Interpolations.respond_to? :s3_alias_url
  Paperclip.interpolates(:s3_path_url) do |attachment, style|
    "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).sub(%r{\A/}, "".freeze)}"
  end unless Paperclip::Interpolations.respond_to? :s3_path_url
  Paperclip.interpolates(:s3_domain_url) do |attachment, style|
    "#{attachment.s3_protocol(style, true)}//#{attachment.bucket_name}.#{attachment.s3_host_name}/#{attachment.path(style).sub(%r{\A/}, "".freeze)}"
  end unless Paperclip::Interpolations.respond_to? :s3_domain_url
  Paperclip.interpolates(:asset_host) do |attachment, style|
    "#{attachment.path(style).sub(%r{\A/}, "".freeze)}"
  end unless Paperclip::Interpolations.respond_to? :asset_host
end