Merge pull request #330 from joker1007/enable-upload-to-use-tempfile

Add Tempfile to if statement of BaseUploadCommand#prepare!
This commit is contained in:
Steve Bazyl 2016-01-06 12:02:56 -08:00
commit a0d99ba253
2 changed files with 21 additions and 1 deletions

View File

@ -82,7 +82,7 @@ module Google
# @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)
@ -97,6 +97,12 @@ module Google
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

View File

@ -129,6 +129,20 @@ RSpec.describe Google::Apis::Core::RawUploadCommand do
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'