@@ -23,6 +23,8 @@ module Core
2323 # Streaming/resumable media download support
2424 class DownloadCommand < ApiCommand
2525 RANGE_HEADER = 'Range'
26+
27+ # @deprecated No longer used
2628 OK_STATUS = [ 200 , 201 , 206 ]
2729
2830 # File or IO to write content to
@@ -61,8 +63,7 @@ def release!
6163 # of file content.
6264 #
6365 # @private
64- # @param [HTTPClient] client
65- # HTTP client
66+ # @param [Faraday::Connection] client Faraday connection
6667 # @yield [result, err] Result or error if block supplied
6768 # @return [Object]
6869 # @raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried
@@ -78,39 +79,39 @@ def execute_once(client, &block)
7879 request_header [ RANGE_HEADER ] = sprintf ( 'bytes=%d-' , @offset )
7980 end
8081
81- http_res = client . get ( url . to_s ,
82- query : query ,
83- header : request_header ,
84- follow_redirect : true ) do |res , chunk |
85- status = res . http_header . status_code . to_i
86- next unless OK_STATUS . include? ( status )
82+ http_res = client . get ( url . to_s , query , request_header ) do |request |
83+ request . options . on_data = proc do |chunk , _size , res |
84+ status = res . status . to_i
85+ next if chunk . nil? || ( status >= 300 && status < 400 )
8786
88- download_offset ||= ( status == 206 ? @offset : 0 )
89- download_offset += chunk . bytesize
87+ # HTTP 206 is Partial Content
88+ download_offset ||= ( status == 206 ? @offset : 0 )
89+ download_offset += chunk . bytesize
9090
91- if download_offset - chunk . bytesize == @offset
92- next_chunk = chunk
93- else
94- # Oh no! Requested a chunk, but received the entire content
95- chunk_index = @offset - ( download_offset - chunk . bytesize )
96- next_chunk = chunk . byteslice ( chunk_index ..-1 )
97- next if next_chunk . nil?
98- end
91+ if download_offset - chunk . bytesize == @offset
92+ next_chunk = chunk
93+ else
94+ # Oh no! Requested a chunk, but received the entire content
95+ chunk_index = @offset - ( download_offset - chunk . bytesize )
96+ next_chunk = chunk . byteslice ( chunk_index ..-1 )
97+ next if next_chunk . nil?
98+ end
9999
100- # logger.debug { sprintf('Writing chunk (%d bytes, %d total)', chunk.length, bytes_read) }
101- @download_io . write ( next_chunk )
100+ # logger.debug { sprintf('Writing chunk (%d bytes, %d total)', chunk.length, bytes_read) }
101+ @download_io . write ( next_chunk )
102102
103- @offset += next_chunk . bytesize
103+ @offset += next_chunk . bytesize
104+ end
104105 end
105106
106- @download_io . flush if @download_io . respond_to? ( :flush )
107+ @download_io . flush if @download_io . respond_to? ( :flush )
107108
108109 if @close_io_on_finish
109110 result = nil
110111 else
111112 result = @download_io
112113 end
113- check_status ( http_res . status . to_i , http_res . header , http_res . body )
114+ check_status ( http_res . status . to_i , http_res . headers , http_res . body )
114115 success ( result , &block )
115116 rescue => e
116117 @download_io . flush if @download_io . respond_to? ( :flush )
0 commit comments