Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CarrierWave::Storage::Fog#copy_to with Content-Type header #2503

Merged
merged 4 commits into from
Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/carrierwave/storage/fog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def filename(options = {})
# @return [CarrierWave::Storage::Fog::File] the location where the file will be stored.
#
def copy_to(new_path)
connection.copy_object(@uploader.fog_directory, file.key, @uploader.fog_directory, new_path, acl_header)
connection.copy_object(@uploader.fog_directory, file.key, @uploader.fog_directory, new_path, uploader_options)
CarrierWave::Storage::Fog::File.new(@uploader, @base, new_path)
end

Expand Down Expand Up @@ -494,6 +494,13 @@ def file
@file ||= directory.files.head(path)
end

def uploader_options
options = {}
options.merge!(acl_header) if acl_header.present?
options['Content-Type'] ||= content_type if content_type
options.merge(@uploader.fog_attributes)
end

def acl_header
if fog_provider == 'AWS'
{ 'x-amz-acl' => @uploader.fog_public ? 'public-read' : 'private' }
Expand Down
34 changes: 31 additions & 3 deletions spec/storage/fog_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,34 @@ def check_file
end
end

context '#uploader_options' do
let(:store_path) { 'uploads/test+.jpg' }
let(:fog_attributes) { { 'x-amz-server-side-encryption' => true } }

before do
allow(@uploader).to receive(:store_path).and_return(store_path)

if @provider == 'AWS'
allow(@uploader).to receive(:fog_attributes).and_return(fog_attributes)
end
end

it 'includes custom attributes' do
if file.is_a?(CarrierWave::Storage::Fog::File)
if @provider == 'AWS'
expect(@storage.connection).to receive(:copy_object)
.with(anything, anything, anything, anything,
{ "Content-Type"=>file.content_type, "x-amz-acl"=>"public-read", 'x-amz-server-side-encryption' => true }).and_call_original
else
expect(@storage.connection).to receive(:copy_object)
.with(anything, anything, anything, anything, { "Content-Type"=>file.content_type }).and_call_original
end

@storage.store!(file)
end
end
end

context '#acl_header' do
let(:store_path) { 'uploads/test+.jpg' }

Expand All @@ -68,13 +96,13 @@ def check_file
if file.is_a?(CarrierWave::Storage::Fog::File)
if @provider == 'AWS'
expect(@storage.connection).to receive(:copy_object)
.with(anything, anything, anything, anything, { "x-amz-acl"=>"public-read" }).and_call_original
.with(anything, anything, anything, anything, { "Content-Type"=>file.content_type, "x-amz-acl"=>"public-read" }).and_call_original
elsif @provider == 'Google'
expect(@storage.connection).to receive(:copy_object)
.with(anything, anything, anything, anything, { destination_predefined_acl: "publicRead" }).and_call_original
.with(anything, anything, anything, anything, { "Content-Type"=>file.content_type, destination_predefined_acl: "publicRead" }).and_call_original
else
expect(@storage.connection).to receive(:copy_object)
.with(anything, anything, anything, anything, {}).and_call_original
.with(anything, anything, anything, anything, { "Content-Type"=>file.content_type }).and_call_original
end
end

Expand Down