Class: SvgSpriteController

Inherits:
ApplicationController show all
Defined in:
app/controllers/svg_sprite_controller.rb

Constant Summary

Constants inherited from ApplicationController

ApplicationController::LEGACY_NO_THEMES, ApplicationController::LEGACY_NO_UNOFFICIAL_PLUGINS, ApplicationController::NO_PLUGINS, ApplicationController::NO_THEMES, ApplicationController::NO_UNOFFICIAL_PLUGINS, ApplicationController::SAFE_MODE

Constants included from CanonicalURL::ControllerExtensions

CanonicalURL::ControllerExtensions::ALLOWED_CANONICAL_PARAMS

Instance Attribute Summary

Attributes inherited from ApplicationController

#theme_id

Instance Method Summary collapse

Methods inherited from ApplicationController

#application_layout, #can_cache_content?, #clear_notifications, #conditionally_allow_site_embedding, #current_homepage, #discourse_expires_in, #dont_cache_page, #ember_cli_required?, #fetch_user_from_params, #guardian, #handle_permalink, #handle_theme, #handle_unverified_request, #has_escaped_fragment?, #immutable_for, #no_cookies, #perform_refresh_session, #post_ids_including_replies, #preload_json, #rate_limit_second_factor!, #redirect_with_client_support, #render_json_dump, #render_serialized, requires_plugin, #rescue_discourse_actions, #resolve_safe_mode, #secure_session, #serialize_data, #set_current_user_for_logs, #set_layout, #set_mobile_view, #set_mp_snapshot_fields, #show_browser_update?, #store_preloaded, #use_crawler_layout?, #with_resolved_locale

Methods included from VaryHeader

#ensure_vary_header

Methods included from ReadOnlyMixin

#add_readonly_header, #allowed_in_staff_writes_only_mode?, #block_if_readonly_mode, #check_readonly_mode, included, #staff_writes_only_mode?

Methods included from Hijack

#hijack

Methods included from GlobalPath

#cdn_path, #cdn_relative_path, #full_cdn_url, #path, #upload_cdn_path

Methods included from JsonError

#create_errors_json

Methods included from CanonicalURL::ControllerExtensions

#canonical_url, #default_canonical, included

Methods included from CurrentUser

#clear_current_user, #current_user, has_auth_cookie?, #is_api?, #is_user_api?, #log_off_user, #log_on_user, lookup_from_env, #refresh_session

Instance Method Details

#icon_picker_searchObject



45
46
47
48
49
50
51
52
# File 'app/controllers/svg_sprite_controller.rb', line 45

def icon_picker_search
  params.permit(:filter, :only_available)
  filter = params[:filter] || ""
  only_available = params[:only_available]

  icons = SvgSprite.icon_picker_search(filter, only_available)
  render json: icons.take(200), root: false
end

#searchObject



34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/svg_sprite_controller.rb', line 34

def search
  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

#showObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/svg_sprite_controller.rb', line 14

def show
  no_cookies

  RailsMultisite::ConnectionManagement.with_hostname(params[:hostname]) do
    theme_id = params[:theme_id].to_i if params[:theme_id].present?

    if SvgSprite.version(theme_id) != params[:version]
      return redirect_to UrlHelper.absolute((SvgSprite.path(theme_id))), allow_other_host: true
    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

#svg_iconObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/svg_sprite_controller.rb', line 54

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