1 # Copyright 2010 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 require 'google/api_client/reference'
19 # Uploadable media support. Holds an IO stream & content type.
21 # @see Faraday::UploadIO
23 # media = Google::APIClient::UploadIO.new('mymovie.m4v', 'video/mp4')
24 class UploadIO < Faraday::UploadIO
26 # Get the length of the stream
29 # Length of stream, in bytes
31 io.respond_to?(:length) ? io.length : File.size(local_path)
38 class ResumableUpload < Request
39 # @return [Fixnum] Max bytes to send in a single request
40 attr_accessor :chunk_size
43 # Creates a new uploader.
45 # @param [Hash] options
47 def initialize(options={})
49 self.uri = options[:uri]
50 self.http_method = :put
51 @offset = options[:offset] || 0
57 # Sends all remaining chunks to the server
59 # @deprecated Pass the instance to {Google::APIClient#execute} instead
61 # @param [Google::APIClient] api_client
62 # API Client instance to use for sending
63 def send_all(api_client)
66 result = send_chunk(api_client)
67 break unless result.status == 308
74 # Sends the next chunk to the server
76 # @deprecated Pass the instance to {Google::APIClient#execute} instead
78 # @param [Google::APIClient] api_client
79 # API Client instance to use for sending
80 def send_chunk(api_client)
81 return api_client.execute(self)
85 # Check if upload is complete
87 # @return [TrueClass, FalseClass]
88 # Whether or not the upload complete successfully
94 # Check if the upload URL expired (upload not completed in alotted time.)
95 # Expired uploads must be restarted from the beginning
97 # @return [TrueClass, FalseClass]
98 # Whether or not the upload has expired and can not be resumed
104 # Check if upload is resumable. That is, neither complete nor expired
106 # @return [TrueClass, FalseClass] True if upload can be resumed
108 return !(self.complete? or self.expired?)
112 # Convert to an HTTP request. Returns components in order of method, URI,
113 # request headers, and body
117 # @return [Array<(Symbol, Addressable::URI, Hash, [#read,#to_str])>]
120 raise Google::APIClient::ClientError, "Upload already complete"
122 self.headers.update({
123 'Content-Length' => "0",
124 'Content-Range' => "bytes */#{media.length}" })
126 start_offset = @offset
127 self.media.io.pos = start_offset
128 chunk = self.media.io.read(chunk_size)
129 content_length = chunk.bytesize
130 end_offset = start_offset + content_length - 1
132 self.headers.update({
133 'Content-Length' => "#{content_length}",
134 'Content-Type' => self.media.content_type,
135 'Content-Range' => "bytes #{start_offset}-#{end_offset}/#{media.length}" })
142 # Check the result from the server, updating the offset and/or location
147 # @param [Faraday::Response] response
150 # @return [Google::APIClient::Result]
151 # Processed API response
152 def process_http_response(response)
157 range = response.headers['range']
159 @offset = range.scan(/\d+/).collect{|x| Integer(x)}.last + 1
161 if response.headers['location']
162 self.uri = response.headers['location']
167 # Invalidate the offset to mark it needs to be queried on the
171 return Google::APIClient::Result.new(self, response)
175 # Hashified verison of the API request
179 super.merge(:offset => @offset)