Class: Google::Apis::Core::ResumableUploadCommand
- Inherits:
-
BaseUploadCommand
- Object
- HttpCommand
- ApiCommand
- BaseUploadCommand
- Google::Apis::Core::ResumableUploadCommand
- 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
Instance Attribute Summary
Attributes inherited from ApiCommand
#client_version, #request_object, #request_representation, #response_class, #response_representation
Attributes inherited from HttpCommand
#body, #connection, #header, #method, #options, #params, #query, #url
Instance Method Summary collapse
-
#prepare!
Reset upload to initial state.
-
#process_response(status, header, body) ⇒ Object
Check the to see if the upload is complete or needs to be resumed.
-
#send_query_command(client) ⇒ HTTP::Message
Query for the status of an incomplete upload.
- #send_start_command(client) ⇒ Object
-
#send_upload_command(client) ⇒ HTTP::Message
Send the actual content.
Methods inherited from ApiCommand
#allow_form_encoding?, #check_status, #decode_response_body, #initialize
Methods inherited from HttpCommand
#allow_form_encoding?, #apply_request_options, #authorization_refreshable?, #check_status, #decode_response_body, #do_retry, #error, #execute, #initialize, #set_api_version_header, #success
Methods included from Logging
Constructor Details
This class inherits a constructor from Google::Apis::Core::ApiCommand
Instance Method Details
#prepare!
This method returns an undefined value.
Reset upload to initial state.
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/google/apis/core/upload.rb', line 138 def prepare! @state = :start @upload_url = nil @offset = 0 # Prevent the command from populating the body with form encoding, by # asserting that it already has a body. Form encoding is never used # by upload requests. self.body = '' unless self.body super end |
#process_response(status, header, body) ⇒ Object
Check the to see if the upload is complete or needs to be resumed.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/google/apis/core/upload.rb', line 162 def process_response(status, header, body) bytes_received_header = Array(header[BYTES_RECEIVED_HEADER]) @offset = Integer(bytes_received_header.first) unless bytes_received_header.empty? upload_url_header = Array(header[UPLOAD_URL_HEADER]) @upload_url = upload_url_header.first unless upload_url_header.empty? upload_status = Array(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
200 201 202 203 204 205 206 207 208 |
# File 'lib/google/apis/core/upload.rb', line 200 def send_query_command(client) logger.debug { sprintf('Sending upload query command to %s', @upload_url) } request_header = header.dup (request_header) request_header[UPLOAD_COMMAND_HEADER] = QUERY_COMMAND client.post(@upload_url, '', request_header) end |
#send_start_command(client) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/google/apis/core/upload.rb', line 180 def send_start_command(client) logger.debug { sprintf('Sending upload start command to %s', url) } request_header = header.dup (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.run_request(method, url.to_s, body, request_header) rescue => e raise Google::Apis::ServerError, e. end |
#send_upload_command(client) ⇒ HTTP::Message
Send the actual content
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/google/apis/core/upload.rb', line 216 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 (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 request_header[CONTENT_LENGTH_HEADER] = (upload_io.size - @offset).to_s client.post(@upload_url, content, request_header) end |