Method: Roda::RodaPlugins::SinatraHelpers::RequestMethods#send_file

Defined in:
lib/roda/plugins/sinatra_helpers.rb

#send_file(path, opts = OPTS) ⇒ Object

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



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/roda/plugins/sinatra_helpers.rb', line 323

def send_file(path, opts = OPTS)
  res = response
  headers = res.headers
  if opts[:type] || !headers[CONTENT_TYPE]
    res.content_type(opts[:type] || ::File.extname(path), :default => OCTET_STREAM)
  end

  disposition = opts[:disposition]
  filename    = opts[:filename]
  if disposition || filename
    disposition ||= ATTACHMENT
    filename = path if filename.nil?
    res.attachment(filename, disposition)
  end

  if lm = opts[:last_modified]
    last_modified(lm)
  end

  file      = ::Rack::File.new nil
  file.path = path
  s, h, b   = file.serving(@env)

  res.status = opts[:status] || s
  headers.delete(CONTENT_LENGTH)
  headers.replace(h.merge!(headers))
  res.body = b

  halt
rescue Errno::ENOENT
  not_found
end