mirror of
https://github.com/mastodon/mastodon.git
synced 2026-01-13 16:16:40 +00:00
32 lines
680 B
Ruby
32 lines
680 B
Ruby
# frozen_string_literal: true
|
|
|
|
require 'singleton'
|
|
|
|
class InlineScriptManager
|
|
include Singleton
|
|
include ActionView::Helpers::TagHelper
|
|
include ActionView::Helpers::JavaScriptHelper
|
|
|
|
def initialize
|
|
@cached_files = {}
|
|
end
|
|
|
|
def file(name)
|
|
@cached_files[name] ||= load_file(name)
|
|
end
|
|
|
|
private
|
|
|
|
def load_file(name)
|
|
path = Pathname.new(name).cleanpath
|
|
raise ArgumentError, "Invalid inline javascript path: #{path}" if path.to_s.start_with?('..')
|
|
|
|
path = Rails.root.join('app', 'javascript', 'inline', path)
|
|
|
|
contents = javascript_cdata_section(path.read)
|
|
digest = Digest::SHA256.base64digest(contents)
|
|
|
|
{ contents:, digest: }
|
|
end
|
|
end
|