From f676b262a3933b5e1d295944aa1bc4de73c44edf Mon Sep 17 00:00:00 2001
From: Michael Kozono <mkozono@gitlab.com>
Date: Fri, 21 Jul 2023 21:27:19 +0000
Subject: [PATCH] Merge branch 'eb-fix-pending-upload-completion-bucket-prefix'
 into 'master'

Fix completion of pending direct upload

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/127017

Merged-by: Michael Kozono <mkozono@gitlab.com>
Approved-by: Albert Salim <asalim@gitlab.com>
Approved-by: Michael Kozono <mkozono@gitlab.com>
Reviewed-by: Michael Kozono <mkozono@gitlab.com>
Co-authored-by: Erick Bajao <fbajao@gitlab.com>

(cherry picked from commit 26ed6f8d1997055e3309c7f91c9c8ad81d80fe88)

a3e85cee Fix completion of pending direct upload
---
 app/uploaders/object_storage.rb       |  7 ++++++-
 spec/uploaders/object_storage_spec.rb | 18 +++++++++++++++---
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/app/uploaders/object_storage.rb b/app/uploaders/object_storage.rb
index 672433ec534bb..a8328304e734d 100644
--- a/app/uploaders/object_storage.rb
+++ b/app/uploaders/object_storage.rb
@@ -31,7 +31,8 @@ def store!(file)
       # The direct_upload_final_path is defined which means
       # file was uploaded to its final location so no need to move it.
       # Now we delete the pending upload entry as the upload is considered complete.
-      ObjectStorage::PendingDirectUpload.complete(@uploader.class.storage_location_identifier, file.path)
+      pending_upload_path = @uploader.class.without_bucket_prefix(file.path)
+      ObjectStorage::PendingDirectUpload.complete(@uploader.class.storage_location_identifier, pending_upload_path)
 
       file
     end
@@ -196,6 +197,10 @@ def with_bucket_prefix(path)
         File.join([object_store_options.bucket_prefix, path].compact)
       end
 
+      def without_bucket_prefix(path)
+        Pathname.new(path).relative_path_from(object_store_options.bucket_prefix.to_s).to_s
+      end
+
       def object_store_config
         ObjectStorage::Config.new(object_store_options)
       end
diff --git a/spec/uploaders/object_storage_spec.rb b/spec/uploaders/object_storage_spec.rb
index a748c544bfdc6..8c33224968dc4 100644
--- a/spec/uploaders/object_storage_spec.rb
+++ b/spec/uploaders/object_storage_spec.rb
@@ -1097,19 +1097,31 @@ def escape_path(path)
               let(:fog_config) do
                 Gitlab.config.uploads.object_store.tap do |config|
                   config[:remote_directory] = 'main-bucket'
-                  config[:bucket_prefix] = 'uploads'
+                  config[:bucket_prefix] = 'my/uploads'
                 end
               end
 
               let(:bucket) { 'main-bucket' }
-              let(:fog_file_path) { "uploads/#{final_path}" }
+              let(:fog_file_path) { "my/uploads/#{final_path}" }
 
               it 'stores the file final path in the db without the prefix' do
                 expect { subject }.not_to raise_error
 
-                expect(uploader.store_path).to eq("uploads/#{final_path}")
+                expect(uploader.store_path).to eq("my/uploads/#{final_path}")
                 expect(object.file_final_path).to eq(final_path)
               end
+
+              context 'and file is stored' do
+                subject do
+                  uploader.store!(uploaded_file)
+                end
+
+                it 'completes the matching pending upload entry' do
+                  expect { subject }
+                    .to change { ObjectStorage::PendingDirectUpload.exists?(uploader_class.storage_location_identifier, final_path) }
+                    .to(false)
+                end
+              end
             end
 
             context 'when file is stored' do
-- 
GitLab