forked from refinery/refinerycms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme_server.rb
36 lines (31 loc) · 1.15 KB
/
theme_server.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
# Allow the metal piece to run in isolation
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
# Serves theme files from the theme directory without touching Rails too much
class ThemeServer
def initialize(app)
@app = app
end
def call(env)
if env["PATH_INFO"] =~ /^\/theme/
relative_path = env["PATH_INFO"].gsub(/^\/theme\//, '')
if (file_path = Rails.root.join("themes", RefinerySetting[:theme], relative_path)).exist?
# generate an etag for client-side caching.
etag = Digest::MD5.hexdigest("#{file_path.to_s}#{file_path.mtime}")
unless (env["HTTP_IF_NONE_MATCH"] == etag and RefinerySetting.find_or_set(:themes_use_etags, false) == true)
[200, {
"Content-Type" => Rack::Mime.mime_type(file_path.extname),
"Cache-Control" => "public",
"ETag" => etag
}, file_path.open]
else
[304, {}, []]
end
else
[404, {"Content-Type" => "text/html"}, ["Not Found"]]
end
else
status, headers, response = @app.call(env)
[status, headers, response]
end
end
end