debian-mirror-gitlab/app/uploaders/file_mover.rb

107 lines
2.5 KiB
Ruby
Raw Permalink Normal View History

2018-11-08 19:23:39 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
class FileMover
2019-07-07 11:18:12 +05:30
include Gitlab::Utils::StrongMemoize
2017-09-10 17:25:29 +05:30
2019-07-07 11:18:12 +05:30
attr_reader :secret, :file_name, :from_model, :to_model, :update_field
def initialize(file_path, update_field = :description, from_model:, to_model:)
2017-09-10 17:25:29 +05:30
@secret = File.split(File.dirname(file_path)).last
@file_name = File.basename(file_path)
2019-07-07 11:18:12 +05:30
@from_model = from_model
@to_model = to_model
2017-09-10 17:25:29 +05:30
@update_field = update_field
end
def execute
2019-07-07 11:18:12 +05:30
temp_file_uploader.retrieve_from_store!(file_name)
2019-03-13 22:55:13 +05:30
return unless valid?
2019-07-07 11:18:12 +05:30
uploader.retrieve_from_store!(file_name)
2017-09-10 17:25:29 +05:30
move
2018-05-09 12:01:36 +05:30
if update_markdown
2019-07-07 11:18:12 +05:30
update_upload_model
2018-05-09 12:01:36 +05:30
end
2017-09-10 17:25:29 +05:30
end
private
2019-03-13 22:55:13 +05:30
def valid?
2019-07-07 11:18:12 +05:30
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-03-13 22:55:13 +05:30
end
2017-09-10 17:25:29 +05:30
def move
2019-07-07 11:18:12 +05:30
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-09-10 17:25:29 +05:30
end
def update_markdown
2019-07-07 11:18:12 +05:30
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)
2021-06-08 01:23:25 +05:30
rescue StandardError
2017-09-10 17:25:29 +05:30
revert
false
end
2019-07-07 11:18:12 +05:30
def update_upload_model
return unless upload = temp_file_uploader.upload
return if upload.destroyed?
2017-09-10 17:25:29 +05:30
2019-07-07 11:18:12 +05:30
upload.update!(model: to_model)
end
2017-09-10 17:25:29 +05:30
2019-07-07 11:18:12 +05:30
def temp_file_path
strong_memoize(:temp_file_path) do
temp_file_uploader.file.path
end
2017-09-10 17:25:29 +05:30
end
def file_path
2019-07-07 11:18:12 +05:30
strong_memoize(:file_path) do
uploader.file.path
end
2017-09-10 17:25:29 +05:30
end
def uploader
2019-07-07 11:18:12 +05:30
@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-09-10 17:25:29 +05:30
end
def temp_file_uploader
2019-07-07 11:18:12 +05:30
@temp_file_uploader ||= PersonalFileUploader.new(from_model, secret: secret)
2017-09-10 17:25:29 +05:30
end
def revert
2020-06-23 00:09:42 +05:30
Gitlab::AppLogger.warn("Markdown not updated, file move reverted for #{to_model}")
2017-09-10 17:25:29 +05:30
2019-07-07 11:18:12 +05:30
if temp_file_uploader.file_storage?
FileUtils.move(file_path, temp_file_path)
end
2017-09-10 17:25:29 +05:30
end
end