debian-mirror-gitlab/app/services/projects/hashed_storage/migrate_attachments_service.rb

68 lines
1.9 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module Projects
module HashedStorage
2019-07-07 11:18:12 +05:30
class MigrateAttachmentsService < BaseAttachmentService
2019-12-26 22:10:19 +05:30
extend ::Gitlab::Utils::Override
# List of paths that can be excluded while evaluation if a target can be discarded
DISCARDABLE_PATHS = %w(tmp tmp/cache tmp/work).freeze
def initialize(project:, old_disk_path:, logger: nil)
super
2019-03-02 22:35:43 +05:30
@skipped = false
2018-03-17 18:26:18 +05:30
end
def execute
2019-12-26 22:10:19 +05:30
origin = find_old_attachments_path(project)
2018-11-18 11:00:15 +05:30
2018-03-17 18:26:18 +05:30
project.storage_version = ::Project::HASHED_STORAGE_FEATURES[:attachments]
target = FileUploader.absolute_base_dir(project)
2019-07-07 11:18:12 +05:30
@new_disk_path = project.disk_path
2019-05-30 16:15:17 +05:30
2019-07-07 11:18:12 +05:30
result = move_folder!(origin, target)
2019-05-30 16:15:17 +05:30
2019-07-07 11:18:12 +05:30
if result
project.save!(validate: false)
2019-05-30 16:15:17 +05:30
2019-07-07 11:18:12 +05:30
yield if block_given?
2019-05-30 16:15:17 +05:30
end
2019-07-07 11:18:12 +05:30
result
2019-05-30 16:15:17 +05:30
end
2019-12-26 22:10:19 +05:30
override :target_path_discardable?
# Check if target path has discardable content
#
# @param [String] new_path
# @return [Boolean] whether we can discard the target path or not
def target_path_discardable?(new_path)
return false unless File.directory?(new_path)
found = Dir.glob(File.join(new_path, '**', '**'))
(found - discardable_paths(new_path)).empty?
end
private
def discardable_paths(new_path)
DISCARDABLE_PATHS.collect { |path| File.join(new_path, path) }
end
def find_old_attachments_path(project)
origin = FileUploader.absolute_base_dir(project)
# It's possible that old_disk_path does not match project.disk_path.
# For example, that happens when we rename a project
#
origin.sub(/#{Regexp.escape(project.full_path)}\z/, old_disk_path)
end
2018-03-17 18:26:18 +05:30
end
end
end
2019-12-04 20:38:33 +05:30
Projects::HashedStorage::MigrateAttachmentsService.prepend_if_ee('EE::Projects::HashedStorage::MigrateAttachmentsService')