2018-11-26 16:49:57 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class SvgSpriteController < ApplicationController
|
2020-05-14 14:17:19 -04:00
|
|
|
skip_before_action :preload_json,
|
|
|
|
:redirect_to_login_if_required,
|
|
|
|
:check_xhr,
|
|
|
|
:verify_authenticity_token,
|
|
|
|
only: %i[show search svg_icon]
|
2018-11-26 16:49:57 -05:00
|
|
|
|
2021-01-28 21:14:49 -05:00
|
|
|
before_action :apply_cdn_headers, only: %i[show search svg_icon]
|
|
|
|
|
2020-05-14 14:17:19 -04:00
|
|
|
requires_login except: %i[show svg_icon]
|
2018-11-26 16:49:57 -05:00
|
|
|
|
|
|
|
def show
|
|
|
|
no_cookies
|
|
|
|
|
|
|
|
RailsMultisite::ConnectionManagement.with_hostname(params[:hostname]) do
|
2021-06-29 23:04:07 -04:00
|
|
|
theme_id = params[:theme_id].to_i if params[:theme_id].present?
|
2018-11-26 16:49:57 -05:00
|
|
|
|
2021-06-15 02:57:17 -04:00
|
|
|
if SvgSprite.version(theme_id) != params[:version]
|
2022-03-21 10:28:52 -04:00
|
|
|
return redirect_to UrlHelper.absolute((SvgSprite.path(theme_id))), allow_other_host: true
|
2018-11-26 16:49:57 -05:00
|
|
|
end
|
|
|
|
|
2021-06-15 02:57:17 -04:00
|
|
|
svg_sprite = "window.__svg_sprite = #{SvgSprite.bundle(theme_id).inspect};"
|
2018-11-26 16:49:57 -05:00
|
|
|
|
|
|
|
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
|
2019-03-15 02:16:15 -04:00
|
|
|
RailsMultisite::ConnectionManagement.with_hostname(params[:hostname]) do
|
|
|
|
keyword = params.require(:keyword)
|
|
|
|
data = SvgSprite.search(keyword)
|
2018-11-26 16:49:57 -05:00
|
|
|
|
2019-03-15 02:16:15 -04:00
|
|
|
if data.blank?
|
|
|
|
render body: nil, status: 404
|
|
|
|
else
|
|
|
|
render plain: data.inspect, disposition: nil, content_type: "text/plain"
|
|
|
|
end
|
2018-11-26 16:49:57 -05:00
|
|
|
end
|
|
|
|
end
|
2020-02-04 18:41:10 -05:00
|
|
|
|
|
|
|
def icon_picker_search
|
|
|
|
RailsMultisite::ConnectionManagement.with_hostname(params[:hostname]) do
|
2023-02-12 17:24:47 -05:00
|
|
|
params.permit(:filter, :only_available)
|
2020-02-04 18:41:10 -05:00
|
|
|
filter = params[:filter] || ""
|
2023-02-12 17:24:47 -05:00
|
|
|
only_available = params[:only_available]
|
2020-02-04 18:41:10 -05:00
|
|
|
|
2023-02-12 17:24:47 -05:00
|
|
|
icons = SvgSprite.icon_picker_search(filter, only_available)
|
2020-02-04 18:41:10 -05:00
|
|
|
render json: icons.take(200), root: false
|
|
|
|
end
|
|
|
|
end
|
2020-05-14 14:17:19 -04:00
|
|
|
|
|
|
|
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"
|
2020-05-14 16:37:45 -04:00
|
|
|
doc.at_xpath("svg")["fill"] = adjust_hex(params[:color]) if params[:color]
|
2020-05-14 14:17:19 -04:00
|
|
|
|
|
|
|
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
|
2020-05-14 16:37:45 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def adjust_hex(hex)
|
|
|
|
if hex.size == 3
|
|
|
|
chars = hex.scan(/\w/)
|
|
|
|
hex = chars.zip(chars).flatten.join
|
|
|
|
end
|
|
|
|
"##{hex}"
|
|
|
|
end
|
2018-11-26 16:49:57 -05:00
|
|
|
end
|