Class: Boar::Handlers::Downloads::Local

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

Returns a new instance of Local.



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

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

  # Normalize the root
  @root = self.get_option(options, :directory, Rails.application.config.boar.downloads_directory)
  @root = self.interpolate(@root, {root: Rails.root, request: @service.controller.request, controller: @controller})
end

Instance Method Details

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



19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/boar/handlers/downloads/local.rb', line 19

def call(path, entry, _, _, _)
  # Get the full path
  file = Pathname.new(@root) + path
  raise Boar::Exceptions::NotFound.new(path) if !file.exist?

  # Check MIME and whether the disposition is inline or not
  type = MIME::Types.type_for(file.to_s).first.content_type
  disposition = (entry[:disposition].ensure_string == "inline") ? "inline" : "attachment"

  # Serve the file
  @service.download_file(file, disposition: disposition, type: type)
end