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



123
124
125
126
127
128
129
130
# File 'lib/google/api_client/media.rb', line 123

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



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

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



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

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



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

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:



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/google/api_client/media.rb', line 228

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



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

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:



139
140
141
142
143
144
145
146
# File 'lib/google/api_client/media.rb', line 139

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:



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

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

#to_hashHash

Hashified verison of the API request

Returns:

  • (Hash)


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

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])>)


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/google/api_client/media.rb', line 194

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