Module: Sinatra::Xsendfile

Defined in:
lib/sinatra/xsendfile.rb,
lib/sinatra/xsendfile/version.rb

Constant Summary collapse

VERSION =
"0.4.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.replace_send_file!Object



33
34
35
36
# File 'lib/sinatra/xsendfile.rb', line 33

def self.replace_send_file!
  Sinatra::Helpers.send(:alias_method, :old_send_file, :send_file)
  Sinatra::Helpers.module_eval("def send_file(path, opts={}); x_send_file(path, opts); end;")
end

Instance Method Details

#x_send_file(path, opts = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sinatra/xsendfile.rb', line 3

def x_send_file(path, opts = {})
  if opts[:type] or not response['Content-Type']
    content_type(opts[:type] || File.extname(path) || 'application/octet-stream')
  end

  if opts[:disposition] == 'attachment' || opts[:filename]
    attachment opts[:filename] || path
  elsif opts[:disposition] == 'inline'
    response['Content-Disposition'] = 'inline'
  end

  header_key = opts[:header] || (settings.respond_to?(:xsf_header) && settings.xsf_header) ||
                                'X-SendFile'

  if header_key == 'X-Accel-Redirect'
    public_folder = if settings.respond_to?(:public_folder)
      settings.public_folder
    else
      settings.public
    end
    path = File.expand_path(path).gsub(public_folder, '')
  end

  response[header_key] = path

  halt
rescue Errno::ENOENT
  not_found
end