module Roda::RodaPlugins::SendFile::InstanceMethods

  1. lib/roda/plugins/send_file.rb

Methods

Public Instance

  1. send_file

Public Instance methods

send_file(path, opts = OPTS)

Use the contents of the file at path as the response body. See plugin documentation for options.

[show source]
    # File lib/roda/plugins/send_file.rb
 71 def send_file(path, opts = OPTS)
 72   r = @_request
 73   res = @_response
 74   headers = res.headers
 75   if (type = opts[:type]) || !headers[RodaResponseHeaders::CONTENT_TYPE]
 76     type_str = type.to_s
 77 
 78     if type_str.include?('/')
 79       type = type_str
 80     else
 81       if type
 82         type = ".#{type}" unless type_str.start_with?(".")
 83       else
 84         type = ::File.extname(path)
 85       end
 86 
 87       type &&= Rack::Mime.mime_type(type, nil)
 88       type ||= 'application/octet-stream'
 89     end
 90     
 91     headers[RodaResponseHeaders::CONTENT_TYPE] = type
 92   end
 93 
 94   disposition = opts[:disposition]
 95   filename    = opts[:filename]
 96   if disposition || filename
 97     disposition ||= 'attachment'
 98     filename = path if filename.nil?
 99     res.attachment(filename, disposition)
100   end
101 
102   if lm = opts[:last_modified]
103     r.last_modified(lm)
104   end
105 
106   file = RACK_FILES.new nil
107   s, h, b = if Rack.release > '2'
108     file.serving(r, path)
109   else
110     file.path = path
111     file.serving(env)
112   end
113 
114   res.status = opts[:status] || s
115   headers.delete(RodaResponseHeaders::CONTENT_LENGTH)
116   headers.replace(h.merge!(headers))
117   r.halt res.finish_with_body(b)
118 rescue Errno::ENOENT
119   response.status = 404
120   r.halt
121 end