Class: Boar::Handlers::Downloads::GoogleDrive

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

Returns a new instance of GoogleDrive.



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

def initialize(service, options)
  super(service, options)
  configuration = self.load_credentials
  @client = Boar::Providers::GoogleDrive.authorized_client(configuration)
  @api = @client.discovered_api('drive', 'v2')
end

Instance Method Details

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



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

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

  if skip_cache || url.blank? then
    begin
      # Get the file
      result = @client.execute(api_method: @api.files.get, parameters: {"fileId" => entry[:id]})
      raise Boar::Exceptions::NotFound if result.status == 404

      # Get the share link
      file = result.data
      url = file.web_content_link || file.alternate_link
      url = file.alternate_link if entry["disposition"] == "inline"
      raise Boar::Exceptions::NotFound if url.blank?

      # 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