Module: Ext::Sendfile::Base

Defined in:
lib/ext/sendfile.rb

Instance Method Summary collapse

Instance Method Details

#sendfile(path, content_type = nil) ⇒ Object

Sends the file passed in the path to the browser.

If content_type is given, it won’t try to lookup in the MIME::Types library.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ext/sendfile.rb', line 56

def sendfile(path, content_type = nil)
  # Kind of check if the file exists using the rescue
  file = File.open(path.to_s)

  unless content_type
    mime = MIME::Types.type_for(path).first || MIME::Types['text/plain'].first
    @headers['Content-Type']     = mime.to_s
    @headers['Content-Encoding'] = mime.encoding
  else
    @headers['Content-Type']     = content_type
  end

  # Sendfile is used ?
  if $mongrel_has_sendfile
    @headers['X-SENDFILE'] = path
  else
    @body = file.read
  end

rescue Errno::ENOENT, Errno::EISDIR # File not found
  forward app::Controllers::NotFound, 'get', @env.PATH_INFO
ensure
  file.close if file
end