2015-05-05 01:50:13 -04:00
|
|
|
class StylesheetsController < ApplicationController
|
2017-08-31 00:06:56 -04:00
|
|
|
skip_before_action :preload_json, :redirect_to_login_if_required, :check_xhr, :verify_authenticity_token, only: [:show, :show_source_map]
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
def show_source_map
|
|
|
|
show_resource(source_map: true)
|
|
|
|
end
|
2015-05-05 01:50:13 -04:00
|
|
|
|
|
|
|
def show
|
2018-03-05 23:20:39 -05:00
|
|
|
is_asset_path
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
show_resource
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def show_resource(source_map: false)
|
|
|
|
|
|
|
|
extension = source_map ? ".css.map" : ".css"
|
|
|
|
|
|
|
|
params[:name]
|
2015-05-05 01:50:13 -04:00
|
|
|
|
2015-05-22 02:15:46 -04:00
|
|
|
no_cookies
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
target, digest = params[:name].split(/_([a-f0-9]{40})/)
|
2015-05-21 02:09:23 -04:00
|
|
|
|
2017-07-25 03:29:27 -04:00
|
|
|
if !Rails.env.production?
|
2017-04-12 10:52:52 -04:00
|
|
|
# TODO add theme
|
|
|
|
# calling this method ensures we have a cache for said target
|
|
|
|
# we hold of re-compilation till someone asks for asset
|
|
|
|
if target.include?("theme")
|
2017-07-27 21:20:09 -04:00
|
|
|
split_target, theme_id = target.split(/_(-?[0-9]+)/)
|
2017-04-12 10:52:52 -04:00
|
|
|
theme = Theme.find(theme_id) if theme_id
|
|
|
|
else
|
2017-07-27 21:20:09 -04:00
|
|
|
split_target, color_scheme_id = target.split(/_(-?[0-9]+)/)
|
2017-04-12 10:52:52 -04:00
|
|
|
theme = Theme.find_by(color_scheme_id: color_scheme_id)
|
|
|
|
end
|
|
|
|
Stylesheet::Manager.stylesheet_link_tag(split_target, nil, theme&.key)
|
|
|
|
end
|
|
|
|
|
2015-05-21 03:05:22 -04:00
|
|
|
cache_time = request.env["HTTP_IF_MODIFIED_SINCE"]
|
2018-03-28 04:20:08 -04:00
|
|
|
|
|
|
|
if cache_time
|
|
|
|
begin
|
|
|
|
cache_time = Time.rfc2822(cache_time)
|
|
|
|
rescue ArgumentError
|
|
|
|
end
|
|
|
|
end
|
2015-05-21 03:05:22 -04:00
|
|
|
|
|
|
|
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 : ""
|
2017-04-12 10:52:52 -04:00
|
|
|
location = "#{Rails.root}/#{Stylesheet::Manager::CACHE_PATH}/#{target}#{underscore_digest}#{extension}"
|
2015-05-21 21:21:16 -04:00
|
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
return render body: nil, status: 304
|
2015-05-21 03:05:22 -04:00
|
|
|
end
|
2015-05-05 01:50:13 -04:00
|
|
|
|
|
|
|
unless File.exist?(location)
|
2017-04-12 10:52:52 -04:00
|
|
|
if current = query.limit(1).pluck(source_map ? :source_map : :content).first
|
|
|
|
File.write(location, current)
|
2015-05-05 01:50:13 -04:00
|
|
|
else
|
2015-05-21 21:21:16 -04:00
|
|
|
raise Discourse::NotFound
|
2015-05-05 01:50:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
if Rails.env == "development"
|
|
|
|
response.headers['Last-Modified'] = Time.zone.now.httpdate
|
|
|
|
immutable_for(1.second)
|
|
|
|
else
|
|
|
|
response.headers['Last-Modified'] = stylesheet_time.httpdate if stylesheet_time
|
|
|
|
immutable_for(1.year)
|
|
|
|
end
|
2015-05-05 01:50:13 -04:00
|
|
|
send_file(location, disposition: :inline)
|
2015-05-21 21:21:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_missing_cache(location, name, digest)
|
2017-04-12 10:52:52 -04:00
|
|
|
location = location.sub(".css.map", ".css")
|
|
|
|
source_map_location = location + ".map"
|
2018-03-28 04:20:08 -04:00
|
|
|
existing = read_file(location)
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2015-05-21 21:21:16 -04:00
|
|
|
if existing && digest
|
2018-03-28 04:20:08 -04:00
|
|
|
source_map = read_file(source_map_location)
|
2017-04-12 10:52:52 -04:00
|
|
|
StylesheetCache.add(name, digest, existing, source_map)
|
2015-05-21 21:21:16 -04:00
|
|
|
end
|
2015-05-05 01:50:13 -04:00
|
|
|
end
|
2015-05-21 21:21:16 -04:00
|
|
|
|
2018-03-28 04:20:08 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def read_file(location)
|
|
|
|
begin
|
|
|
|
File.read(location)
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-05 01:50:13 -04:00
|
|
|
end
|