From eff1990db0867d61b9a6a065075f79f843841aba Mon Sep 17 00:00:00 2001 From: Peter Weldon Date: Wed, 12 Jan 2011 20:23:10 +0000 Subject: [PATCH 1/5] [aws|storage] Assume body has #size, and maybe #path --- lib/fog/storage/aws.rb | 10 ++++------ lib/fog/storage/models/aws/file.rb | 5 ----- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/lib/fog/storage/aws.rb b/lib/fog/storage/aws.rb index 8ca6c5840..13aae31f0 100644 --- a/lib/fog/storage/aws.rb +++ b/lib/fog/storage/aws.rb @@ -64,16 +64,14 @@ module Fog :headers => {} } - if data.is_a?(String) - metadata[:body] = data - metadata[:headers]['Content-Length'] = metadata[:body].size - else + metadata[:body] = data + metadata[:headers]['Content-Length'] = data.size + + if data.respond_to?(:path) filename = ::File.basename(data.path) unless (mime_types = MIME::Types.of(filename)).empty? metadata[:headers]['Content-Type'] = mime_types.first.content_type end - metadata[:body] = data - metadata[:headers]['Content-Length'] = ::File.size(data.path) end # metadata[:headers]['Content-MD5'] = Base64.encode64(Digest::MD5.digest(metadata[:body])).strip metadata diff --git a/lib/fog/storage/models/aws/file.rb b/lib/fog/storage/models/aws/file.rb index 4fd984993..aadaeae39 100644 --- a/lib/fog/storage/models/aws/file.rb +++ b/lib/fog/storage/models/aws/file.rb @@ -118,11 +118,6 @@ module Fog data = connection.put_object(directory.key, key, body, options) merge_attributes(data.headers) - if body.is_a?(String) - self.content_length = body.size - else - self.content_length = ::File.size(body.path) - end true end From e6a408534222f07d29cf3529a04d2e9570c64a47 Mon Sep 17 00:00:00 2001 From: Peter Weldon Date: Tue, 18 Jan 2011 19:00:00 +0000 Subject: [PATCH 2/5] [aws|storage] Make Files#get consistent with Directory#files - etag: drop quotes - content-length: parse to Numeric - last-modified: parse to Time --- lib/fog/storage/models/aws/files.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/fog/storage/models/aws/files.rb b/lib/fog/storage/models/aws/files.rb index b1ec2d676..c68f7f7a8 100644 --- a/lib/fog/storage/models/aws/files.rb +++ b/lib/fog/storage/models/aws/files.rb @@ -46,6 +46,9 @@ module Fog :body => data.body, :key => key }) + file_data['Last-Modified'] = Time.parse(file_data['Last-Modified']) + file_data['ETag'].gsub!('"','') + file_data['Content-Length'] = file_data['Content-Length'].to_i new(file_data) rescue Excon::Errors::NotFound nil From e0b228c4cee8666ccbb7acf3a0793d101bc06434 Mon Sep 17 00:00:00 2001 From: Peter Weldon Date: Tue, 18 Jan 2011 19:42:37 +0000 Subject: [PATCH 3/5] [aws|storage] Pull out body size calculations --- lib/fog/storage/aws.rb | 12 +++++++++++- lib/fog/storage/models/aws/file.rb | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/fog/storage/aws.rb b/lib/fog/storage/aws.rb index 13aae31f0..fda4342b8 100644 --- a/lib/fog/storage/aws.rb +++ b/lib/fog/storage/aws.rb @@ -58,6 +58,16 @@ module Fog ) end + def get_body_size(body) + case + when body.respond_to?(:bytesize) then body.bytesize + when body.respond_to?(:size) then body.size + when body.respond_to?(:stat) then body.stat.size + else + 0 + end + end + def parse_data(data) metadata = { :body => nil, @@ -65,7 +75,7 @@ module Fog } metadata[:body] = data - metadata[:headers]['Content-Length'] = data.size + metadata[:headers]['Content-Length'] = get_body_size(data) if data.respond_to?(:path) filename = ::File.basename(data.path) diff --git a/lib/fog/storage/models/aws/file.rb b/lib/fog/storage/models/aws/file.rb index aadaeae39..8a6808ba3 100644 --- a/lib/fog/storage/models/aws/file.rb +++ b/lib/fog/storage/models/aws/file.rb @@ -117,7 +117,9 @@ module Fog options['x-amz-storage-class'] = storage_class if storage_class data = connection.put_object(directory.key, key, body, options) + data.headers.delete('Content-Length') merge_attributes(data.headers) + self.content_length = connection.get_body_size(body) true end From 939d95bcfed0733e82099aa5d7180073ede92168 Mon Sep 17 00:00:00 2001 From: Peter Weldon Date: Tue, 18 Jan 2011 19:56:50 +0000 Subject: [PATCH 4/5] [aws|storage] Remove etag quotes for File#save --- lib/fog/storage/models/aws/file.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/fog/storage/models/aws/file.rb b/lib/fog/storage/models/aws/file.rb index 8a6808ba3..4f72bffd0 100644 --- a/lib/fog/storage/models/aws/file.rb +++ b/lib/fog/storage/models/aws/file.rb @@ -118,6 +118,7 @@ module Fog data = connection.put_object(directory.key, key, body, options) data.headers.delete('Content-Length') + data.headers['ETag'].gsub!('"','') merge_attributes(data.headers) self.content_length = connection.get_body_size(body) true From bfb9d22ad2f1d2da32c3018492ee88882879d23f Mon Sep 17 00:00:00 2001 From: Peter Weldon Date: Wed, 19 Jan 2011 18:10:47 +0000 Subject: [PATCH 5/5] [aws|storage] Normalise headers for Files#get/#head --- lib/fog/storage/models/aws/files.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/fog/storage/models/aws/files.rb b/lib/fog/storage/models/aws/files.rb index c68f7f7a8..e2b125b00 100644 --- a/lib/fog/storage/models/aws/files.rb +++ b/lib/fog/storage/models/aws/files.rb @@ -46,9 +46,7 @@ module Fog :body => data.body, :key => key }) - file_data['Last-Modified'] = Time.parse(file_data['Last-Modified']) - file_data['ETag'].gsub!('"','') - file_data['Content-Length'] = file_data['Content-Length'].to_i + normalise_headers(file_data) new(file_data) rescue Excon::Errors::NotFound nil @@ -65,6 +63,7 @@ module Fog file_data = data.headers.merge({ :key => key }) + normalise_headers(file_data) new(file_data) rescue Excon::Errors::NotFound nil @@ -75,6 +74,12 @@ module Fog super({ :directory => directory }.merge!(attributes)) end + def normalise_headers(headers) + headers['Last-Modified'] = Time.parse(headers['Last-Modified']) + headers['ETag'].gsub!('"','') + headers['Content-Length'] = headers['Content-Length'].to_i + end + end end