2018-01-09 07:23:49 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-26 04:33:29 +08:00
|
|
|
class ExtraLocalesController < ApplicationController
|
2023-12-06 20:19:09 +08:00
|
|
|
layout false
|
2019-04-30 14:58:18 +08:00
|
|
|
|
|
|
|
skip_before_action :check_xhr,
|
|
|
|
:preload_json,
|
|
|
|
:redirect_to_login_if_required,
|
2024-06-25 19:32:18 +08:00
|
|
|
:redirect_to_profile_if_required,
|
2019-04-30 14:58:18 +08:00
|
|
|
:verify_authenticity_token
|
2016-08-26 04:33:29 +08:00
|
|
|
|
2019-11-05 09:15:44 +08:00
|
|
|
OVERRIDES_BUNDLE ||= "overrides"
|
2019-11-12 05:30:31 +08:00
|
|
|
MD5_HASH_LENGTH ||= 32
|
2024-06-18 00:21:04 +08:00
|
|
|
MF_BUNDLE = "mf"
|
|
|
|
BUNDLES = [OVERRIDES_BUNDLE, MF_BUNDLE]
|
2019-11-05 09:15:44 +08:00
|
|
|
|
2024-06-18 00:21:04 +08:00
|
|
|
class << self
|
|
|
|
def js_digests
|
|
|
|
@js_digests ||= {}
|
|
|
|
end
|
2019-08-20 18:38:46 +08:00
|
|
|
|
2024-06-18 00:21:04 +08:00
|
|
|
def bundle_js_hash(bundle)
|
|
|
|
bundle_key = "#{bundle}_#{I18n.locale}"
|
|
|
|
if bundle.in?(BUNDLES)
|
|
|
|
site = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
|
|
|
|
js_digests[site] ||= {}
|
|
|
|
js_digests[site][bundle_key] ||= begin
|
|
|
|
js = bundle_js(bundle)
|
|
|
|
js.present? ? Digest::MD5.hexdigest(js) : nil
|
|
|
|
end
|
2019-11-12 05:30:31 +08:00
|
|
|
else
|
2024-06-18 00:21:04 +08:00
|
|
|
js_digests[bundle_key] ||= Digest::MD5.hexdigest(bundle_js(bundle))
|
2019-11-12 05:30:31 +08:00
|
|
|
end
|
2018-01-09 07:23:49 +08:00
|
|
|
end
|
2019-08-20 18:38:46 +08:00
|
|
|
|
2024-06-18 00:21:04 +08:00
|
|
|
def url(bundle)
|
|
|
|
"#{Discourse.base_path}/extra-locales/#{bundle}?v=#{bundle_js_hash(bundle)}"
|
|
|
|
end
|
2018-01-09 07:23:49 +08:00
|
|
|
|
2024-06-18 00:21:04 +08:00
|
|
|
def client_overrides_exist?
|
|
|
|
bundle_js_hash(OVERRIDES_BUNDLE).present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def bundle_js(bundle)
|
|
|
|
locale_str = I18n.locale.to_s
|
|
|
|
bundle_str = "#{bundle}_js"
|
2018-01-09 07:23:49 +08:00
|
|
|
|
2024-06-18 00:21:04 +08:00
|
|
|
case bundle
|
|
|
|
when OVERRIDES_BUNDLE
|
|
|
|
JsLocaleHelper.output_client_overrides(locale_str)
|
|
|
|
when MF_BUNDLE
|
|
|
|
JsLocaleHelper.output_MF(locale_str)
|
|
|
|
else
|
|
|
|
JsLocaleHelper.output_extra_locales(bundle_str, locale_str)
|
2019-11-05 09:15:44 +08:00
|
|
|
end
|
2018-01-09 07:23:49 +08:00
|
|
|
end
|
2016-08-26 04:33:29 +08:00
|
|
|
|
2024-06-18 00:21:04 +08:00
|
|
|
def bundle_js_with_hash(bundle)
|
|
|
|
js = bundle_js(bundle)
|
|
|
|
[js, Digest::MD5.hexdigest(js)]
|
|
|
|
end
|
2019-11-05 09:15:44 +08:00
|
|
|
|
2024-06-18 00:21:04 +08:00
|
|
|
def clear_cache!
|
|
|
|
site = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
js_digests.delete(site)
|
|
|
|
end
|
2019-11-05 09:15:44 +08:00
|
|
|
end
|
|
|
|
|
2024-06-18 00:21:04 +08:00
|
|
|
def show
|
|
|
|
bundle = params[:bundle]
|
|
|
|
raise Discourse::InvalidAccess.new if !valid_bundle?(bundle)
|
2017-02-24 18:31:21 +08:00
|
|
|
|
2024-06-18 00:21:04 +08:00
|
|
|
version = params[:v]
|
|
|
|
if version.present?
|
|
|
|
raise Discourse::InvalidParameters.new(:v) unless version.to_s.size == MD5_HASH_LENGTH
|
2019-05-24 03:23:31 +08:00
|
|
|
end
|
2017-02-24 18:31:21 +08:00
|
|
|
|
2024-06-18 00:21:04 +08:00
|
|
|
content, hash = ExtraLocalesController.bundle_js_with_hash(bundle)
|
|
|
|
immutable_for(1.year) if hash == version
|
|
|
|
|
|
|
|
render plain: content, content_type: "application/javascript"
|
2019-11-05 09:15:44 +08:00
|
|
|
end
|
2016-08-26 04:33:29 +08:00
|
|
|
|
2019-11-05 09:15:44 +08:00
|
|
|
private
|
2016-09-30 14:29:30 +08:00
|
|
|
|
2019-11-05 09:15:44 +08:00
|
|
|
def valid_bundle?(bundle)
|
2024-06-18 00:21:04 +08:00
|
|
|
bundle.in?(BUNDLES) || (bundle =~ /\A(admin|wizard)\z/ && current_user&.staff?)
|
2016-08-26 04:33:29 +08:00
|
|
|
end
|
|
|
|
end
|