Skip to content
Browse files

Merge pull request #468 from chrisdurtschi/fog_streaming_upload

Fog streaming upload
  • Loading branch information...
2 parents 2a1d3fc + 1c48854 commit ba8b9d4de5519a367d7731662d8efa8472c2f999 @bensie bensie committed Sep 23, 2011
Showing with 227 additions and 127 deletions.
  1. +12 −0 lib/carrierwave/sanitized_file.rb
  2. +2 −1 lib/carrierwave/storage/fog.rb
  3. +32 −0 spec/sanitized_file_spec.rb
  4. +181 −126 spec/storage/fog_helper.rb
View
12 lib/carrierwave/sanitized_file.rb
@@ -217,6 +217,18 @@ def delete
end
##
+ # Returns a File object, or nil if it does not exist.
+ #
+ # === Returns
+ #
+ # [File] a File object representing the SanitizedFile
+ #
+ def to_file
+ return @file if @file.is_a?(File)
+ File.open(path) if exists?
+ end
+
+ ##
# Returns the content type of the file.
#
# === Returns
View
3 lib/carrierwave/storage/fog.rb
@@ -225,9 +225,10 @@ def size
#
# [Boolean] true on success or raises error
def store(new_file)
+ fog_file = new_file.to_file
@content_type ||= new_file.content_type
@file = directory.files.create({
- :body => new_file.read,
+ :body => fog_file ? fog_file : new_file.read,
:content_type => @content_type,
:key => path,
:public => @uploader.fog_public
View
32 spec/sanitized_file_spec.rb
@@ -383,6 +383,20 @@
File.exists?(@sanitized_file.path).should be_false
end
end
+
+ describe '#to_file' do
+ it "should return a File object" do
+ @sanitized_file.to_file.should be_a(File)
+ end
+
+ it "should have the same path as the SanitizedFile" do
+ @sanitized_file.to_file.path.should == @sanitized_file.path
+ end
+
+ it "should have the same contents as the SantizedFile" do
+ @sanitized_file.to_file.read.should == @sanitized_file.read
+ end
+ end
end
describe "with a valid Hash" do
@@ -470,6 +484,12 @@
end
end
+ describe '#to_file' do
+ it "should be nil" do
+ @sanitized_file.to_file.should be_nil
+ end
+ end
+
end
describe "with a valid File object" do
@@ -614,6 +634,12 @@
running { @empty.delete }.should_not raise_error
end
end
+
+ describe '#to_file' do
+ it "should be nil" do
+ @empty.to_file.should be_nil
+ end
+ end
end
describe "that is an empty string" do
@@ -680,6 +706,12 @@
running { @empty.delete }.should_not raise_error
end
end
+
+ describe '#to_file' do
+ it "should be nil" do
+ @empty.to_file.should be_nil
+ end
+ end
end
end
View
307 spec/storage/fog_helper.rb
@@ -1,182 +1,237 @@
def fog_tests(fog_credentials)
describe CarrierWave::Storage::Fog do
describe fog_credentials[:provider] do
- before do
- CarrierWave.configure do |config|
- config.reset_config
- config.fog_attributes = {}
- config.fog_credentials = fog_credentials
- config.fog_directory = CARRIERWAVE_DIRECTORY
- config.fog_host = nil
- config.fog_public = true
- end
- eval <<-RUBY
+ shared_examples_for "#{fog_credentials[:provider]} storage" do
+
+ before do
+ CarrierWave.configure do |config|
+ config.reset_config
+ config.fog_attributes = {}
+ config.fog_credentials = fog_credentials
+ config.fog_directory = CARRIERWAVE_DIRECTORY
+ config.fog_host = nil
+ config.fog_public = true
+ end
+
+ eval <<-RUBY
class FogSpec#{fog_credentials[:provider]}Uploader < CarrierWave::Uploader::Base
storage :fog
end
- RUBY
+ RUBY
- @provider = fog_credentials[:provider]
+ @provider = fog_credentials[:provider]
- # @uploader = FogSpecUploader.new
- @uploader = eval("FogSpec#{@provider}Uploader")
- @uploader.stub!(:store_path).and_return('uploads/test.jpg')
+ # @uploader = FogSpecUploader.new
+ @uploader = eval("FogSpec#{@provider}Uploader")
+ @uploader.stub!(:store_path).and_return('uploads/test.jpg')
- @storage = CarrierWave::Storage::Fog.new(@uploader)
- @directory = @storage.connection.directories.get(CARRIERWAVE_DIRECTORY) || @storage.connection.directories.create(:key => CARRIERWAVE_DIRECTORY, :public => true)
+ @storage = CarrierWave::Storage::Fog.new(@uploader)
+ @directory = @storage.connection.directories.get(CARRIERWAVE_DIRECTORY) || @storage.connection.directories.create(:key => CARRIERWAVE_DIRECTORY, :public => true)
+ end
- @file = CarrierWave::SanitizedFile.new(
- :tempfile => StringIO.new(File.open(file_path('test.jpg')).read),
- :filename => 'test.jpg',
- :content_type => 'image/jpeg'
- )
- end
+ describe '#store!' do
+ before do
+ @uploader.stub!(:store_path).and_return('uploads/test.jpg')
+ @fog_file = @storage.store!(@file)
+ end
- describe '#store!' do
- before do
- @uploader.stub!(:store_path).and_return('uploads/test.jpg')
- @fog_file = @storage.store!(@file)
- end
+ it "should upload the file" do
+ @directory.files.get('uploads/test.jpg').body.should == 'this is stuff'
+ end
- it "should upload the file" do
- @directory.files.get('uploads/test.jpg').body.should == 'this is stuff'
- end
+ it "should have a path" do
+ @fog_file.path.should == 'uploads/test.jpg'
+ end
- it "should have a path" do
- @fog_file.path.should == 'uploads/test.jpg'
- end
+ it "should have a content_type" do
+ @fog_file.content_type.should == 'image/jpeg'
+ @directory.files.get('uploads/test.jpg').content_type.should == 'image/jpeg'
+ end
- it "should have a content_type" do
- @fog_file.content_type.should == 'image/jpeg'
- @directory.files.get('uploads/test.jpg').content_type.should == 'image/jpeg'
- end
+ context "without fog_host" do
+ it "should have a public_url" do
+ unless fog_credentials[:provider] == 'Local'
+ @fog_file.public_url.should_not be_nil
+ end
+ end
- context "without fog_host" do
- it "should have a public_url" do
- unless fog_credentials[:provider] == 'Local'
- @fog_file.public_url.should_not be_nil
+ it "should have a url" do
+ unless fog_credentials[:provider] == 'Local'
+ @fog_file.url.should_not be_nil
+ end
end
end
- it "should have a url" do
- unless fog_credentials[:provider] == 'Local'
- @fog_file.url.should_not be_nil
+ context "with fog_host" do
+ it "should have a fog_host rooted public_url" do
+ @uploader.stub!(:fog_host).and_return('http://foo.bar')
+ @fog_file.public_url.should == 'http://foo.bar/uploads/test.jpg'
end
- end
- end
- context "with fog_host" do
- it "should have a fog_host rooted public_url" do
- @uploader.stub!(:fog_host).and_return('http://foo.bar')
- @fog_file.public_url.should == 'http://foo.bar/uploads/test.jpg'
+ it "should have a fog_host rooted url" do
+ @uploader.stub!(:fog_host).and_return('http://foo.bar')
+ @fog_file.url.should == 'http://foo.bar/uploads/test.jpg'
+ end
+
+ it "should always have the same fog_host rooted url" do
+ @uploader.stub!(:fog_host).and_return('http://foo.bar')
+ @fog_file.url.should == 'http://foo.bar/uploads/test.jpg'
+ @fog_file.url.should == 'http://foo.bar/uploads/test.jpg'
+ end
end
- it "should have a fog_host rooted url" do
- @uploader.stub!(:fog_host).and_return('http://foo.bar')
- @fog_file.url.should == 'http://foo.bar/uploads/test.jpg'
+ it "should return filesize" do
+ @fog_file.size.should == 13
end
- it "should always have the same fog_host rooted url" do
- @uploader.stub!(:fog_host).and_return('http://foo.bar')
- @fog_file.url.should == 'http://foo.bar/uploads/test.jpg'
- @fog_file.url.should == 'http://foo.bar/uploads/test.jpg'
+ it "should be deletable" do
+ @fog_file.delete
+ @directory.files.head('uploads/test.jpg').should == nil
end
end
- it "should return filesize" do
- @fog_file.size.should == 13
- end
+ describe '#retrieve!' do
+ before do
+ @directory.files.create(:key => 'uploads/test.jpg', :body => 'A test, 1234', :public => true)
+ @uploader.stub!(:store_path).with('test.jpg').and_return('uploads/test.jpg')
+ @fog_file = @storage.retrieve!('test.jpg')
+ end
- it "should be deletable" do
- @fog_file.delete
- @directory.files.head('uploads/test.jpg').should == nil
- end
- end
+ it "should retrieve the file contents" do
+ @fog_file.read.chomp.should == "A test, 1234"
+ end
- describe '#retrieve!' do
- before do
- @directory.files.create(:key => 'uploads/test.jpg', :body => 'A test, 1234', :public => true)
- @uploader.stub!(:store_path).with('test.jpg').and_return('uploads/test.jpg')
- @fog_file = @storage.retrieve!('test.jpg')
- end
+ it "should have a path" do
+ @fog_file.path.should == 'uploads/test.jpg'
+ end
- it "should retrieve the file contents" do
- @fog_file.read.chomp.should == "A test, 1234"
- end
+ it "should have a public url" do
+ unless fog_credentials[:provider] == 'Local'
+ @fog_file.public_url.should_not be_nil
+ end
+ end
- it "should have a path" do
- @fog_file.path.should == 'uploads/test.jpg'
- end
+ it "should return filesize" do
+ @fog_file.size.should == 12
+ end
- it "should have a public url" do
- unless fog_credentials[:provider] == 'Local'
- @fog_file.public_url.should_not be_nil
+ it "should be deletable" do
+ @fog_file.delete
+ @directory.files.head('uploads/test.jpg').should == nil
end
end
- it "should return filesize" do
- @fog_file.size.should == 12
- end
+ describe 'fog_public' do
- it "should be deletable" do
- @fog_file.delete
- @directory.files.head('uploads/test.jpg').should == nil
- end
- end
+ context "true" do
+ before do
+ directory_key = "#{CARRIERWAVE_DIRECTORY}public"
+ @directory = @storage.connection.directories.create(:key => directory_key, :public => true)
+ @uploader.stub!(:fog_directory).and_return(directory_key)
+ @uploader.stub!(:store_path).and_return('uploads/public.txt')
+ @fog_file = @storage.store!(@file)
+ end
- describe 'fog_public' do
+ after do
+ @directory.files.new(:key => 'uploads/public.txt').destroy
+ @directory.destroy
+ end
- context "true" do
- before do
- directory_key = "#{CARRIERWAVE_DIRECTORY}public"
- @directory = @storage.connection.directories.create(:key => directory_key, :public => true)
- @uploader.stub!(:fog_directory).and_return(directory_key)
- @uploader.stub!(:store_path).and_return('uploads/public.txt')
- @fog_file = @storage.store!(@file)
+ it "should be available at public URL" do
+ unless Fog.mocking? || fog_credentials[:provider] == 'Local'
+ open(@fog_file.public_url).read.should == 'this is stuff'
+ end
+ end
end
- after do
- @directory.files.new(:key => 'uploads/public.txt').destroy
- @directory.destroy
- end
+ context "false" do
+ before do
+ directory_key = "#{CARRIERWAVE_DIRECTORY}private"
+ @directory = @storage.connection.directories.create(:key => directory_key, :public => true)
+ @uploader.stub!(:fog_directory).and_return(directory_key)
+ @uploader.stub!(:fog_public).and_return(false)
+ @uploader.stub!(:store_path).and_return('uploads/private.txt')
+ @fog_file = @storage.store!(@file)
+ end
- it "should be available at public URL" do
- unless Fog.mocking? || fog_credentials[:provider] == 'Local'
- open(@fog_file.public_url).read.should == 'this is stuff'
+ after do
+ @directory.files.new(:key => 'uploads/private.txt').destroy
+ @directory.destroy
end
- end
- end
- context "false" do
- before do
- directory_key = "#{CARRIERWAVE_DIRECTORY}private"
- @directory = @storage.connection.directories.create(:key => directory_key)
- @uploader.stub!(:fog_directory).and_return(directory_key)
- @uploader.stub!(:fog_public).and_return(false)
- @uploader.stub!(:store_path).and_return('uploads/private.txt')
- @fog_file = @storage.store!(@file)
+ it "should have an authenticated_url" do
+ if ['AWS', 'Google'].include?(@provider)
+ @fog_file.authenticated_url.should_not be_nil
+ end
+ end
end
+ end
- after do
- @directory.files.new(:key => 'uploads/private.txt').destroy
+ context 'finishing' do
+ it "should destroy the directory" do # hack, but after never does what/when I want
@directory.destroy
end
-
- it "should have an authenticated_url" do
- if ['AWS', 'Google'].include?(@provider)
- @fog_file.authenticated_url.should_not be_nil
- end
- end
end
+
end
- context 'finishing' do
- it "should destroy the directory" do # hack, but after never does what/when I want
- @directory.destroy
- end
+ end
+
+ describe "with a valid Hash" do
+ before do
+ @file = CarrierWave::SanitizedFile.new(
+ :tempfile => stub_merb_tempfile('test.jpg'),
+ :filename => 'test.jpg',
+ :content_type => 'image/jpeg'
+ )
end
+ it_should_behave_like "#{fog_credentials[:provider]} storage"
end
+
+ describe "with a valid Tempfile" do
+ before do
+ @file = CarrierWave::SanitizedFile.new(stub_tempfile('test.jpg', 'image/jpeg'))
+ end
+
+ it_should_behave_like "#{fog_credentials[:provider]} storage"
+ end
+
+ describe "with a valid StringIO" do
+ before do
+ @file = CarrierWave::SanitizedFile.new(stub_stringio('test.jpg', 'image/jpeg'))
+ end
+
+ it_should_behave_like "#{fog_credentials[:provider]} storage"
+ end
+
+ describe "with a valid File object" do
+ before do
+ @file = CarrierWave::SanitizedFile.new(stub_file('test.jpg', 'image/jpeg'))
+ @file.should_not be_empty
+ end
+
+ it_should_behave_like "#{fog_credentials[:provider]} storage"
+ end
+
+ describe "with a valid path" do
+ before do
+ @file = CarrierWave::SanitizedFile.new(file_path('test.jpg'))
+ @file.should_not be_empty
+ end
+
+ it_should_behave_like "#{fog_credentials[:provider]} storage"
+ end
+
+ describe "with a valid Pathname" do
+ before do
+ @file = CarrierWave::SanitizedFile.new(Pathname.new(file_path('test.jpg')))
+ @file.should_not be_empty
+ end
+
+ it_should_behave_like "#{fog_credentials[:provider]} storage"
+ end
+
end
end

0 comments on commit ba8b9d4

Please sign in to comment.
Something went wrong with that request. Please try again.