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-05-18 00:54:41 +05:30
|
|
|
class MigrateAttachmentsService < BaseAttachmentService
|
2018-11-18 11:00:15 +05:30
|
|
|
def initialize(project, old_disk_path, logger: nil)
|
2018-03-17 18:26:18 +05:30
|
|
|
@project = project
|
|
|
|
@logger = logger || Rails.logger
|
2018-11-18 11:00:15 +05:30
|
|
|
@old_disk_path = old_disk_path
|
2019-03-02 22:35:43 +05:30
|
|
|
@skipped = false
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
origin = FileUploader.absolute_base_dir(project)
|
2019-05-18 00:54:41 +05:30
|
|
|
# It's possible that old_disk_path does not match project.disk_path.
|
|
|
|
# For example, that happens when we rename a project
|
2018-11-18 11:00:15 +05:30
|
|
|
origin.sub!(/#{Regexp.escape(project.full_path)}\z/, old_disk_path)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
project.storage_version = ::Project::HASHED_STORAGE_FEATURES[:attachments]
|
|
|
|
target = FileUploader.absolute_base_dir(project)
|
|
|
|
|
2019-05-18 00:54:41 +05:30
|
|
|
@new_disk_path = project.disk_path
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2019-05-18 00:54:41 +05:30
|
|
|
result = move_folder!(origin, target)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-05-18 00:54:41 +05:30
|
|
|
if result
|
|
|
|
project.save!(validate: false)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-05-18 00:54:41 +05:30
|
|
|
yield if block_given?
|
|
|
|
else
|
|
|
|
# Rollback changes
|
|
|
|
project.rollback!
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2019-05-18 00:54:41 +05:30
|
|
|
result
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|