From df07478009ae8a79ae7309ae97f456a2556810cd Mon Sep 17 00:00:00 2001 From: joker1007 Date: Tue, 5 Jan 2016 20:19:11 +0900 Subject: [PATCH 1/4] Add Tempfile to if statement of BaseUploadCommand#prepare! --- lib/google/apis/core/upload.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/google/apis/core/upload.rb b/lib/google/apis/core/upload.rb index 723dbc94f..cee9d8d97 100644 --- a/lib/google/apis/core/upload.rb +++ b/lib/google/apis/core/upload.rb @@ -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 upload_source.is_a?(IO) || upload_source.is_a?(StringIO) || upload_source.is_a?(Tempfile) self.upload_io = UploadIO.from_io(upload_source, content_type: upload_content_type) @close_io_on_finish = false elsif upload_source.is_a?(String) From da53defdc8bcabdd8f2e7726cbd593bf9c168f71 Mon Sep 17 00:00:00 2001 From: joker1007 Date: Wed, 6 Jan 2016 14:29:58 +0900 Subject: [PATCH 2/4] Add test case for Tempfile input --- spec/google/apis/core/upload_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/google/apis/core/upload_spec.rb b/spec/google/apis/core/upload_spec.rb index e18c60119..fff3f6e82 100644 --- a/spec/google/apis/core/upload_spec.rb +++ b/spec/google/apis/core/upload_spec.rb @@ -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 + 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' From 4b78f99f7a3f69c42b71bf765f6f48d09d6b2f0d Mon Sep 17 00:00:00 2001 From: joker1007 Date: Wed, 6 Jan 2016 14:35:23 +0900 Subject: [PATCH 3/4] Extract long condition statement to its own method --- lib/google/apis/core/upload.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/google/apis/core/upload.rb b/lib/google/apis/core/upload.rb index cee9d8d97..f02e8ddb0 100644 --- a/lib/google/apis/core/upload.rb +++ b/lib/google/apis/core/upload.rb @@ -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) || upload_source.is_a?(Tempfile) + 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 From db1ab7d0b2c423a89a46b540813ebb094cd0b819 Mon Sep 17 00:00:00 2001 From: joker1007 Date: Thu, 7 Jan 2016 03:39:53 +0900 Subject: [PATCH 4/4] Add basename arg to Tempfile constructor --- spec/google/apis/core/upload_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/google/apis/core/upload_spec.rb b/spec/google/apis/core/upload_spec.rb index fff3f6e82..99039f8f0 100644 --- a/spec/google/apis/core/upload_spec.rb +++ b/spec/google/apis/core/upload_spec.rb @@ -131,7 +131,7 @@ RSpec.describe Google::Apis::Core::RawUploadCommand do context('with Tempfile input') do let(:file) do - temp_file = Tempfile.new + temp_file = Tempfile.new("tempfile") temp_file.write("Hello world\n") temp_file.rewind temp_file