Class: Google::APIClient::ResumableUpload
- 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
-
#chunk_size ⇒ Fixnum
Max bytes to send in a single request.
Attributes inherited from Request
#api_method, #authenticated, #authorization, #body, #headers, #http_method, #media, #parameters, #upload_type, #uri
Instance Method Summary collapse
-
#complete? ⇒ TrueClass, FalseClass
Check if upload is complete.
-
#expired? ⇒ TrueClass, FalseClass
Check if the upload URL expired (upload not completed in alotted time.) Expired uploads must be restarted from the beginning.
-
#initialize(options = {}) ⇒ ResumableUpload
constructor
Creates a new uploader.
-
#process_http_response(response) ⇒ Google::APIClient::Result
private
Check the result from the server, updating the offset and/or location if available.
-
#resumable? ⇒ TrueClass, FalseClass
Check if upload is resumable.
-
#send_all(api_client) ⇒ Object
deprecated
Deprecated.
Pass the instance to #execute instead
-
#send_chunk(api_client) ⇒ Object
deprecated
Deprecated.
Pass the instance to #execute instead
-
#to_hash ⇒ Hash
Hashified verison of the API request.
-
#to_http_request ⇒ Array<(Symbol, Addressable::URI, Hash, [#read,#to_str])>
private
Convert to an HTTP request.
Methods inherited from Request
Methods included from Logging
Constructor Details
#initialize(options = {}) ⇒ ResumableUpload
Creates a new uploader.
122 123 124 125 126 127 128 129 |
# File 'lib/google/api_client/media.rb', line 122 def initialize(={}) super self.uri = [:uri] self.http_method = :put @offset = [:offset] || 0 @complete = false @expired = false end |
Instance Attribute Details
#chunk_size ⇒ Fixnum
Returns 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
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
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.
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
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
Pass the instance to Google::APIClient#execute instead
Sends all remaining chunks to the server
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
Pass the instance to Google::APIClient#execute instead
Sends the next chunk to the server
155 156 157 |
# File 'lib/google/api_client/media.rb', line 155 def send_chunk(api_client) return api_client.execute(self) end |
#to_hash ⇒ Hash
Hashified verison of the API request
253 254 255 |
# File 'lib/google/api_client/media.rb', line 253 def to_hash super.merge(:offset => @offset) end |
#to_http_request ⇒ Array<(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
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 |