Class: Boar::Handlers::Downloads::Dropbox

Inherits:
Base
  • Object
show all
Defined in:
app/models/boar/handlers/downloads/dropbox.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) ⇒ Dropbox

Returns a new instance of Dropbox.



11
12
13
14
15
16
17
# File 'app/models/boar/handlers/downloads/dropbox.rb', line 11

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

  @session = ::DropboxSession.deserialize(load_credentials[:session])
  @root = @provider_options.fetch(:root, "dropbox").to_sym
  @client = ::DropboxClient.new(@session, @root)
end

Instance Method Details

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



19
20
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
# File 'app/models/boar/handlers/downloads/dropbox.rb', line 19

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

  if skip_cache || url.blank? then
    begin
      attachment = entry[:disposition] != "inline"
      share =  attachment ? self.direct_shares(entry[:path]) : @client.shares(entry[:path])

      url = share["url"] + (attachment ? "?dl=1" : "")
      expire = DateTime.parse(share["expires"], "%a, %d %b %Y %T %Z")

      # Save the URL
      if url.present? then
        @configuration.backend.set(key, url)
        @configuration.backend.expire(key, expire.to_i)
      end
    rescue => e
      if e.message =~ /Path .+ not found/ then
        raise Boar::Exceptions::NotFound.new(path)
      else
        raise e
        raise Boar::Exceptions::ServerError.new("[#{e.class}] #{e.message}")
      end
    end
  end

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

#direct_shares(path) ⇒ Object



51
52
53
54
55
# File 'app/models/boar/handlers/downloads/dropbox.rb', line 51

def direct_shares(path)
  # Get the public URL - We don't use Dropbox shares method directly because we have to pass a parameter
  response = @session.do_get@client.build_url("/shares/#{@root}#{@client.format_path(path)}?short_url=false")
  @client.parse_response(response)
end