2019-03-01 06:58:05 +00:00
# frozen_string_literal: true
module Projects
module HashedStorage
AttachmentMigrationError = Class . new ( StandardError )
AttachmentCannotMoveError = Class . new ( StandardError )
class BaseAttachmentService < BaseService
# Returns the disk_path value before the execution
attr_reader :old_disk_path
# Returns the disk_path value after the execution
attr_reader :new_disk_path
# Returns the logger currently in use
attr_reader :logger
2019-11-14 21:06:14 +00:00
def initialize ( project : , old_disk_path : , logger : nil )
@project = project
@old_disk_path = old_disk_path
@logger = logger || Rails . logger # rubocop:disable Gitlab/RailsLogger
end
2019-03-01 06:58:05 +00:00
# Return whether this operation was skipped or not
#
# @return [Boolean] true if skipped of false otherwise
def skipped?
@skipped
end
2019-11-14 21:06:14 +00:00
# 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 )
false
end
2019-03-01 06:58:05 +00:00
protected
def move_folder! ( old_path , new_path )
unless File . directory? ( old_path )
logger . info ( " Skipped attachments move from ' #{ old_path } ' to ' #{ new_path } ', source path doesn't exist or is not a directory (PROJECT_ID= #{ project . id } ) " )
@skipped = true
return true
end
if File . exist? ( new_path )
2019-11-14 21:06:14 +00:00
if target_path_discardable? ( new_path )
discard_path! ( new_path )
else
logger . error ( " Cannot move attachments from ' #{ old_path } ' to ' #{ new_path } ', target path already exist (PROJECT_ID= #{ project . id } ) " )
raise AttachmentCannotMoveError , " Target path ' #{ new_path } ' already exists "
end
2019-03-01 06:58:05 +00:00
end
# Create base path folder on the new storage layout
FileUtils . mkdir_p ( File . dirname ( new_path ) )
FileUtils . mv ( old_path , new_path )
logger . info ( " Project attachments moved from ' #{ old_path } ' to ' #{ new_path } ' (PROJECT_ID= #{ project . id } ) " )
true
end
2019-11-14 21:06:14 +00:00
# Rename a path adding a suffix in order to prevent data-loss.
#
# @param [String] new_path
def discard_path! ( new_path )
discarded_path = " #{ new_path } - #{ Time . now . utc . to_i } "
logger . info ( " Moving existing empty attachments folder from ' #{ new_path } ' to ' #{ discarded_path } ', (PROJECT_ID= #{ project . id } ) " )
FileUtils . mv ( new_path , discarded_path )
end
2019-03-01 06:58:05 +00:00
end
end
end