2015-05-05 01:50:13 -04:00
|
|
|
class StylesheetsController < ApplicationController
|
|
|
|
skip_before_filter :preload_json, :redirect_to_login_if_required, :check_xhr, :verify_authenticity_token, only: [:show]
|
|
|
|
|
|
|
|
def show
|
|
|
|
|
2015-05-22 02:15:46 -04:00
|
|
|
no_cookies
|
|
|
|
|
2015-05-23 01:25:05 -04:00
|
|
|
target,digest = params[:name].split(/_([a-f0-9]{40})/)
|
2015-05-21 02:09:23 -04:00
|
|
|
|
2015-05-21 03:05:22 -04:00
|
|
|
cache_time = request.env["HTTP_IF_MODIFIED_SINCE"]
|
|
|
|
cache_time = Time.rfc2822(cache_time) rescue nil if cache_time
|
|
|
|
|
|
|
|
query = StylesheetCache.where(target: target)
|
|
|
|
if digest
|
2015-05-21 21:21:16 -04:00
|
|
|
query = query.where(digest: digest)
|
2015-05-21 03:05:22 -04:00
|
|
|
else
|
|
|
|
query = query.order('id desc')
|
2015-05-21 02:09:23 -04:00
|
|
|
end
|
|
|
|
|
2015-05-21 21:21:16 -04:00
|
|
|
# Security note, safe due to route constraint
|
|
|
|
underscore_digest = digest ? "_" + digest : ""
|
|
|
|
location = "#{Rails.root}/#{DiscourseStylesheets::CACHE_PATH}/#{target}#{underscore_digest}.css"
|
|
|
|
|
2015-05-21 03:05:22 -04:00
|
|
|
stylesheet_time = query.pluck(:created_at).first
|
2015-05-21 21:21:16 -04:00
|
|
|
|
2015-05-21 03:05:22 -04:00
|
|
|
if !stylesheet_time
|
2015-05-21 21:21:16 -04:00
|
|
|
handle_missing_cache(location, target, digest)
|
2015-05-21 03:05:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if cache_time && stylesheet_time && stylesheet_time <= cache_time
|
|
|
|
return render nothing: true, status: 304
|
|
|
|
end
|
2015-05-05 01:50:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
unless File.exist?(location)
|
|
|
|
if current = query.first
|
|
|
|
File.write(location, current.content)
|
|
|
|
else
|
2015-05-21 21:21:16 -04:00
|
|
|
raise Discourse::NotFound
|
2015-05-05 01:50:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-21 21:21:16 -04:00
|
|
|
response.headers['Last-Modified'] = stylesheet_time.httpdate if stylesheet_time
|
2017-02-23 13:05:00 -05:00
|
|
|
immutable_for(1.year) unless Rails.env == "development"
|
2015-05-05 01:50:13 -04:00
|
|
|
send_file(location, disposition: :inline)
|
2015-05-21 21:21:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
2015-05-05 01:50:13 -04:00
|
|
|
|
2015-05-21 21:21:16 -04:00
|
|
|
def handle_missing_cache(location, name, digest)
|
|
|
|
existing = File.read(location) rescue nil
|
|
|
|
if existing && digest
|
|
|
|
StylesheetCache.add(name, digest, existing)
|
|
|
|
end
|
2015-05-05 01:50:13 -04:00
|
|
|
end
|
2015-05-21 21:21:16 -04:00
|
|
|
|
2015-05-05 01:50:13 -04:00
|
|
|
end
|
|
|
|
|