Class: Pasaporte::Controllers::Assets

Inherits:
R
  • Object
show all
Defined in:
lib/pasaporte.rb

Overview

Catchall for static files, with some caching support

Constant Summary collapse

MIME_TYPES =
{
  'html' => 'text/html', 'css' => 'text/css', 'js' => 'text/javascript', 
  'jpg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png'
}
ASSETS =
File.join File.dirname(Pasaporte::PATH), 'pasaporte/assets/'

Instance Method Summary collapse

Instance Method Details

#get(path) ⇒ Object



625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# File 'lib/pasaporte.rb', line 625

def get(path)
  if env["HTTP_IF_MODIFIED_SINCE"]
    @status = 304
    return 'Not Modified'
  end
  
  # Squeeze out all possible directory traversals
  path = File.join ASSETS, path.gsub(/\.\./, '').gsub(/\s/, '')
  path.squeeze!('/')
  
  # Somehow determine if we support sendfile, because sometimes we dont
  if File.exist?(path)
    ext = File.extname(path).gsub(/^\./, '')
    magic_headers = {
      'Last-Modified' => 2.days.ago.to_s(:http),
      'Expires' => 30.days.from_now.to_s(:http),
      'Cache-Control' => "public; max-age=#{360.hours}",
      'Last-Modified' => 2.hours.ago.to_s(:http),
    }
    @headers.merge!(magic_headers)
    @headers['Content-Type'] = MIME_TYPES[ext] || "text/plain"
    @headers['X-Sendfile'] = path
  else
    @status = 404
    self << "No such item"
  end
end