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::ERROR_REASON_MAPPING, ApiCommand::FIELDS_PARAM, ApiCommand::JSON_CONTENT_TYPE

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:



137
138
139
140
141
142
# File 'lib/google/apis/core/upload.rb', line 137

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 (HTTP::Message::Headers)

    Response headers

  • body (String, #read)

    Response body

Returns:

  • (Object)

    Response object

Raises:



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/google/apis/core/upload.rb', line 157

def process_response(status, header, body)
  @offset = Integer(header[BYTES_RECEIVED_HEADER].first) unless header[BYTES_RECEIVED_HEADER].empty?
  @upload_url = header[UPLOAD_URL_HEADER].first unless header[UPLOAD_URL_HEADER].empty?
  upload_status = header[UPLOAD_STATUS_HEADER].first
  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) ⇒ HTTP::Message

Query for the status of an incomplete upload

Parameters:

  • client (HTTPClient)

    HTTP client

Returns:

  • (HTTP::Message)

Raises:



198
199
200
201
202
203
204
205
206
# File 'lib/google/apis/core/upload.rb', line 198

def send_query_command(client)
  logger.debug { sprintf('Sending upload query command to %s', @upload_url) }

  request_header = header.dup
  apply_request_options(request_header)
  request_header[UPLOAD_COMMAND_HEADER] = QUERY_COMMAND

  client.post(@upload_url, body: '', header: request_header, follow_redirect: true)
end

#send_start_command(client) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/google/apis/core/upload.rb', line 173

def send_start_command(client)
  logger.debug { sprintf('Sending upload start command to %s', url) }

  request_header = header.dup
  apply_request_options(request_header)
  request_header[UPLOAD_PROTOCOL_HEADER] = RESUMABLE
  request_header[UPLOAD_COMMAND_HEADER] = START_COMMAND
  request_header[UPLOAD_CONTENT_LENGTH] = upload_io.size.to_s
  request_header[UPLOAD_CONTENT_TYPE_HEADER] = upload_content_type

  client.request(method.to_s.upcase,
                 url.to_s, query: nil,
                 body: body,
                 header: request_header,
                 follow_redirect: true)
rescue => e
  raise Google::Apis::ServerError, e.message
end

#send_upload_command(client) ⇒ HTTP::Message

Send the actual content

Parameters:

  • client (HTTPClient)

    HTTP client

Returns:

  • (HTTP::Message)

Raises:



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/google/apis/core/upload.rb', line 215

def send_upload_command(client)
  logger.debug { sprintf('Sending upload command to %s', @upload_url) }

  content = upload_io
  content.pos = @offset

  request_header = header.dup
  apply_request_options(request_header)
  request_header[UPLOAD_COMMAND_HEADER] = QUERY_COMMAND
  request_header[UPLOAD_COMMAND_HEADER] = UPLOAD_COMMAND
  request_header[UPLOAD_OFFSET_HEADER] = @offset.to_s
  request_header[CONTENT_TYPE_HEADER] = upload_content_type

  client.post(@upload_url, body: content, header: request_header, follow_redirect: true)
end