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 attr_accessor :chunk_size
42 # Creates a new uploader.
44 # @param [Hash] options
46 def initialize(options={})
48 self.uri = options[:uri]
49 self.http_method = :put
50 @offset = options[:offset] || 0
56 # Sends all remaining chunks to the server
58 # @deprecated Pass the instance to {Google::APIClient#execute} instead
60 # @param [Google::APIClient] api_client
61 # API Client instance to use for sending
62 def send_all(api_client)
65 result = send_chunk(api_client)
66 break unless result.status == 308
73 # Sends the next chunk to the server
75 # @deprecated Pass the instance to {Google::APIClient#execute} instead
77 # @param [Google::APIClient] api_client
78 # API Client instance to use for sending
79 def send_chunk(api_client)
80 return api_client.execute(self)
84 # Check if upload is complete
86 # @return [TrueClass, FalseClass]
87 # Whether or not the upload complete successfully
93 # Check if the upload URL expired (upload not completed in alotted time.)
94 # Expired uploads must be restarted from the beginning
96 # @return [TrueClass, FalseClass]
97 # Whether or not the upload has expired and can not be resumed
103 # Convert to an HTTP request. Returns components in order of method, URI,
104 # request headers, and body
108 # @return [Array<(Symbol, Addressable::URI, Hash, [#read,#to_str])>]
111 raise Google::APIClient::ClientError, "Upload already complete"
113 self.headers.update({
114 'Content-Length' => "0",
115 'Content-Range' => "bytes */#{media.length}" })
117 start_offset = @offset
118 self.media.io.pos = start_offset
119 chunk = self.media.io.read(chunk_size)
120 content_length = chunk.bytesize
121 end_offset = start_offset + content_length - 1
123 self.headers.update({
124 'Content-Length' => "#{content_length}",
125 'Content-Type' => self.media.content_type,
126 'Content-Range' => "bytes #{start_offset}-#{end_offset}/#{media.length}" })
133 # Check the result from the server, updating the offset and/or location
138 # @param [Faraday::Response] response
141 # @return [Google::APIClient::Result]
142 # Processed API response
143 def process_http_response(response)
148 range = response.headers['range']
150 @offset = range.scan(/\d+/).collect{|x| Integer(x)}.last + 1
152 if response.headers['location']
153 self.uri = response.headers['location']
158 # Invalidate the offset to mark it needs to be queried on the
162 return Google::APIClient::Result.new(self, response)
166 super.merge(:offset => @offset)