Class: Boar::Handlers::Downloads::S3

Inherits:
Base show all
Defined in:
app/models/boar/handlers/downloads/s3.rb

Overview

noinspection RubyClassModuleNamingConvention

Constant Summary collapse

URL_AGE =
604800

Instance Attribute Summary

Attributes inherited from Base

#options, #provider_options

Attributes inherited from Generic

#configuration, #service

Instance Method Summary collapse

Methods inherited from Base

#class_key, #credentials_params, #finalize_path, #load_credentials, #update_credentials

Methods included from Utils::Basic

#ensure_hash, #get_option, #interpolate

Constructor Details

#initialize(service, options) ⇒ S3

Returns a new instance of S3.



14
15
16
17
18
19
# File 'app/models/boar/handlers/downloads/s3.rb', line 14

def initialize(service, options)
  super(service, options)

  configuration = self.load_credentials
  @s3_configuration = {access_key_id: configuration[:access_key], secret_access_key: configuration[:access_secret], region: configuration[:region]}
end

Instance Method Details

#call(path, entry, skip_cache, _, _) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/boar/handlers/downloads/s3.rb', line 21

def call(path, entry, skip_cache, _, _)
  # Read the URL from Redis
  key = @configuration.backend_key("downloads:s3[#{path}]", self, @service.controller.request)
  url = @configuration.backend.get(key)

  if skip_cache || url.blank? then
    begin
      # Update the region
      @s3_configuration[:region] = entry[:region] if entry[:region].present?

      # Get the object
      object = AWS::S3.new(@s3_configuration).buckets[entry[:bucket]].objects[entry[:key]]
      raise ArgumentError if !object.exists?

      # Get the public URL
      url = object.url_for(:read, response_content_disposition: entry[:disposition], expire: Boar::Handlers::Downloads::S3::URL_AGE).to_s

      # Save the URL
      if url.present? then
        @configuration.backend.set(key, url)
        @configuration.backend.expire(key, Boar::Handlers::Downloads::S3::URL_AGE)
      end
    rescue => e
      raise Boar::Exceptions::ServerError.new("[#{e.class}] #{e.message}")
    end
  end

  # Redirect to the final URL
  @service.controller.redirect_to(url)
end