lib/kiss/static_file.rb in kiss-1.1 vs lib/kiss/static_file.rb in kiss-1.7
- old
+ new
@@ -1,28 +1,35 @@
class Kiss
class StaticFile
- def initialize(path,mime_type = nil)
- @path = path
- @mime_type = mime_type
+ def initialize(path, options = {})
+ @_path = path
+ @_options = options
end
def finish
- ext = File.extname(@path)[1..-1]
+ ext = File.extname(@_path)[1..-1]
- if File.file?(@path) && File.readable?(@path)
- [200, {
- "Last-Modified" => File.mtime(@path).rfc822,
- "Content-Type" => @mime_type || Kiss.mime_type(ext) || "text/plain",
- "Content-Length" => File.size(@path).to_s
- }, self]
+ 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
- return [404, {"Content-Type" => "text/plain"},
- ["File not found: #{@path}\n"]]
+ raise "file not found: #{@_path}"
end
end
def each
- File.open(@path, "rb") { |file|
+ File.open(@_path, "rb") { |file|
while part = file.read(8192)
yield part
end
}
end
\ No newline at end of file