summaryrefslogtreecommitdiff
path: root/app/services/packages/update_package_file_service.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-08-19 09:08:42 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2021-08-19 09:08:42 +0000
commitb76ae638462ab0f673e5915986070518dd3f9ad3 (patch)
treebdab0533383b52873be0ec0eb4d3c66598ff8b91 /app/services/packages/update_package_file_service.rb
parent434373eabe7b4be9593d18a585fb763f1e5f1a6f (diff)
downloadgitlab-ce-b76ae638462ab0f673e5915986070518dd3f9ad3.tar.gz
Add latest changes from gitlab-org/gitlab@14-2-stable-eev14.2.0-rc42
Diffstat (limited to 'app/services/packages/update_package_file_service.rb')
-rw-r--r--app/services/packages/update_package_file_service.rb58
1 files changed, 58 insertions, 0 deletions
diff --git a/app/services/packages/update_package_file_service.rb b/app/services/packages/update_package_file_service.rb
new file mode 100644
index 00000000000..9a8a78e509a
--- /dev/null
+++ b/app/services/packages/update_package_file_service.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+module Packages
+ class UpdatePackageFileService
+ delegate :file, to: :@package_file
+
+ def initialize(package_file, params)
+ @package_file = package_file
+ @params = params
+ end
+
+ def execute
+ check_params
+
+ return if same_as_params?
+
+ # we need to access the file *before* updating the attributes linked to its path/key.
+ file_storage_mode = file.file_storage?
+
+ @package_file.package_id = package_id if package_id
+ @package_file.file_name = file_name if file_name
+
+ if file_storage_mode
+ # package file is in mode LOCAL: we can pass the `file` to the update
+ @package_file.file = file
+ else
+ # package file is in mode REMOTE: don't pass the `file` to the update
+ # instead, pass the new file path. This will move the file
+ # in object storage.
+ @package_file.new_file_path = File.join(file.store_dir, @package_file.file_name)
+ end
+
+ @package_file.save!
+ end
+
+ private
+
+ def check_params
+ raise ArgumentError, 'package_file not persisted' unless @package_file.persisted?
+ raise ArgumentError, 'package_id and file_name are blank' if package_id.blank? && file_name.blank?
+ end
+
+ def same_as_params?
+ return false if package_id && package_id != @package_file.package_id
+ return false if file_name && file_name != @package_file.file_name
+
+ true
+ end
+
+ def package_id
+ @params[:package_id]
+ end
+
+ def file_name
+ @params[:file_name]
+ end
+ end
+end