Skip to content

Commit

Permalink
Merge pull request googleapis#330 from joker1007/enable-upload-to-use…
Browse files Browse the repository at this point in the history
…-tempfile

Add Tempfile to if statement of BaseUploadCommand#prepare!
  • Loading branch information
sqrrrl committed Jan 6, 2016
2 parents d9056b3 + db1ab7d commit a0d99ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/google/apis/core/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class BaseUploadCommand < ApiCommand
# @raise [Google::Apis::ClientError] if upload source is invalid
def prepare!
super
if upload_source.is_a?(IO) || upload_source.is_a?(StringIO)
if streamable?(upload_source)
self.upload_io = UploadIO.from_io(upload_source, content_type: upload_content_type)
@close_io_on_finish = false
elsif upload_source.is_a?(String)
Expand All @@ -97,6 +97,12 @@ def prepare!
def release!
upload_io.close if @close_io_on_finish
end

private

def streamable?(upload_source)
upload_source.is_a?(IO) || upload_source.is_a?(StringIO) || upload_source.is_a?(Tempfile)
end
end

# Implementation of the raw upload protocol
Expand Down
14 changes: 14 additions & 0 deletions spec/google/apis/core/upload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@
end
end

context('with Tempfile input') do
let(:file) do
temp_file = Tempfile.new("tempfile")
temp_file.write("Hello world\n")
temp_file.rewind
temp_file
end
include_examples 'should upload'

it 'should not close stream' do
expect(file.closed?).to be false
end
end

context('with file path input') do
let(:file) { File.join(FIXTURES_DIR, 'files', 'test.txt') }
include_examples 'should upload'
Expand Down

0 comments on commit a0d99ba

Please sign in to comment.