Module: Flame::Dispatcher::Static

Included in:
Flame::Dispatcher
Defined in:
lib/flame/static.rb

Overview

Module for working with static files

Instance Method Summary collapse

Instance Method Details

#return_static(file) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/flame/static.rb', line 17

def return_static(file)
	file_time = File.mtime(file)
	halt 304 if static_cached?(file_time)
	mime_type = Rack::Mime.mime_type(File.extname(file))
	response.headers.merge!(
		'Content-Type' => mime_type,
		'Last-Modified' => file_time.httpdate
		# 'Content-Disposition' => 'attachment;' \
		#	"filename=\"#{File.basename(static_file)}\"",
		# 'Content-Length' => File.size?(static_file).to_s
	)
	halt 200, File.read(file)
end

#static_cached?(file_time) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/flame/static.rb', line 12

def static_cached?(file_time)
	since = request.env['HTTP_IF_MODIFIED_SINCE']
	since && Time.httpdate(since).to_i >= file_time.to_i
end

#try_static(dir = config[:public_dir]) ⇒ Object

Find static files and try return it



6
7
8
9
10
# File 'lib/flame/static.rb', line 6

def try_static(dir = config[:public_dir])
	file = File.join(dir, request.path_info)
	return nil unless File.exist?(file) && File.file?(file)
	return_static(file)
end