Commit 4e2fd07e authored by Gabriel Mazetto's avatar Gabriel Mazetto

Added replace_file_without_saving! to upload files to the secondary node

parent d07dabd1
......@@ -176,6 +176,17 @@ class FileUploader < GitlabUploader
record_upload # after_store is not triggered
end
# Used to replace an existing upload with the informed file without modifying stored metadata
# Use this method only to repair/replace an existing upload, or to upload to a Geo secondary node
#
# @param [CarrierWave::SanitizedFile] file
# @return CarrierWave::SanitizedFile
def replace_file_without_saving!(file)
raise ArgumentError, 'should be a CarrierWave::SanitizedFile' unless file.is_a? CarrierWave::SanitizedFile
storage.store!(file)
end
private
def apply_context!(uploader_context)
......
......@@ -197,7 +197,7 @@ module Gitlab
file_size = temp_file.size
# Upload file to Object Storage
uploader.send(:storage).store! CarrierWave::SanitizedFile.new(temp_file)
uploader.replace_file_without_saving!(CarrierWave::SanitizedFile.new(temp_file))
log_info("Successful downloaded", filename: filename, file_size_bytes: file_size)
rescue => e
......
......@@ -3,7 +3,7 @@ require 'spec_helper'
describe FileUploader do
let(:group) { create(:group, name: 'awesome') }
let(:project) { create(:project, :legacy_storage, namespace: group, name: 'project') }
let(:uploader) { described_class.new(project) }
let(:uploader) { described_class.new(project, :avatar) }
let(:upload) { double(model: project, path: 'secret/foo.jpg') }
subject { uploader }
......@@ -184,6 +184,14 @@ describe FileUploader do
end
end
describe '#replace_file_without_saving!' do
let(:replacement) { Tempfile.create('replacement.jpg') }
it 'replaces an existing file without changing its metadata' do
expect { subject.replace_file_without_saving! CarrierWave::SanitizedFile.new(replacement) }.not_to change { subject.upload }
end
end
context 'when remote file is used' do
let(:temp_file) { Tempfile.new("test") }
......
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