discourse/app/controllers/svg_sprite_controller.rb
Alan Guo Xiang Tan 8e3691d537 PERF: Eager load Theme associations in Stylesheet Manager.
Before this change, calling `StyleSheet::Manager.stylesheet_details`
for the first time resulted in multiple queries to the database. This is
because the code was modelled in a way where each `Theme` was loaded
from the database one at a time.

This PR restructures the code such that it allows us to load all the
theme records in a single query. It also allows us to eager load the
required associations upfront. In order to achieve this, I removed the
support of loading multiple themes per request. It was initially added
to support user selectable theme components but the feature was never
completed and abandoned because it wasn't a feature that we thought was
worth building.
2021-06-21 11:06:58 +08:00

90 lines
2.5 KiB
Ruby

# frozen_string_literal: true
class SvgSpriteController < ApplicationController
skip_before_action :preload_json, :redirect_to_login_if_required, :check_xhr, :verify_authenticity_token, only: [:show, :search, :svg_icon]
before_action :apply_cdn_headers, only: [:show, :search, :svg_icon]
requires_login except: [:show, :svg_icon]
def show
no_cookies
RailsMultisite::ConnectionManagement.with_hostname(params[:hostname]) do
theme_id = params[:theme_id].to_i
if SvgSprite.version(theme_id) != params[:version]
return redirect_to path(SvgSprite.path(theme_id))
end
svg_sprite = "window.__svg_sprite = #{SvgSprite.bundle(theme_id).inspect};"
response.headers["Last-Modified"] = 10.years.ago.httpdate
response.headers["Content-Length"] = svg_sprite.bytesize.to_s
immutable_for 1.year
render plain: svg_sprite, disposition: nil, content_type: 'application/javascript'
end
end
def search
RailsMultisite::ConnectionManagement.with_hostname(params[:hostname]) do
keyword = params.require(:keyword)
data = SvgSprite.search(keyword)
if data.blank?
render body: nil, status: 404
else
render plain: data.inspect, disposition: nil, content_type: 'text/plain'
end
end
end
def icon_picker_search
RailsMultisite::ConnectionManagement.with_hostname(params[:hostname]) do
params.permit(:filter)
filter = params[:filter] || ""
icons = SvgSprite.icon_picker_search(filter)
render json: icons.take(200), root: false
end
end
def svg_icon
no_cookies
RailsMultisite::ConnectionManagement.with_hostname(params[:hostname]) do
params.permit(:color)
name = params.require(:name)
icon = SvgSprite.search(name)
if icon.blank?
render body: nil, status: 404
else
doc = Nokogiri.XML(icon)
doc.at_xpath("symbol").name = "svg"
doc.at_xpath("svg")['xmlns'] = "http://www.w3.org/2000/svg"
doc.at_xpath("svg")['fill'] = adjust_hex(params[:color]) if params[:color]
response.headers["Last-Modified"] = 1.years.ago.httpdate
response.headers["Content-Length"] = doc.to_s.bytesize.to_s
immutable_for 1.day
render plain: doc, disposition: nil, content_type: 'image/svg+xml'
end
end
end
private
def adjust_hex(hex)
if hex.size == 3
chars = hex.scan(/\w/)
hex = chars.zip(chars).flatten.join
end
"##{hex}"
end
end