1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

fixes for streaming files

* create chunks if the response wasn't chunked
* mock out the chunk behaviour
* move chunk size into a constant
This commit is contained in:
Wesley Beary 2009-09-10 10:08:29 -07:00
parent cec971052b
commit a7a45e88f6
3 changed files with 24 additions and 7 deletions

View file

@ -54,7 +54,7 @@ else
module AWS
class S3
def get_object(bucket_name, object_name, options = {})
def get_object(bucket_name, object_name, options = {}, &block)
unless bucket_name
raise ArgumentError.new('bucket_name is required')
end
@ -79,7 +79,17 @@ else
'ETag' => object['ETag'],
'Last-Modified' => object['LastModified']
}
response.body = object[:body]
unless block_given?
response.body = object[:body]
else
data = StringIO.new(object[:body])
remaining = data.length
while remaining > 0
chunk = data.read([remaining, Fog::Connection::CHUNK_SIZE].min)
block.call(chunk)
remaining -= Fog::Connection::CHUNK_SIZE
end
end
end
else
response.status = 404

View file

@ -96,7 +96,7 @@ module Fog
metadata[:headers]['Content-Type'] = mime_types.first.content_type
end
metadata[:body] = data.read
metadata[:headers]['Content-Length'] = File.size(data.path)
metadata[:headers]['Content-Length'] = File.size(data.path).to_s
end
# metadata[:headers]['Content-MD5'] = Base64.encode64(Digest::MD5.digest(metadata[:body])).strip
metadata

View file

@ -11,6 +11,10 @@ unless Fog.mocking?
module Fog
class Connection
unless const_defined?(:CHUNK_SIZE)
CHUNK_SIZE = 1048576 # 1 megabyte
end
def initialize(url)
@uri = URI.parse(url)
@connection = TCPSocket.open(@uri.host, @uri.port)
@ -49,7 +53,7 @@ unless Fog.mocking?
else
body = params[:body]
end
while chunk = body.read(1048576) # 1 megabyte
while chunk = body.read(CHUNK_SIZE)
@connection.write(chunk)
end
end
@ -84,11 +88,14 @@ unless Fog.mocking?
unless params[:method] == 'HEAD'
if response.headers['Content-Length']
content = @connection.read(response.headers['Content-Length'].to_i)
unless params[:block]
body << content
body << @connection.read(response.headers['Content-Length'].to_i)
else
params[:block].call(content)
remaining = response.headers['Content-Length'].to_i
while remaining > 0
params[:block].call(@connection.read([CHUNK_SIZE, remaining].min))
remaining -= CHUNK_SIZE;
end
end
elsif response.headers['Transfer-Encoding'] == 'chunked'
while true