Resumable uploader.
Creates a new uploader.
@param [Hash] options
Request options
# File lib/google/api_client/media.rb, line 122 def initialize(options={}) super options self.uri = options[:uri] self.http_method = :put @offset = options[:offset] || 0 @complete = false @expired = false end
Check if upload is complete
@return [TrueClass, FalseClass]
Whether or not the upload complete successfully
# File lib/google/api_client/media.rb, line 164 def complete? return @complete end
Check if the upload URL expired (upload not completed in alotted time.) Expired uploads must be restarted from the beginning
@return [TrueClass, FalseClass]
Whether or not the upload has expired and can not be resumed
# File lib/google/api_client/media.rb, line 174 def expired? return @expired end
Check the result from the server, updating the offset and/or location if available.
@api private
@param [Faraday::Response] response
HTTP response
@return [Google::APIClient::Result]
Processed API response
# File lib/google/api_client/media.rb, line 227 def process_http_response(response) case response.status when 200...299 @complete = true when 308 range = response.headers['range'] if range @offset = range.scan(/\d+/).collect{|x| Integer(x)}.last + 1 end if response.headers['location'] self.uri = response.headers['location'] end when 400...499 @expired = true when 500...599 # Invalidate the offset to mark it needs to be queried on the # next request @offset = nil end return Google::APIClient::Result.new(self, response) end
Check if upload is resumable. That is, neither complete nor expired
@return [TrueClass, FalseClass] True if upload can be resumed
# File lib/google/api_client/media.rb, line 182 def resumable? return !(self.complete? or self.expired?) end
Sends all remaining chunks to the server
@deprecated Pass the instance to {Google::APIClient#execute} instead
@param [Google::APIClient] api_client
API Client instance to use for sending
# File lib/google/api_client/media.rb, line 138 def send_all(api_client) result = nil until complete? result = send_chunk(api_client) break unless result.status == 308 end return result end
Sends the next chunk to the server
@deprecated Pass the instance to {Google::APIClient#execute} instead
@param [Google::APIClient] api_client
API Client instance to use for sending
# File lib/google/api_client/media.rb, line 155 def send_chunk(api_client) return api_client.execute(self) end
Hashified verison of the API request
@return [Hash]
# File lib/google/api_client/media.rb, line 253 def to_hash super.merge(:offset => @offset) end
Convert to an HTTP request. Returns components in order of method, URI, request headers, and body
@api private
@return [Array<(Symbol, Addressable::URI, Hash, [read,to_str])>]
# File lib/google/api_client/media.rb, line 193 def to_http_request if @complete raise Google::APIClient::ClientError, "Upload already complete" elsif @offset.nil? self.headers.update({ 'Content-Length' => "0", 'Content-Range' => "bytes */#{media.length}" }) else start_offset = @offset remaining = self.media.length - start_offset chunk_size = self.media.chunk_size || self.chunk_size || self.media.length content_length = [remaining, chunk_size].min chunk = RangedIO.new(self.media.io, start_offset, content_length) end_offset = start_offset + content_length - 1 self.headers.update({ 'Content-Length' => "#{content_length}", 'Content-Type' => self.media.content_type, 'Content-Range' => "bytes #{start_offset}-#{end_offset}/#{media.length}" }) self.body = chunk end super end
Generated with the Darkfish Rdoc Generator 2.