2020-12-15 12:10:17 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Pages
|
|
|
|
class ZipDirectoryService
|
2021-01-12 18:11:03 +00:00
|
|
|
include BaseServiceUtility
|
2020-12-21 18:10:26 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2021-01-12 18:11:03 +00:00
|
|
|
# used only to track exceptions in Sentry
|
|
|
|
InvalidEntryError = Class.new(StandardError)
|
2020-12-15 12:10:17 +00:00
|
|
|
|
|
|
|
PUBLIC_DIR = 'public'
|
|
|
|
|
2021-02-11 21:09:00 +00:00
|
|
|
attr_reader :public_dir, :real_dir
|
|
|
|
|
|
|
|
def initialize(input_dir, ignore_invalid_entries: false)
|
2020-12-21 18:10:26 +00:00
|
|
|
@input_dir = input_dir
|
2021-02-11 21:09:00 +00:00
|
|
|
@ignore_invalid_entries = ignore_invalid_entries
|
2020-12-15 12:10:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2021-04-05 21:09:19 +00:00
|
|
|
return success unless resolve_public_dir
|
2020-12-21 18:10:26 +00:00
|
|
|
|
|
|
|
output_file = File.join(real_dir, "@migrated.zip") # '@' to avoid any name collision with groups or projects
|
|
|
|
|
|
|
|
FileUtils.rm_f(output_file)
|
2020-12-15 12:10:17 +00:00
|
|
|
|
2021-01-12 18:11:03 +00:00
|
|
|
entries_count = 0
|
2021-11-26 09:10:08 +00:00
|
|
|
# Since we're writing not reading here, we can safely silence the cop.
|
|
|
|
# It currently cannot discern between opening for reading or writing.
|
|
|
|
::Zip::File.open(output_file, ::Zip::File::CREATE) do |zipfile| # rubocop:disable Performance/Rubyzip
|
2020-12-15 12:10:17 +00:00
|
|
|
write_entry(zipfile, PUBLIC_DIR)
|
2021-01-12 18:11:03 +00:00
|
|
|
entries_count = zipfile.entries.count
|
2020-12-15 12:10:17 +00:00
|
|
|
end
|
|
|
|
|
2021-01-12 18:11:03 +00:00
|
|
|
success(archive_path: output_file, entries_count: entries_count)
|
2021-04-26 12:09:44 +00:00
|
|
|
rescue StandardError => e
|
2020-12-21 18:10:26 +00:00
|
|
|
FileUtils.rm_f(output_file) if output_file
|
|
|
|
raise e
|
2020-12-15 12:10:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-02-11 21:09:00 +00:00
|
|
|
def resolve_public_dir
|
|
|
|
@real_dir = File.realpath(@input_dir)
|
|
|
|
@public_dir = File.join(real_dir, PUBLIC_DIR)
|
|
|
|
|
|
|
|
valid_path?(public_dir)
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2020-12-15 12:10:17 +00:00
|
|
|
def write_entry(zipfile, zipfile_path)
|
2020-12-21 18:10:26 +00:00
|
|
|
disk_file_path = File.join(real_dir, zipfile_path)
|
2020-12-15 12:10:17 +00:00
|
|
|
|
|
|
|
unless valid_path?(disk_file_path)
|
|
|
|
# archive with invalid entry will just have this entry missing
|
2021-02-11 21:09:00 +00:00
|
|
|
raise InvalidEntryError, "#{disk_file_path} is invalid, input_dir: #{@input_dir}"
|
2020-12-15 12:10:17 +00:00
|
|
|
end
|
|
|
|
|
2021-02-11 21:09:00 +00:00
|
|
|
ftype = File.lstat(disk_file_path).ftype
|
|
|
|
case ftype
|
2020-12-15 12:10:17 +00:00
|
|
|
when 'directory'
|
|
|
|
recursively_zip_directory(zipfile, disk_file_path, zipfile_path)
|
|
|
|
when 'file', 'link'
|
|
|
|
zipfile.add(zipfile_path, disk_file_path)
|
|
|
|
else
|
2021-02-11 21:09:00 +00:00
|
|
|
raise InvalidEntryError, "#{disk_file_path} has invalid ftype: #{ftype}, input_dir: #{@input_dir}"
|
2020-12-15 12:10:17 +00:00
|
|
|
end
|
2021-02-11 21:09:00 +00:00
|
|
|
rescue Errno::ENOENT, Errno::ELOOP, InvalidEntryError => e
|
2020-12-15 12:10:17 +00:00
|
|
|
Gitlab::ErrorTracking.track_exception(e, input_dir: @input_dir, disk_file_path: disk_file_path)
|
2021-02-11 21:09:00 +00:00
|
|
|
|
|
|
|
raise e unless @ignore_invalid_entries
|
2020-12-15 12:10:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def recursively_zip_directory(zipfile, disk_file_path, zipfile_path)
|
|
|
|
zipfile.mkdir(zipfile_path)
|
|
|
|
|
|
|
|
entries = Dir.entries(disk_file_path) - %w[. ..]
|
|
|
|
entries = entries.map { |entry| File.join(zipfile_path, entry) }
|
|
|
|
|
|
|
|
write_entries(zipfile, entries)
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_entries(zipfile, entries)
|
|
|
|
entries.each do |zipfile_path|
|
|
|
|
write_entry(zipfile, zipfile_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-11 21:09:00 +00:00
|
|
|
# SafeZip was introduced only recently,
|
|
|
|
# so we have invalid entries on disk
|
2020-12-15 12:10:17 +00:00
|
|
|
def valid_path?(disk_file_path)
|
|
|
|
realpath = File.realpath(disk_file_path)
|
2021-01-12 18:11:03 +00:00
|
|
|
realpath == public_dir || realpath.start_with?(public_dir + "/")
|
|
|
|
end
|
2020-12-15 12:10:17 +00:00
|
|
|
end
|
|
|
|
end
|