diff options
author | Oswaldo Ferreira <oswaldo@gitlab.com> | 2019-06-29 00:28:52 +0300 |
---|---|---|
committer | Oswaldo Ferreira <oswaldo@gitlab.com> | 2019-07-01 02:40:05 +0300 |
commit | e8bd2dfb40e3b711f766f56a59184880c06e0b5d (patch) | |
tree | 63a86927fab9a639c16ebb9b23a812ed518a3c48 /app | |
parent | 9ce341475a06dd3dba67dbe2ba7b7fcefdd24ec6 (diff) |
Support object storage at FileMover class
Diffstat (limited to 'app')
-rw-r--r-- | app/uploaders/file_mover.rb | 61 |
1 files changed, 43 insertions, 18 deletions
diff --git a/app/uploaders/file_mover.rb b/app/uploaders/file_mover.rb index dcf1e8792ad..12be1e2bb22 100644 --- a/app/uploaders/file_mover.rb +++ b/app/uploaders/file_mover.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class FileMover + include Gitlab::Utils::StrongMemoize + attr_reader :secret, :file_name, :from_model, :to_model, :update_field def initialize(file_path, update_field = :description, from_model:, to_model:) @@ -12,8 +14,12 @@ class FileMover end def execute + temp_file_uploader.retrieve_from_store!(file_name) + return unless valid? + uploader.retrieve_from_store!(file_name) + move if update_markdown @@ -25,14 +31,23 @@ class FileMover private def valid? - Pathname.new(temp_file_path).realpath.to_path.start_with?( - (Pathname(temp_file_uploader.root) + temp_file_uploader.base_dir).to_path - ) + 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 end def move - FileUtils.mkdir_p(File.dirname(file_path)) - FileUtils.move(temp_file_path, file_path) + 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 end def update_markdown @@ -46,28 +61,36 @@ class FileMover def update_upload_model return unless upload = temp_file_uploader.upload + return if upload.destroyed? - upload.update!(model_id: to_model.id, model_type: to_model.type) + upload.update!(model: to_model) end def temp_file_path - return @temp_file_path if @temp_file_path - - temp_file_uploader.retrieve_from_store!(file_name) - - @temp_file_path = temp_file_uploader.file.path + strong_memoize(:temp_file_path) do + temp_file_uploader.file.path + end end def file_path - return @file_path if @file_path - - uploader.retrieve_from_store!(file_name) - - @file_path = uploader.file.path + strong_memoize(:file_path) do + uploader.file.path + end end def uploader - @uploader ||= PersonalFileUploader.new(to_model, secret: secret) + @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 end def temp_file_uploader @@ -77,6 +100,8 @@ class FileMover def revert Rails.logger.warn("Markdown not updated, file move reverted for #{to_model}") - FileUtils.move(file_path, temp_file_path) + if temp_file_uploader.file_storage? + FileUtils.move(file_path, temp_file_path) + end end end |