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
|
2021-03-11 19:13:27 +05:30
|
|
|
def initialize(base_dir, extension, categories = {}, include_categories_for_file = {}, excluded_patterns: [])
|
2016-09-13 17:45:13 +05:30
|
|
|
@categories = categories
|
|
|
|
@extension = extension
|
2021-03-11 19:13:27 +05:30
|
|
|
@include_categories_for_file = include_categories_for_file
|
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)
|
2021-03-11 19:13:27 +05:30
|
|
|
categories = @categories
|
|
|
|
categories.merge!(@include_categories_for_file[file_name]) if @include_categories_for_file[file_name].present?
|
|
|
|
categories.keys.find do |category|
|
2016-09-13 17:45:13 +05:30
|
|
|
File.exist?(File.join(category_directory(category), file_name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|