2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-05-05 13:50:13 +08:00
|
|
|
class StylesheetsController < ApplicationController
|
2017-08-31 12:06:56 +08:00
|
|
|
skip_before_action :preload_json, :redirect_to_login_if_required, :check_xhr, :verify_authenticity_token, only: [:show, :show_source_map]
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
def show_source_map
|
|
|
|
show_resource(source_map: true)
|
|
|
|
end
|
2015-05-05 13:50:13 +08:00
|
|
|
|
|
|
|
def show
|
2018-03-06 12:20:39 +08:00
|
|
|
is_asset_path
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
show_resource
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def show_resource(source_map: false)
|
|
|
|
|
|
|
|
extension = source_map ? ".css.map" : ".css"
|
|
|
|
|
|
|
|
params[:name]
|
2015-05-05 13:50:13 +08:00
|
|
|
|
2015-05-22 14:15:46 +08:00
|
|
|
no_cookies
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
target, digest = params[:name].split(/_([a-f0-9]{40})/)
|
2015-05-21 14:09:23 +08:00
|
|
|
|
2017-07-25 15:29:27 +08:00
|
|
|
if !Rails.env.production?
|
2017-04-12 22:52:52 +08: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-28 09:20:09 +08:00
|
|
|
split_target, theme_id = target.split(/_(-?[0-9]+)/)
|
2018-08-08 12:46:34 +08:00
|
|
|
theme = Theme.find_by(id: theme_id) if theme_id.present?
|
2017-04-12 22:52:52 +08:00
|
|
|
else
|
2017-07-28 09:20:09 +08:00
|
|
|
split_target, color_scheme_id = target.split(/_(-?[0-9]+)/)
|
2017-04-12 22:52:52 +08:00
|
|
|
theme = Theme.find_by(color_scheme_id: color_scheme_id)
|
|
|
|
end
|
2018-07-12 12:18:21 +08:00
|
|
|
Stylesheet::Manager.stylesheet_link_tag(split_target, nil, theme&.id)
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2015-05-21 15:05:22 +08:00
|
|
|
cache_time = request.env["HTTP_IF_MODIFIED_SINCE"]
|
2018-03-28 16:20:08 +08:00
|
|
|
|
|
|
|
if cache_time
|
|
|
|
begin
|
|
|
|
cache_time = Time.rfc2822(cache_time)
|
|
|
|
rescue ArgumentError
|
|
|
|
end
|
|
|
|
end
|
2015-05-21 15:05:22 +08:00
|
|
|
|
|
|
|
query = StylesheetCache.where(target: target)
|
|
|
|
if digest
|
2015-05-22 09:21:16 +08:00
|
|
|
query = query.where(digest: digest)
|
2015-05-21 15:05:22 +08:00
|
|
|
else
|
|
|
|
query = query.order('id desc')
|
2015-05-21 14:09:23 +08:00
|
|
|
end
|
|
|
|
|
2015-05-22 09:21:16 +08:00
|
|
|
# Security note, safe due to route constraint
|
|
|
|
underscore_digest = digest ? "_" + digest : ""
|
2019-08-29 15:27:07 +08:00
|
|
|
|
|
|
|
cache_path = "#{Rails.root}/#{Stylesheet::Manager::CACHE_PATH}"
|
|
|
|
location = "#{cache_path}/#{target}#{underscore_digest}#{extension}"
|
2015-05-22 09:21:16 +08:00
|
|
|
|
2015-05-21 15:05:22 +08:00
|
|
|
stylesheet_time = query.pluck(:created_at).first
|
2015-05-22 09:21:16 +08:00
|
|
|
|
2015-05-21 15:05:22 +08:00
|
|
|
if !stylesheet_time
|
2015-05-22 09:21:16 +08:00
|
|
|
handle_missing_cache(location, target, digest)
|
2015-05-21 15:05:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if cache_time && stylesheet_time && stylesheet_time <= cache_time
|
2017-08-31 12:06:56 +08:00
|
|
|
return render body: nil, status: 304
|
2015-05-21 15:05:22 +08:00
|
|
|
end
|
2015-05-05 13:50:13 +08:00
|
|
|
|
|
|
|
unless File.exist?(location)
|
2017-04-12 22:52:52 +08:00
|
|
|
if current = query.limit(1).pluck(source_map ? :source_map : :content).first
|
2019-08-29 15:27:07 +08:00
|
|
|
FileUtils.mkdir_p(cache_path)
|
2017-04-12 22:52:52 +08:00
|
|
|
File.write(location, current)
|
2015-05-05 13:50:13 +08:00
|
|
|
else
|
2015-05-22 09:21:16 +08:00
|
|
|
raise Discourse::NotFound
|
2015-05-05 13:50:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08: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 13:50:13 +08:00
|
|
|
send_file(location, disposition: :inline)
|
2015-05-22 09:21:16 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_missing_cache(location, name, digest)
|
2017-04-12 22:52:52 +08:00
|
|
|
location = location.sub(".css.map", ".css")
|
|
|
|
source_map_location = location + ".map"
|
2018-03-28 16:20:08 +08:00
|
|
|
existing = read_file(location)
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2015-05-22 09:21:16 +08:00
|
|
|
if existing && digest
|
2018-03-28 16:20:08 +08:00
|
|
|
source_map = read_file(source_map_location)
|
2017-04-12 22:52:52 +08:00
|
|
|
StylesheetCache.add(name, digest, existing, source_map)
|
2015-05-22 09:21:16 +08:00
|
|
|
end
|
2015-05-05 13:50:13 +08:00
|
|
|
end
|
2015-05-22 09:21:16 +08:00
|
|
|
|
2018-03-28 16:20:08 +08:00
|
|
|
private
|
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def read_file(location)
|
|
|
|
begin
|
|
|
|
File.read(location)
|
|
|
|
rescue Errno::ENOENT
|
2018-03-28 16:20:08 +08:00
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2018-03-28 16:20:08 +08:00
|
|
|
|
2015-05-05 13:50:13 +08:00
|
|
|
end
|