Skip to content

Commit

Permalink
Merge branch 'Lautus'
Browse files Browse the repository at this point in the history
  • Loading branch information
geemus committed Mar 21, 2011
2 parents bc79b39 + f5f0a2f commit 9751fc4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
29 changes: 20 additions & 9 deletions lib/fog/storage.rb
Expand Up @@ -21,26 +21,37 @@ def self.new(attributes)
end
end

def self.get_body_size(body)
case
when body.respond_to?(:bytesize)
body.bytesize
when body.respond_to?(:size)
body.size
when body.respond_to?(:stat)
body.stat.size
else
0
end
end

def self.parse_data(data)
metadata = {
:body => nil,
:headers => {}
}

if data.is_a?(String)
metadata[:body] = data
metadata[:headers]['Content-Length'] = metadata[:body].size
else
metadata[:body] = data
metadata[:headers]['Content-Length'] = get_body_size(data)

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
end

end
end
end
8 changes: 3 additions & 5 deletions lib/fog/storage/models/aws/file.rb
Expand Up @@ -117,12 +117,10 @@ def save(options = {})
options['x-amz-storage-class'] = storage_class if storage_class

data = connection.put_object(directory.key, key, body, options)
data.headers.delete('Content-Length')
data.headers['ETag'].gsub!('"','')
merge_attributes(data.headers)
if body.is_a?(String)
self.content_length = body.size
else
self.content_length = ::File.size(body.path)
end
self.content_length = Fog::Storage.get_body_size(body)
true
end

Expand Down
7 changes: 7 additions & 0 deletions lib/fog/storage/models/aws/files.rb
Expand Up @@ -46,6 +46,7 @@ def get(key, options = {}, &block)
:body => data.body,
:key => key
})
normalise_headers(file_data)
new(file_data)
rescue Excon::Errors::NotFound
nil
Expand All @@ -62,6 +63,7 @@ def head(key, options = {})
file_data = data.headers.merge({
:key => key
})
normalise_headers(file_data)
new(file_data)
rescue Excon::Errors::NotFound
nil
Expand All @@ -72,6 +74,11 @@ def new(attributes = {})
super({ :directory => directory }.merge!(attributes))
end

def normalise_headers(headers)
headers['Last-Modified'] = Time.parse(headers['Last-Modified'])
headers['ETag'].gsub!('"','')
end

end

end
Expand Down

0 comments on commit 9751fc4

Please sign in to comment.