Commit de2e069c authored by Stan Hu's avatar Stan Hu

Fix division by error when upload max size is set to 0

When pre-signed multipart URLs are generated and the max size is 0,
previously the code would error out while calculating the URLs.

We now handle this case by assuming the file is the maximum number of
parts.

Relates to https://gitlab.com/gitlab-org/gitlab/-/issues/259801
parent 7ff19f72
---
title: Fix division by error when upload max size is set to 0
merge_request: 49482
author:
type: fixed
......@@ -184,15 +184,20 @@ module ObjectStorage
private
def rounded_multipart_part_size
# round multipart_part_size up to minimum_mulitpart_size
# round multipart_part_size up to minimum_multipart_size
(multipart_part_size + MINIMUM_MULTIPART_SIZE - 1) / MINIMUM_MULTIPART_SIZE * MINIMUM_MULTIPART_SIZE
end
def multipart_part_size
return MINIMUM_MULTIPART_SIZE if maximum_size == 0
maximum_size / number_of_multipart_parts
end
def number_of_multipart_parts
# If we don't have max length, we can only assume the file is as large as possible.
return MAXIMUM_MULTIPART_PARTS if maximum_size == 0
[
# round maximum_size up to minimum_mulitpart_size
(maximum_size + MINIMUM_MULTIPART_SIZE - 1) / MINIMUM_MULTIPART_SIZE,
......@@ -201,7 +206,11 @@ module ObjectStorage
end
def requires_multipart_upload?
config.aws? && !has_length
config.aws? && !has_length && can_use_multipart_upload?
end
def can_use_multipart_upload?
(maximum_size == 0) || (maximum_size > MINIMUM_MULTIPART_SIZE)
end
def upload_id
......
......@@ -340,6 +340,26 @@ RSpec.describe ObjectStorage::DirectUpload do
stub_object_storage_multipart_init(storage_url, "myUpload")
end
context 'when maximum upload size is 0' do
let(:maximum_size) { 0 }
it 'returns maximum number of parts' do
expect(subject[:MultipartUpload][:PartURLs].length).to eq(100)
end
it 'part size is minimum, 5MB' do
expect(subject[:MultipartUpload][:PartSize]).to eq(5.megabyte)
end
end
context 'when maximum upload size is < 5 MB' do
let(:maximum_size) { 1024 }
it 'does not include multipart URLs' do
expect(subject).not_to include(:MultipartUpload)
end
end
context 'when maximum upload size is 10MB' do
let(:maximum_size) { 10.megabyte }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment