Fix 404s with snippet uploads in object storage

Previously, an HTTP request for
`/uploads/-/system/personal_snippet/:snippet_id/:hash/:filename` would
look for an uploader of `PersonalFileUploader` class and use
`PersonalFileUploader#upload_paths` to search the datbase for one of the
following paths:

1. `:hash/:filename`
2. `uploads/-/system/personal_snippet/:id/:hash/:filename`

However, when the upload were stored in object storage,
`PersonalFileUploader#store_dirs` stored the path as:

`personal_snippet/:snippet_id/:hash`

The extraneous `uploads/-/system` prefix prevented the path from being
matched, and uploads in object storage would return a 404 error. Uploads
in local storage would work fine.

To fix this, we set the `#base_dir` properly so that `#upload_paths`
generates the right value for object storage. Note that this also makes
`#store_dirs` do the right thing in `FileUploader`.

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/52595
This commit is contained in:
Stan Hu 2019-01-21 16:03:37 -08:00
parent c141d0afb1
commit 940ad0c7a1
3 changed files with 29 additions and 26 deletions

View File

@ -6,8 +6,15 @@ class PersonalFileUploader < FileUploader
options.storage_path
end
def self.base_dir(model, _store = nil)
File.join(options.base_dir, model_path_segment(model))
def self.base_dir(model, store = nil)
base_dirs(model)[store || Store::LOCAL]
end
def self.base_dirs(model)
{
Store::LOCAL => File.join(options.base_dir, model_path_segment(model)),
Store::REMOTE => model_path_segment(model)
}
end
def self.model_path_segment(model)
@ -33,13 +40,6 @@ class PersonalFileUploader < FileUploader
store_dirs[object_store]
end
def store_dirs
{
Store::LOCAL => File.join(base_dir, dynamic_segment),
Store::REMOTE => File.join(self.class.model_path_segment(model), dynamic_segment)
}
end
private
def secure_url

View File

@ -0,0 +1,5 @@
---
title: Fix 404s with snippet uploads in object storage
merge_request: 24550
author:
type: fixed

View File

@ -4,19 +4,13 @@ describe PersonalFileUploader do
let(:model) { create(:personal_snippet) }
let(:uploader) { described_class.new(model) }
let(:upload) { create(:upload, :personal_snippet_upload) }
let(:identifier) { %r{\h+/\S+} }
subject { uploader }
it_behaves_like 'builds correct paths' do
let(:patterns) do
{
store_dir: %r[uploads/-/system/personal_snippet/\d+],
upload_path: identifier,
absolute_path: %r[#{CarrierWave.root}/uploads/-/system/personal_snippet/\d+/#{identifier}]
}
end
end
it_behaves_like 'builds correct paths',
store_dir: %r[uploads/-/system/personal_snippet/\d+],
upload_path: %r[\h+/\S+],
absolute_path: %r[#{CarrierWave.root}/uploads/-/system/personal_snippet\/\d+\/\h+\/\S+$]
context "object_store is REMOTE" do
before do
@ -25,13 +19,17 @@ describe PersonalFileUploader do
include_context 'with storage', described_class::Store::REMOTE
it_behaves_like 'builds correct paths' do
let(:patterns) do
{
store_dir: %r[\d+/\h+],
upload_path: identifier
}
end
it_behaves_like 'builds correct paths',
store_dir: %r[\d+/\h+],
upload_path: %r[^personal_snippet\/\d+\/\h+\/<filename>]
end
describe '#upload_paths' do
it 'builds correct paths for both local and remote storage' do
paths = uploader.upload_paths('test.jpg')
expect(paths.first).to match(%r[\h+\/test.jpg])
expect(paths.second).to match(%r[^personal_snippet\/\d+\/\h+\/test.jpg])
end
end