Class: Kiss::StaticFile

Inherits:
Object show all
Defined in:
lib/kiss/static_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ StaticFile

Returns a new instance of StaticFile.



3
4
5
6
# File 'lib/kiss/static_file.rb', line 3

def initialize(path, options = {})
  @_path = path
  @_options = options
end

Instance Method Details

#eachObject



30
31
32
33
34
35
36
# File 'lib/kiss/static_file.rb', line 30

def each
  File.open(@_path, "rb") { |file|
    while part = file.read(8192)
      yield part
    end
  }
end

#finishObject



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

def finish
  ext = File.extname(@_path)[1..-1]
  
  headers = {
    "Last-Modified"  => File.mtime(@_path).rfc822,
    "Content-Type"   => @_options[:type] || Kiss.mime_type(ext) || "text/plain",
    "Content-Length" => File.size(@_path).to_s
  }
  
  if @_options[:filename]
    headers['Content-Disposition'] = "#{@_options[:disposition] || 'inline'}; filename=#{@_options[:filename]}"
  elsif @_options[:disposition]
    headers['Content-Disposition'] = @_options[:disposition]
  end
  
  if File.file?(@_path) && File.readable?(@_path)
    [200, headers, self]
  else
    raise "file not found: #{@_path}"
  end
end