Class: Google::APIClient::ResumableUpload

Inherits:
Request
  • Object
show all
Defined in:
lib/google/api_client/media.rb

Overview

Resumable uploader.

Constant Summary

Constants inherited from Request

Google::APIClient::Request::MULTIPART_BOUNDARY

Instance Attribute Summary collapse

Attributes inherited from Request

#api_method, #authenticated, #authorization, #body, #headers, #http_method, #media, #parameters, #upload_type, #uri

Instance Method Summary collapse

Methods inherited from Request

#send, #to_env

Methods included from Logging

#logger

Constructor Details

#initialize(options = {}) ⇒ ResumableUpload

Creates a new uploader.

Parameters:

  • options (Hash) (defaults to: {})

    Request options



122
123
124
125
126
127
128
129
# 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

Instance Attribute Details

#chunk_sizeFixnum

Returns Max bytes to send in a single request

Returns:

  • (Fixnum)

    Max bytes to send in a single request



115
116
117
# File 'lib/google/api_client/media.rb', line 115

def chunk_size
  @chunk_size
end

Instance Method Details

#complete?TrueClass, FalseClass

Check if upload is complete

Returns:

  • (TrueClass, FalseClass)

    Whether or not the upload complete successfully



164
165
166
# File 'lib/google/api_client/media.rb', line 164

def complete?
  return @complete
end

#expired?TrueClass, FalseClass

Check if the upload URL expired (upload not completed in alotted time.) Expired uploads must be restarted from the beginning

Returns:

  • (TrueClass, FalseClass)

    Whether or not the upload has expired and can not be resumed



174
175
176
# File 'lib/google/api_client/media.rb', line 174

def expired?
  return @expired
end

#process_http_response(response) ⇒ Google::APIClient::Result

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check the result from the server, updating the offset and/or location if available.

Parameters:

  • response (Faraday::Response)

    HTTP response

Returns:



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# 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

#resumable?TrueClass, FalseClass

Check if upload is resumable. That is, neither complete nor expired

Returns:

  • (TrueClass, FalseClass)

    True if upload can be resumed



182
183
184
# File 'lib/google/api_client/media.rb', line 182

def resumable?
  return !(self.complete? or self.expired?)
end

#send_all(api_client) ⇒ Object

Deprecated.

Pass the instance to Google::APIClient#execute instead

Sends all remaining chunks to the server

Parameters:



138
139
140
141
142
143
144
145
# 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

#send_chunk(api_client) ⇒ Object

Deprecated.

Pass the instance to Google::APIClient#execute instead

Sends the next chunk to the server

Parameters:



155
156
157
# File 'lib/google/api_client/media.rb', line 155

def send_chunk(api_client)
  return api_client.execute(self)
end

#to_hashHash

Hashified verison of the API request

Returns:

  • (Hash)


253
254
255
# File 'lib/google/api_client/media.rb', line 253

def to_hash
  super.merge(:offset => @offset)
end

#to_http_requestArray<(Symbol, Addressable::URI, Hash, [#read,#to_str])>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert to an HTTP request. Returns components in order of method, URI, request headers, and body

Returns:

  • (Array<(Symbol, Addressable::URI, Hash, [#read,#to_str])>)


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# 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