2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# Searches and reads file present on GitLab installation directory
|
2016-09-13 17:45:13 +05:30
|
|
|
module Gitlab
|
|
|
|
module Template
|
|
|
|
module Finders
|
|
|
|
class GlobalTemplateFinder < BaseTemplateFinder
|
2020-11-24 15:15:51 +05:30
|
|
|
def initialize(base_dir, extension, categories = {}, excluded_patterns: [])
|
2016-09-13 17:45:13 +05:30
|
|
|
@categories = categories
|
|
|
|
@extension = extension
|
2020-11-24 15:15:51 +05:30
|
|
|
@excluded_patterns = excluded_patterns
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
super(base_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read(path)
|
|
|
|
File.read(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find(key)
|
2020-04-08 14:13:33 +05:30
|
|
|
return if excluded?(key)
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
file_name = "#{key}#{@extension}"
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
# The key is untrusted input, so ensure we can't be directed outside
|
|
|
|
# of base_dir
|
|
|
|
Gitlab::Utils.check_path_traversal!(file_name)
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
directory = select_directory(file_name)
|
|
|
|
directory ? File.join(category_directory(directory), file_name) : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_files_for(dir)
|
2019-02-15 15:39:39 +05:30
|
|
|
dir = "#{dir}/" unless dir.end_with?('/')
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
Dir.glob(File.join(dir, "*#{@extension}")).select do |f|
|
|
|
|
next if excluded?(f)
|
|
|
|
|
|
|
|
f =~ self.class.filter_regex(@extension)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def excluded?(file_name)
|
2020-11-24 15:15:51 +05:30
|
|
|
@excluded_patterns.any? { |pattern| pattern.match?(file_name) }
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def select_directory(file_name)
|
|
|
|
@categories.keys.find do |category|
|
|
|
|
File.exist?(File.join(category_directory(category), file_name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|