Class: Google::Apis::Core::ResumableUploadCommand

Inherits:
BaseUploadCommand show all
Defined in:
lib/google/apis/core/upload.rb

Overview

Implementation of the resumable upload protocol

Constant Summary collapse

UPLOAD_COMMAND_HEADER =
'X-Goog-Upload-Command'
UPLOAD_OFFSET_HEADER =
'X-Goog-Upload-Offset'
BYTES_RECEIVED_HEADER =
'X-Goog-Upload-Size-Received'
UPLOAD_URL_HEADER =
'X-Goog-Upload-URL'
UPLOAD_STATUS_HEADER =
'X-Goog-Upload-Status'
STATUS_ACTIVE =
'active'
STATUS_FINAL =
'final'
STATUS_CANCELLED =
'cancelled'
RESUMABLE =
'resumable'
START_COMMAND =
'start'
QUERY_COMMAND =
'query'
UPLOAD_COMMAND =
'upload, finalize'

Constants inherited from ApiCommand

ApiCommand::FIELDS_PARAM, ApiCommand::JSON_CONTENT_TYPE, ApiCommand::RATE_LIMIT_ERRORS

Constants inherited from HttpCommand

HttpCommand::RETRIABLE_ERRORS

Instance Attribute Summary

Attributes inherited from ApiCommand

#request_object, #request_representation, #response_class, #response_representation

Attributes inherited from HttpCommand

#body, #connection, #header, #method, #options, #params, #query, #url

Instance Method Summary collapse

Methods inherited from ApiCommand

#allow_form_encoding?, #check_status, #decode_response_body

Methods inherited from HttpCommand

#allow_form_encoding?, #apply_request_options, #authorization_refreshable?, #check_status, #decode_response_body, #error, #execute, #initialize, #success

Methods included from Logging

#logger

Constructor Details

This class inherits a constructor from Google::Apis::Core::HttpCommand

Instance Method Details

#prepare!

This method returns an undefined value.

Reset upload to initial state.

Raises:


171
172
173
174
175
176
# File 'lib/google/apis/core/upload.rb', line 171

def prepare!
  @state = :start
  @upload_url = nil
  @offset = 0
  super
end

#process_response(status, header, body) ⇒ Object

Check the to see if the upload is complete or needs to be resumed.

Parameters:

  • status (Fixnum)

    HTTP status code of response

  • header (Hurley::Header)

    Response headers

  • body (String, #read)

    Response body

Returns:

  • (Object)

    Response object

Raises:


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/google/apis/core/upload.rb', line 191

def process_response(status, header, body)
  @offset = Integer(header[BYTES_RECEIVED_HEADER]) if header.key?(BYTES_RECEIVED_HEADER)
  @upload_url = header[UPLOAD_URL_HEADER] if header.key?(UPLOAD_URL_HEADER)
  upload_status = header[UPLOAD_STATUS_HEADER]
  logger.debug { sprintf('Upload status %s', upload_status) }
  if upload_status == STATUS_ACTIVE
    @state = :active
  elsif upload_status == STATUS_FINAL
    @state = :final
  elsif upload_status == STATUS_CANCELLED
    @state = :cancelled
    fail Google::Apis::ClientError, body
  end
  super(status, header, body)
end

#send_query_command(client) ⇒ Hurley::Response

Query for the status of an incomplete upload

Parameters:

  • client (Hurley::Client)

    HTTP client

Returns:

  • (Hurley::Response)

Raises:


232
233
234
235
236
237
238
# File 'lib/google/apis/core/upload.rb', line 232

def send_query_command(client)
  logger.debug { sprintf('Sending upload query command to %s', @upload_url) }
  client.post(@upload_url, nil) do |req|
    apply_request_options(req)
    req.header[UPLOAD_COMMAND_HEADER] = QUERY_COMMAND
  end
end

#send_start_command(client) ⇒ Hurley::Response

Send the start command to initiate the upload

Parameters:

  • client (Hurley::Client)

    HTTP client

Returns:

  • (Hurley::Response)

Raises:


213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/google/apis/core/upload.rb', line 213

def send_start_command(client)
  logger.debug { sprintf('Sending upload start command to %s', url) }
  client.send(method, url, body) do |req|
    apply_request_options(req)
    req.header[UPLOAD_PROTOCOL_HEADER] = RESUMABLE
    req.header[UPLOAD_COMMAND_HEADER] = START_COMMAND
    req.header[UPLOAD_CONTENT_LENGTH] = upload_io.length.to_s
    req.header[UPLOAD_CONTENT_TYPE_HEADER] = upload_io.content_type
  end
rescue => e
  raise Google::Apis::ServerError, e.message
end

#send_upload_command(client) ⇒ Hurley::Response

Send the actual content

Parameters:

  • client (Hurley::Client)

    HTTP client

Returns:

  • (Hurley::Response)

Raises:


246
247
248
249
250
251
252
253
254
255
# File 'lib/google/apis/core/upload.rb', line 246

def send_upload_command(client)
  logger.debug { sprintf('Sending upload command to %s', @upload_url) }
  content = upload_io
  content.pos = @offset
  client.post(@upload_url, content) do |req|
    apply_request_options(req)
    req.header[UPLOAD_COMMAND_HEADER] = UPLOAD_COMMAND
    req.header[UPLOAD_OFFSET_HEADER] = @offset.to_s
  end
end