Module: Capcode::Helpers

Defined in:
lib/capcode/render/static.rb

Instance Method Summary collapse

Instance Method Details

#render_static(f, opts = {}) ⇒ Object

:nodoc:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/capcode/render/static.rb', line 7

def render_static( f, opts = {} ) #:nodoc:
  # Update options
  opts = { :exact_path => true }.merge(opts)
  opts = (Capcode::Configuration.options[:static] || {}).merge(opts)
  
  # Path with ".." not allowed
  if Capcode.static.nil? or f.include? '..'
    return [403, {}, '403 - Invalid path']
  end
  
  # Update Content-Type
  if opts.keys.include?(:content_type)
    @response['Content-Type'] = opts[:content_type]
  else
    @response['Content-Type'] = MIME::Types.type_for(File.join( static[:path], URI.parse( f ).path ))[0].content_type
  end
  
  if !opts.keys.include?(:exact_path) or opts[:exact_path] == true
    redirect File.join( static[:uri], f )
  else
    File.read( File.join( static[:path], f ) ).to_s
  end
end