Commit 81f821d3 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Rename `build_uploader to `retrieve_uploader``

This change is to clarify that we are not only rebuilding the uploader
but also fetching and caching the file locally, which is an expensive
operation.
parent 43be05a5
...@@ -91,7 +91,7 @@ module UploadsActions ...@@ -91,7 +91,7 @@ module UploadsActions
upload_paths = uploader.upload_paths(params[:filename]) upload_paths = uploader.upload_paths(params[:filename])
upload = Upload.find_by(model: model, uploader: uploader_class.to_s, path: upload_paths) upload = Upload.find_by(model: model, uploader: uploader_class.to_s, path: upload_paths)
upload&.build_uploader upload&.retrieve_uploader
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
......
...@@ -58,9 +58,23 @@ class Upload < ApplicationRecord ...@@ -58,9 +58,23 @@ class Upload < ApplicationRecord
self.checksum = Digest::SHA256.file(absolute_path).hexdigest self.checksum = Digest::SHA256.file(absolute_path).hexdigest
end end
# Initialize the associated Uploader class with current model
#
# @param [String] mounted_as
# @return [GitlabUploader] one of the subclasses, defined at the model's uploader attribute
def build_uploader(mounted_as = nil) def build_uploader(mounted_as = nil)
uploader_class.new(model, mounted_as || mount_point).tap do |uploader| uploader_class.new(model, mounted_as || mount_point).tap do |uploader|
uploader.upload = self uploader.upload = self
end
end
# Initialize the associated Uploader class with current model and
# retrieve existing file from the store to a local cache
#
# @param [String] mounted_as
# @return [GitlabUploader] one of the subclasses, defined at the model's uploader attribute
def retrieve_uploader(mounted_as = nil)
build_uploader(mounted_as).tap do |uploader|
uploader.retrieve_from_store!(identifier) uploader.retrieve_from_store!(identifier)
end end
end end
...@@ -72,7 +86,7 @@ class Upload < ApplicationRecord ...@@ -72,7 +86,7 @@ class Upload < ApplicationRecord
exist = if local? exist = if local?
File.exist?(absolute_path) File.exist?(absolute_path)
else else
build_uploader.exists? retrieve_uploader.exists?
end end
# Help sysadmins find missing upload files # Help sysadmins find missing upload files
...@@ -111,7 +125,7 @@ class Upload < ApplicationRecord ...@@ -111,7 +125,7 @@ class Upload < ApplicationRecord
private private
def delete_file! def delete_file!
build_uploader.remove! retrieve_uploader.remove!
end end
def foreground_checksummable? def foreground_checksummable?
......
...@@ -12,7 +12,7 @@ class ImportIssuesCsvWorker ...@@ -12,7 +12,7 @@ class ImportIssuesCsvWorker
@project = Project.find(project_id) @project = Project.find(project_id)
@upload = Upload.find(upload_id) @upload = Upload.find(upload_id)
importer = Issues::ImportCsvService.new(@user, @project, @upload.build_uploader) importer = Issues::ImportCsvService.new(@user, @project, @upload.retrieve_uploader)
importer.execute importer.execute
@upload.destroy @upload.destroy
......
...@@ -22,7 +22,7 @@ module ObjectStorage ...@@ -22,7 +22,7 @@ module ObjectStorage
def build_uploader(subject, mount_point) def build_uploader(subject, mount_point)
case subject case subject
when Upload then subject.build_uploader(mount_point) when Upload then subject.retrieve_uploader(mount_point)
else else
subject.send(mount_point) # rubocop:disable GitlabSecurity/PublicSend subject.send(mount_point) # rubocop:disable GitlabSecurity/PublicSend
end end
......
...@@ -119,7 +119,7 @@ module ObjectStorage ...@@ -119,7 +119,7 @@ module ObjectStorage
end end
def build_uploaders(uploads) def build_uploaders(uploads)
uploads.map { |upload| upload.build_uploader(@mounted_as) } uploads.map { |upload| upload.retrieve_uploader(@mounted_as) }
end end
def migrate(uploads) def migrate(uploads)
......
...@@ -79,7 +79,7 @@ module Geo ...@@ -79,7 +79,7 @@ module Geo
when :job_artifact when :job_artifact
Ci::JobArtifact.find(object_db_id).file Ci::JobArtifact.find(object_db_id).file
when *Gitlab::Geo::Replication::USER_UPLOADS_OBJECT_TYPES when *Gitlab::Geo::Replication::USER_UPLOADS_OBJECT_TYPES
Upload.find(object_db_id).build_uploader Upload.find(object_db_id).retrieve_uploader
else else
raise NameError, "Unrecognized type: #{object_type}" raise NameError, "Unrecognized type: #{object_type}"
end end
......
...@@ -13,7 +13,7 @@ module Gitlab ...@@ -13,7 +13,7 @@ module Gitlab
return file_not_found(recorded_file) unless recorded_file.exist? return file_not_found(recorded_file) unless recorded_file.exist?
return error('Upload not found') unless valid? return error('Upload not found') unless valid?
success(recorded_file.build_uploader) success(recorded_file.retrieve_uploader)
end end
private private
......
...@@ -30,7 +30,7 @@ module Gitlab ...@@ -30,7 +30,7 @@ module Gitlab
file_type: file_type, file_type: file_type,
file_id: upload.id, file_id: upload.id,
filename: upload.absolute_path, filename: upload.absolute_path,
uploader: upload.build_uploader, uploader: upload.retrieve_uploader,
expected_checksum: upload.checksum, expected_checksum: upload.checksum,
request_data: build_request_data(file_type, upload) request_data: build_request_data(file_type, upload)
} }
...@@ -40,7 +40,7 @@ module Gitlab ...@@ -40,7 +40,7 @@ module Gitlab
{ {
file_type: file_type, file_type: file_type,
file_id: upload.id, file_id: upload.id,
uploader: upload.build_uploader, uploader: upload.retrieve_uploader,
request_data: build_request_data(file_type, upload) request_data: build_request_data(file_type, upload)
} }
end end
......
...@@ -43,12 +43,12 @@ describe Gitlab::Geo::Replication::FileDownloader, :geo do ...@@ -43,12 +43,12 @@ describe Gitlab::Geo::Replication::FileDownloader, :geo do
def stub_geo_file_transfer(file_type, upload) def stub_geo_file_transfer(file_type, upload)
url = primary_node.geo_transfers_url(file_type, upload.id.to_s) url = primary_node.geo_transfers_url(file_type, upload.id.to_s)
stub_request(:get, url).to_return(status: 200, body: upload.build_uploader.file.read, headers: {}) stub_request(:get, url).to_return(status: 200, body: upload.retrieve_uploader.file.read, headers: {})
end end
def stub_geo_file_transfer_object_storage(file_type, upload) def stub_geo_file_transfer_object_storage(file_type, upload)
url = primary_node.geo_transfers_url(file_type, upload.id.to_s) url = primary_node.geo_transfers_url(file_type, upload.id.to_s)
stub_request(:get, url).to_return(status: 307, body: upload.build_uploader.url, headers: {}) stub_request(:get, url).to_return(status: 307, body: upload.retrieve_uploader.url, headers: {})
end end
end end
...@@ -44,7 +44,7 @@ describe Gitlab::Geo::Replication::FileTransfer do ...@@ -44,7 +44,7 @@ describe Gitlab::Geo::Replication::FileTransfer do
context 'when the HTTP response is successful' do context 'when the HTTP response is successful' do
it 'returns a successful result' do it 'returns a successful result' do
content = upload.build_uploader.file.read content = upload.retrieve_uploader.file.read
size = content.bytesize size = content.bytesize
stub_request(:get, subject.resource_url).to_return(status: 200, body: content) stub_request(:get, subject.resource_url).to_return(status: 200, body: content)
......
...@@ -120,7 +120,7 @@ describe Geo::FileRegistryRemovalService do ...@@ -120,7 +120,7 @@ describe Geo::FileRegistryRemovalService do
context 'with avatar' do context 'with avatar' do
let!(:upload) { create(:user, :with_avatar).avatar.upload } let!(:upload) { create(:user, :with_avatar).avatar.upload }
let!(:file_registry) { create(:geo_file_registry, :avatar, file_id: upload.id) } let!(:file_registry) { create(:geo_file_registry, :avatar, file_id: upload.id) }
let!(:file_path) { upload.build_uploader.file.path } let!(:file_path) { upload.retrieve_uploader.file.path }
it_behaves_like 'removes' it_behaves_like 'removes'
...@@ -137,7 +137,7 @@ describe Geo::FileRegistryRemovalService do ...@@ -137,7 +137,7 @@ describe Geo::FileRegistryRemovalService do
context 'with attachment' do context 'with attachment' do
let!(:upload) { create(:note, :with_attachment).attachment.upload } let!(:upload) { create(:note, :with_attachment).attachment.upload }
let!(:file_registry) { create(:geo_file_registry, :attachment, file_id: upload.id) } let!(:file_registry) { create(:geo_file_registry, :attachment, file_id: upload.id) }
let!(:file_path) { upload.build_uploader.file.path } let!(:file_path) { upload.retrieve_uploader.file.path }
it_behaves_like 'removes' it_behaves_like 'removes'
...@@ -154,7 +154,7 @@ describe Geo::FileRegistryRemovalService do ...@@ -154,7 +154,7 @@ describe Geo::FileRegistryRemovalService do
context 'with file' do context 'with file' do
let!(:upload) { create(:user, :with_avatar).avatar.upload } let!(:upload) { create(:user, :with_avatar).avatar.upload }
let!(:file_registry) { create(:geo_file_registry, :avatar, file_id: upload.id) } let!(:file_registry) { create(:geo_file_registry, :avatar, file_id: upload.id) }
let!(:file_path) { upload.build_uploader.file.path } let!(:file_path) { upload.retrieve_uploader.file.path }
it_behaves_like 'removes' it_behaves_like 'removes'
...@@ -177,7 +177,7 @@ describe Geo::FileRegistryRemovalService do ...@@ -177,7 +177,7 @@ describe Geo::FileRegistryRemovalService do
end end
let!(:file_registry) { create(:geo_file_registry, :namespace_file, file_id: upload.id) } let!(:file_registry) { create(:geo_file_registry, :namespace_file, file_id: upload.id) }
let!(:file_path) { upload.build_uploader.file.path } let!(:file_path) { upload.retrieve_uploader.file.path }
it_behaves_like 'removes' it_behaves_like 'removes'
...@@ -199,7 +199,7 @@ describe Geo::FileRegistryRemovalService do ...@@ -199,7 +199,7 @@ describe Geo::FileRegistryRemovalService do
Upload.find_by(model: snippet, uploader: PersonalFileUploader.name) Upload.find_by(model: snippet, uploader: PersonalFileUploader.name)
end end
let!(:file_registry) { create(:geo_file_registry, :personal_file, file_id: upload.id) } let!(:file_registry) { create(:geo_file_registry, :personal_file, file_id: upload.id) }
let!(:file_path) { upload.build_uploader.file.path } let!(:file_path) { upload.retrieve_uploader.file.path }
it_behaves_like 'removes' it_behaves_like 'removes'
...@@ -221,7 +221,7 @@ describe Geo::FileRegistryRemovalService do ...@@ -221,7 +221,7 @@ describe Geo::FileRegistryRemovalService do
Upload.find_by(model: appearance, uploader: FaviconUploader.name) Upload.find_by(model: appearance, uploader: FaviconUploader.name)
end end
let!(:file_registry) { create(:geo_file_registry, :favicon, file_id: upload.id) } let!(:file_registry) { create(:geo_file_registry, :favicon, file_id: upload.id) }
let!(:file_path) { upload.build_uploader.file.path } let!(:file_path) { upload.retrieve_uploader.file.path }
it_behaves_like 'removes' it_behaves_like 'removes'
......
...@@ -92,7 +92,7 @@ module Gitlab ...@@ -92,7 +92,7 @@ module Gitlab
def legacy_file_uploader def legacy_file_uploader
strong_memoize(:legacy_file_uploader) do strong_memoize(:legacy_file_uploader) do
uploader = upload.build_uploader uploader = upload.retrieve_uploader
uploader.retrieve_from_store!(File.basename(upload.path)) uploader.retrieve_from_store!(File.basename(upload.path))
uploader uploader
end end
......
...@@ -68,7 +68,7 @@ module Gitlab ...@@ -68,7 +68,7 @@ module Gitlab
yield(@project.avatar) yield(@project.avatar)
else else
project_uploads_except_avatar(avatar_path).find_each(batch_size: UPLOADS_BATCH_SIZE) do |upload| project_uploads_except_avatar(avatar_path).find_each(batch_size: UPLOADS_BATCH_SIZE) do |upload|
yield(upload.build_uploader) yield(upload.retrieve_uploader)
end end
end end
end end
......
...@@ -68,7 +68,7 @@ module Gitlab ...@@ -68,7 +68,7 @@ module Gitlab
} }
relation.find_each(find_params) do |upload| relation.find_each(find_params) do |upload|
clean(upload.build_uploader, dry_run: dry_run) clean(upload.retrieve_uploader, dry_run: dry_run)
sleep sleep_time if sleep_time sleep sleep_time if sleep_time
rescue => err rescue => err
logger.error "failed to sanitize #{upload_ref(upload)}: #{err.message}" logger.error "failed to sanitize #{upload_ref(upload)}: #{err.message}"
......
...@@ -32,7 +32,7 @@ module Gitlab ...@@ -32,7 +32,7 @@ module Gitlab
end end
def remote_object_exists?(upload) def remote_object_exists?(upload)
upload.build_uploader.file.exists? upload.retrieve_uploader.file.exists?
end end
end end
end end
......
...@@ -32,7 +32,7 @@ describe Gitlab::BackgroundMigration::LegacyUploadMover do ...@@ -32,7 +32,7 @@ describe Gitlab::BackgroundMigration::LegacyUploadMover do
if with_file if with_file
upload = create(:upload, :with_file, :attachment_upload, params) upload = create(:upload, :with_file, :attachment_upload, params)
model.update(attachment: upload.build_uploader) model.update(attachment: upload.retrieve_uploader)
model.attachment.upload model.attachment.upload
else else
create(:upload, :attachment_upload, params) create(:upload, :attachment_upload, params)
......
...@@ -24,7 +24,7 @@ describe Gitlab::BackgroundMigration::LegacyUploadsMigrator do ...@@ -24,7 +24,7 @@ describe Gitlab::BackgroundMigration::LegacyUploadsMigrator do
if with_file if with_file
upload = create(:upload, :with_file, :attachment_upload, params) upload = create(:upload, :with_file, :attachment_upload, params)
model.update(attachment: upload.build_uploader) model.update(attachment: upload.retrieve_uploader)
model.attachment.upload model.attachment.upload
else else
create(:upload, :attachment_upload, params) create(:upload, :attachment_upload, params)
......
...@@ -83,7 +83,7 @@ describe Gitlab::ImportExport::UploadsManager do ...@@ -83,7 +83,7 @@ describe Gitlab::ImportExport::UploadsManager do
it 'restores the file' do it 'restores the file' do
manager.restore manager.restore
expect(project.uploads.map { |u| u.build_uploader.filename }).to include('dummy.txt') expect(project.uploads.map { |u| u.retrieve_uploader.filename }).to include('dummy.txt')
end end
end end
end end
...@@ -27,7 +27,7 @@ describe Gitlab::ImportExport::UploadsRestorer do ...@@ -27,7 +27,7 @@ describe Gitlab::ImportExport::UploadsRestorer do
it 'copies the uploads to the project path' do it 'copies the uploads to the project path' do
subject.restore subject.restore
expect(project.uploads.map { |u| u.build_uploader.filename }).to include('dummy.txt') expect(project.uploads.map { |u| u.retrieve_uploader.filename }).to include('dummy.txt')
end end
end end
...@@ -43,7 +43,7 @@ describe Gitlab::ImportExport::UploadsRestorer do ...@@ -43,7 +43,7 @@ describe Gitlab::ImportExport::UploadsRestorer do
it 'copies the uploads to the project path' do it 'copies the uploads to the project path' do
subject.restore subject.restore
expect(project.uploads.map { |u| u.build_uploader.filename }).to include('dummy.txt') expect(project.uploads.map { |u| u.retrieve_uploader.filename }).to include('dummy.txt')
end end
end end
end end
......
...@@ -58,7 +58,7 @@ describe Gitlab::Sanitizers::Exif do ...@@ -58,7 +58,7 @@ describe Gitlab::Sanitizers::Exif do
end end
describe '#clean' do describe '#clean' do
let(:uploader) { create(:upload, :with_file, :issuable_upload).build_uploader } let(:uploader) { create(:upload, :with_file, :issuable_upload).retrieve_uploader }
context "no dry run" do context "no dry run" do
it "removes exif from the image" do it "removes exif from the image" do
......
...@@ -107,6 +107,16 @@ describe Upload do ...@@ -107,6 +107,16 @@ describe Upload do
end end
end end
describe '#build_uploader' do
it 'returns a uploader object with current upload associated with it' do
subject = build(:upload)
uploader = subject.build_uploader
expect(uploader.upload).to eq(subject)
expect(uploader.mounted_as).to eq(subject.send(:mount_point))
end
end
describe '#needs_checksum??' do describe '#needs_checksum??' do
context 'with local storage' do context 'with local storage' do
it 'returns true when no checksum exists' do it 'returns true when no checksum exists' do
......
...@@ -44,7 +44,7 @@ describe Uploads::Fog do ...@@ -44,7 +44,7 @@ describe Uploads::Fog do
subject { data_store.delete_keys(keys) } subject { data_store.delete_keys(keys) }
before do before do
uploads.each { |upload| upload.build_uploader.migrate!(2) } uploads.each { |upload| upload.retrieve_uploader.migrate!(2) }
end end
it 'deletes multiple data' do it 'deletes multiple data' do
......
...@@ -104,7 +104,7 @@ shared_examples 'handle uploads' do ...@@ -104,7 +104,7 @@ shared_examples 'handle uploads' do
context "when neither the uploader nor the model exists" do context "when neither the uploader nor the model exists" do
before do before do
allow_any_instance_of(Upload).to receive(:build_uploader).and_return(nil) allow_any_instance_of(Upload).to receive(:retrieve_uploader).and_return(nil)
allow(controller).to receive(:find_model).and_return(nil) allow(controller).to receive(:find_model).and_return(nil)
end end
......
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