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

Add config to disable ACL in fog storage #2709

Closed
Closed
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ See `carrierwave/uploader/processing.rb` for details.
```ruby
class MyUploader < CarrierWave::Uploader::Base
process :scale => [200, 200], :if => :image?

def image?(carrier_wave_sanitized_file)
true
end
Expand Down Expand Up @@ -764,6 +764,7 @@ CarrierWave.configure do |config|
config.fog_directory = 'name_of_bucket' # required
config.fog_public = false # optional, defaults to true
config.fog_attributes = { cache_control: "public, max-age=#{365.days.to_i}" } # optional, defaults to {}
config.fog_acl = false # optional, defaults to true
# For an application which utilizes multiple servers but does not need caches persisted across requests,
# uncomment the line :file instead of the default :storage. Otherwise, it will use AWS as the temp cache store.
# config.cache_storage = :file
Expand Down
11 changes: 8 additions & 3 deletions lib/carrierwave/storage/fog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,15 @@ def store(new_file)
else
fog_file = new_file.to_file
@content_type ||= new_file.content_type
@file = directory.files.create({

fog_create_file_params = {
:body => fog_file || new_file.read,
:content_type => @content_type,
:key => path,
:public => @uploader.fog_public
}.merge(@uploader.fog_attributes))
}.merge(@uploader.fog_attributes)
fog_create_file_params[:public] = @uploader.fog_public if @uploader.fog_acl

@file = directory.files.create(fog_create_file_params)
fog_file.close if fog_file && !fog_file.closed?
end
true
Expand Down Expand Up @@ -510,6 +513,8 @@ def copy_options
end

def acl_header
return {} unless @uploader.fog_acl

case fog_provider
when 'AWS'
{ 'x-amz-acl' => @uploader.fog_public ? 'public-read' : 'private' }
Expand Down
2 changes: 2 additions & 0 deletions lib/carrierwave/uploader/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Configuration
add_config :remove_previously_stored_files_after_update
add_config :downloader
add_config :force_extension
add_config :fog_acl

# fog
add_deprecated_config :fog_provider
Expand Down Expand Up @@ -193,6 +194,7 @@ def reset_config
config.fog_attributes = {}
config.fog_credentials = {}
config.fog_public = true
config.fog_acl = true
config.fog_authenticated_url_expiration = 600
config.fog_use_ssl_for_aws = true
config.fog_aws_accelerate = false
Expand Down
95 changes: 94 additions & 1 deletion spec/storage/fog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@
end

describe CarrierWave::Storage::Fog::File do
subject(:file) { CarrierWave::Storage::Fog::File.new(nil, nil, nil) }
subject(:file) { CarrierWave::Storage::Fog::File.new(uploader, base, path) }

let(:uploader) { instance_double(CarrierWave::Uploader::Base, fog_attributes: {some: :attribute}, fog_public: true, fog_acl: fog_acl, fog_credentials: {provider: 'AWS'}) }
let(:base) { instance_double(CarrierWave::Storage::Fog) }
let(:path) { 'path/to/file.txt' }
let(:fog_acl) { true }

describe "#filename" do
subject(:filename) { file.filename }
Expand Down Expand Up @@ -131,5 +136,93 @@
end
end
end

describe '#store' do
subject(:store) { file.store(new_file) }

let(:fog_file) { double('Fog::File') }
let(:directory_files) { double('files', head: fog_file) }
let(:directory) { double('directory', files: directory_files) }
let(:fog_directory) { 'a-wonderful-fog-directory' }

before do
allow(uploader).to receive(:fog_directory).and_return(fog_directory)
connection = double('connection', directories: double('directories', new: directory))
allow(base).to receive(:connection).and_return(connection)
end

context 'when fog_acl is true' do
let(:fog_acl) { true }

context 'and new_file is a Fog::File' do
let(:new_file) { CarrierWave::Storage::Fog::File.new(uploader, base, path) }

it 'stores the file' do
expected_options = {some: :attribute, "x-amz-acl" => "public-read"}
expect(fog_file).to receive(:copy).with(fog_directory, path, expected_options)

expect(store).to be true
end
end

context 'and new_file is a SanitizedFile' do
let(:new_file) { CarrierWave::SanitizedFile.new(file_path('test.jpg')) }

before do
# stub this to always return the same value so that the equality comparison works
allow(new_file).to receive(:to_file).and_return(File.new(file_path('test.jpg')))
end

it 'stores the file' do
fog_create_file_params = {
body: new_file.to_file,
content_type: 'application/octet-stream',
key: path,
public: true,
some: :attribute
}
expect(directory_files).to receive(:create).with(fog_create_file_params)

expect(store).to be true
end
end
end

context 'when fog_acl is false' do
let(:fog_acl) { false }

context 'and new_file is a Fog::File' do
let(:new_file) { CarrierWave::Storage::Fog::File.new(uploader, base, path) }

it 'stores the file' do
expected_options = {some: :attribute}
expect(fog_file).to receive(:copy).with(fog_directory, path, expected_options)

expect(store).to be true
end
end

context 'and new_file is a SanitizedFile' do
let(:new_file) { CarrierWave::SanitizedFile.new(file_path('test.jpg')) }

before do
# stub this to always return the same value so that the equality comparison works
allow(new_file).to receive(:to_file).and_return(File.new(file_path('test.jpg')))
end

it 'stores the file' do
fog_create_file_params = {
body: new_file.to_file,
content_type: 'application/octet-stream',
key: path,
some: :attribute
}
expect(directory_files).to receive(:create).with(fog_create_file_params)

expect(store).to be true
end
end
end
end
end
end