Skip to content

Commit

Permalink
Add support for fog streaming uploads
Browse files Browse the repository at this point in the history
Fog supports streaming uploads if passed a File object.
Added SanitizedFile#to_file method that returns a File object.
This works for file system based SanitizedFile (File, Tempfile, Hash, etc)
StringIO must still be read into memory before being passed to fog.
  • Loading branch information
chrisdurtschi committed Sep 23, 2011
1 parent 2a1d3fc commit 7cafd3f
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 127 deletions.
11 changes: 11 additions & 0 deletions lib/carrierwave/sanitized_file.rb
Expand Up @@ -216,6 +216,17 @@ def delete
FileUtils.rm(self.path) if exists?
end

##
# Returns a File object, or nil if it does not exist.
#
# === Returns
#
# [File] a File object representing the SanitizedFile
#
def to_file
File.open(@file) if exists?
end

##
# Returns the content type of the file.
#
Expand Down
2 changes: 1 addition & 1 deletion lib/carrierwave/storage/fog.rb
Expand Up @@ -227,7 +227,7 @@ def size
def store(new_file)
@content_type ||= new_file.content_type
@file = directory.files.create({
:body => new_file.read,
:body => new_file.exists? ? new_file.to_file : new_file.read,
:content_type => @content_type,
:key => path,
:public => @uploader.fog_public
Expand Down
32 changes: 32 additions & 0 deletions spec/sanitized_file_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 7cafd3f

Please sign in to comment.