Class: Boar::Handlers::Downloads::BoxNet

Inherits:
Base
  • Object
show all
Defined in:
app/models/boar/handlers/downloads/box_net.rb

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) ⇒ BoxNet

Returns a new instance of BoxNet.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/boar/handlers/downloads/box_net.rb', line 11

def initialize(service, options)
  super(service, options)
  configuration = self.load_credentials

  # Open the session and refresh the token
  @session = RubyBox::Session.new({client_id: configuration[:client_id], client_secret: configuration[:client_secret], access_token: configuration[:access_token]})
  token = @session.refresh_token(configuration[:refresh_token])

  # Save the token and schedule an update to avoid expiration
  self.update_credentials({access_token: token.token, refresh_token: token.refresh_token})
  # TODO: Reschedule token refreshing after 13 days

  @client = RubyBox::Client.new(@session)
end

Instance Method Details

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



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
51
# File 'app/models/boar/handlers/downloads/box_net.rb', line 26

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

  if skip_cache || url.blank? then
    begin
      # Get the file
      file = @client.file(entry[:path])
      raise Boar::Exceptions::NotFound if file.nil?

      # Create the shared link
      shared_link = create_shared_link(file)
      raise Boar::Exceptions::NotFound if shared_link.nil?
      url = entry["disposition"] == "inline" ? shared_link["url"] : shared_link["download_url"]

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

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