2019-02-02 18:00:53 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module SafeZip
|
|
|
|
class ExtractParams
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2023-02-26 17:17:37 +05:30
|
|
|
attr_reader :directories, :files, :extract_path
|
2019-02-02 18:00:53 +05:30
|
|
|
|
2023-02-26 17:17:37 +05:30
|
|
|
def initialize(to:, directories: [], files: [])
|
2019-02-02 18:00:53 +05:30
|
|
|
@directories = directories
|
2023-02-26 17:17:37 +05:30
|
|
|
@files = files
|
2019-02-02 18:00:53 +05:30
|
|
|
@extract_path = ::File.realpath(to)
|
2023-02-26 17:17:37 +05:30
|
|
|
validate!
|
2019-02-02 18:00:53 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def matching_target_directory(path)
|
|
|
|
target_directories.find do |directory|
|
|
|
|
path.start_with?(directory)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_directories
|
|
|
|
strong_memoize(:target_directories) do
|
|
|
|
directories.map do |directory|
|
|
|
|
::File.join(::File.expand_path(directory, extract_path), '')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def directories_wildcard
|
|
|
|
strong_memoize(:directories_wildcard) do
|
|
|
|
directories.map do |directory|
|
|
|
|
::File.join(directory, '*')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-02-26 17:17:37 +05:30
|
|
|
|
|
|
|
def matching_target_file(path)
|
|
|
|
target_files.include?(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def target_files
|
|
|
|
strong_memoize(:target_files) do
|
|
|
|
files.map do |file|
|
|
|
|
::File.join(extract_path, file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate!
|
|
|
|
raise ArgumentError, 'Either directories or files are required' if directories.empty? && files.empty?
|
|
|
|
end
|
2019-02-02 18:00:53 +05:30
|
|
|
end
|
|
|
|
end
|