2018-07-04 13:32:46 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-03 11:26:49 -04:00
|
|
|
class FileMover
|
2019-06-28 17:28:52 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2019-06-04 19:50:57 -04:00
|
|
|
attr_reader :secret, :file_name, :from_model, :to_model, :update_field
|
2017-05-03 11:26:49 -04:00
|
|
|
|
2019-06-04 19:50:57 -04:00
|
|
|
def initialize(file_path, update_field = :description, from_model:, to_model:)
|
2017-05-03 11:26:49 -04:00
|
|
|
@secret = File.split(File.dirname(file_path)).last
|
|
|
|
@file_name = File.basename(file_path)
|
2019-06-04 19:50:57 -04:00
|
|
|
@from_model = from_model
|
|
|
|
@to_model = to_model
|
2017-05-29 03:54:35 -04:00
|
|
|
@update_field = update_field
|
2017-05-03 11:26:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2019-06-28 17:28:52 -04:00
|
|
|
temp_file_uploader.retrieve_from_store!(file_name)
|
|
|
|
|
2019-02-13 03:24:26 -05:00
|
|
|
return unless valid?
|
|
|
|
|
2019-06-28 17:28:52 -04:00
|
|
|
uploader.retrieve_from_store!(file_name)
|
|
|
|
|
2017-05-03 11:26:49 -04:00
|
|
|
move
|
2018-03-16 14:34:55 -04:00
|
|
|
|
|
|
|
if update_markdown
|
2019-06-04 19:50:57 -04:00
|
|
|
update_upload_model
|
2018-03-16 14:34:55 -04:00
|
|
|
uploader.schedule_background_upload
|
|
|
|
end
|
2017-05-03 11:26:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-02-13 03:24:26 -05:00
|
|
|
def valid?
|
2019-06-28 17:28:52 -04:00
|
|
|
if temp_file_uploader.file_storage?
|
|
|
|
Pathname.new(temp_file_path).realpath.to_path.start_with?(
|
|
|
|
(Pathname(temp_file_uploader.root) + temp_file_uploader.base_dir).to_path
|
|
|
|
)
|
|
|
|
else
|
|
|
|
temp_file_uploader.exists?
|
|
|
|
end
|
2019-02-13 03:24:26 -05:00
|
|
|
end
|
|
|
|
|
2017-05-03 11:26:49 -04:00
|
|
|
def move
|
2019-06-28 17:28:52 -04:00
|
|
|
if temp_file_uploader.file_storage?
|
|
|
|
FileUtils.mkdir_p(File.dirname(file_path))
|
|
|
|
FileUtils.move(temp_file_path, file_path)
|
|
|
|
else
|
|
|
|
uploader.copy_file(temp_file_uploader.file)
|
|
|
|
temp_file_uploader.upload.destroy!
|
|
|
|
end
|
2017-05-03 11:26:49 -04:00
|
|
|
end
|
|
|
|
|
2017-05-29 03:54:35 -04:00
|
|
|
def update_markdown
|
2019-06-04 19:50:57 -04:00
|
|
|
updated_text = to_model.read_attribute(update_field)
|
|
|
|
.gsub(temp_file_uploader.markdown_link, uploader.markdown_link)
|
|
|
|
to_model.update_attribute(update_field, updated_text)
|
2017-05-29 03:54:35 -04:00
|
|
|
rescue
|
|
|
|
revert
|
|
|
|
false
|
2017-05-03 11:26:49 -04:00
|
|
|
end
|
|
|
|
|
2019-06-04 19:50:57 -04:00
|
|
|
def update_upload_model
|
|
|
|
return unless upload = temp_file_uploader.upload
|
2019-06-28 17:28:52 -04:00
|
|
|
return if upload.destroyed?
|
2019-06-04 19:50:57 -04:00
|
|
|
|
2019-06-28 17:28:52 -04:00
|
|
|
upload.update!(model: to_model)
|
2019-06-04 19:50:57 -04:00
|
|
|
end
|
|
|
|
|
2017-05-03 11:26:49 -04:00
|
|
|
def temp_file_path
|
2019-06-28 17:28:52 -04:00
|
|
|
strong_memoize(:temp_file_path) do
|
|
|
|
temp_file_uploader.file.path
|
|
|
|
end
|
2017-05-03 11:26:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def file_path
|
2019-06-28 17:28:52 -04:00
|
|
|
strong_memoize(:file_path) do
|
|
|
|
uploader.file.path
|
|
|
|
end
|
2017-05-03 11:26:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def uploader
|
2019-06-28 17:28:52 -04:00
|
|
|
@uploader ||=
|
|
|
|
begin
|
|
|
|
uploader = PersonalFileUploader.new(to_model, secret: secret)
|
|
|
|
|
|
|
|
# Enforcing a REMOTE object storage given FileUploader#retrieve_from_store! won't do it
|
|
|
|
# (there's no upload at the target yet).
|
|
|
|
if uploader.class.object_store_enabled?
|
|
|
|
uploader.object_store = ::ObjectStorage::Store::REMOTE
|
|
|
|
end
|
|
|
|
|
|
|
|
uploader
|
|
|
|
end
|
2017-05-03 11:26:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def temp_file_uploader
|
2019-06-04 19:50:57 -04:00
|
|
|
@temp_file_uploader ||= PersonalFileUploader.new(from_model, secret: secret)
|
2017-05-03 11:26:49 -04:00
|
|
|
end
|
2017-05-29 03:54:35 -04:00
|
|
|
|
|
|
|
def revert
|
2019-07-10 15:26:47 -04:00
|
|
|
Rails.logger.warn("Markdown not updated, file move reverted for #{to_model}") # rubocop:disable Gitlab/RailsLogger
|
2017-05-29 03:54:35 -04:00
|
|
|
|
2019-06-28 17:28:52 -04:00
|
|
|
if temp_file_uploader.file_storage?
|
|
|
|
FileUtils.move(file_path, temp_file_path)
|
|
|
|
end
|
2017-05-29 03:54:35 -04:00
|
|
|
end
|
2017-05-03 11:26:49 -04:00
|
|
|
end
|