forked from zedalaye/theme_support
/
theme_controller.rb
executable file
·69 lines (56 loc) · 1.49 KB
/
theme_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# The controller for serving/cacheing theme content...
#
class ThemeController < ActionController::Base
after_filter :cache_theme_files
def stylesheets
render_theme_item(:stylesheets, joined_filename, params[:theme], 'text/css')
end
def javascript
render_theme_item(:javascript, joined_filename, params[:theme], 'text/javascript')
end
def images
render_theme_item(:images, joined_filename, params[:theme])
end
def error
render :nothing => true, :status => 404
end
private
def render_theme_item(type, file, theme, mime = mime_for(file))
file_path = "#{Theme.path_to_theme(theme)}/#{type}/#{file}"
if file.split(%r{[\\/]}).include?("..") || !File.exists?(file_path) || file.blank?
render :text => "Not Found", :status => 404
return
else
send_file file_path, :type => mime, :disposition => 'inline', :stream => false
end
end
def joined_filename
params[:filename].join '/'
end
def cache_theme_files
path = request.request_uri
begin
ThemeController.cache_page( response.body, path )
rescue
STERR.puts "Cache Exception: #{$!}"
end
end
def mime_for(filename)
case filename.downcase
when /\.js$/
'text/javascript'
when /\.css$/
'text/css'
when /\.gif$/
'image/gif'
when /(\.jpg|\.jpeg)$/
'image/jpeg'
when /\.png$/
'image/png'
when /\.swf$/
'application/x-shockwave-flash'
else
'application/binary'
end
end
end