mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[storage] consolidate parse_data
This commit is contained in:
parent
e3c646b03d
commit
e8842bea8a
8 changed files with 27 additions and 69 deletions
|
@ -21,5 +21,26 @@ module Fog
|
|||
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
|
||||
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
|
|
@ -58,27 +58,6 @@ module Fog
|
|||
)
|
||||
end
|
||||
|
||||
def parse_data(data)
|
||||
metadata = {
|
||||
:body => nil,
|
||||
:headers => {}
|
||||
}
|
||||
|
||||
if data.is_a?(String)
|
||||
metadata[:body] = data
|
||||
metadata[:headers]['Content-Length'] = metadata[:body].size
|
||||
else
|
||||
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
|
||||
|
||||
def url(params, expires)
|
||||
params[:headers]['Date'] = expires.to_i
|
||||
params[:path] = CGI.escape(params[:path]).gsub('%2F', '/')
|
||||
|
|
|
@ -31,27 +31,6 @@ module Fog
|
|||
|
||||
module Utils
|
||||
|
||||
def parse_data(data)
|
||||
metadata = {
|
||||
:body => nil,
|
||||
:headers => {}
|
||||
}
|
||||
|
||||
if data.is_a?(String)
|
||||
metadata[:body] = data
|
||||
metadata[:headers]['Content-Length'] = metadata[:body].size
|
||||
else
|
||||
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
|
||||
|
||||
def url(params, expires)
|
||||
params[:headers]['Date'] = expires.to_i
|
||||
params[:path] = CGI.escape(params[:path]).gsub('%2F', '/')
|
||||
|
|
|
@ -34,27 +34,6 @@ module Fog
|
|||
)
|
||||
end
|
||||
|
||||
def parse_data(data)
|
||||
metadata = {
|
||||
:body => nil,
|
||||
:headers => {}
|
||||
}
|
||||
|
||||
if data.is_a?(String)
|
||||
metadata[:body] = data
|
||||
metadata[:headers]['Content-Length'] = metadata[:body].size
|
||||
else
|
||||
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
|
||||
|
||||
class Mock
|
||||
|
|
|
@ -30,7 +30,7 @@ module Fog
|
|||
# http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTObjectPUT.html
|
||||
#
|
||||
def put_object(bucket_name, object_name, data, options = {})
|
||||
data = parse_data(data)
|
||||
data = Fog::Storage.parse_data(data)
|
||||
headers = data[:headers].merge!(options)
|
||||
request({
|
||||
:body => data[:body],
|
||||
|
@ -56,7 +56,7 @@ module Fog
|
|||
@data[:acls][:object][bucket_name][object_name] = self.class.acls(acl)
|
||||
end
|
||||
|
||||
data = parse_data(data)
|
||||
data = Fog::Storage.parse_data(data)
|
||||
unless data[:body].is_a?(String)
|
||||
data[:body] = data[:body].read
|
||||
end
|
||||
|
|
|
@ -23,7 +23,7 @@ module Fog
|
|||
# http://docs.amazonwebservices.com/AmazonS3/latest/API/mpUploadUploadPart.html
|
||||
#
|
||||
def upload_part(bucket_name, object_name, upload_id, part_number, data, options = {})
|
||||
data = parse_data(data)
|
||||
data = Fog::Storage.parse_data(data)
|
||||
headers = options
|
||||
headers['Content-Length'] = data[:headers]['Content-Length']
|
||||
request({
|
||||
|
|
|
@ -24,7 +24,7 @@ module Fog
|
|||
# * headers<~Hash>:
|
||||
# * 'ETag'<~String> - etag of new object
|
||||
def put_object(bucket_name, object_name, data, options = {})
|
||||
data = parse_data(data)
|
||||
data = Fog::Storage.parse_data(data)
|
||||
headers = data[:headers].merge!(options)
|
||||
request({
|
||||
:body => data[:body],
|
||||
|
@ -50,7 +50,7 @@ module Fog
|
|||
@data[:acls][:object][bucket_name][object_name] = self.class.acls(acl)
|
||||
end
|
||||
|
||||
data = parse_data(data)
|
||||
data = Fog::Storage.parse_data(data)
|
||||
unless data[:body].is_a?(String)
|
||||
data[:body] = data[:body].read
|
||||
end
|
||||
|
|
|
@ -9,7 +9,7 @@ module Fog
|
|||
# * container<~String> - Name for container, should be < 256 bytes and must not contain '/'
|
||||
#
|
||||
def put_object(container, object, data, options = {})
|
||||
data = parse_data(data)
|
||||
data = Fog::Storage.parse_data(data)
|
||||
headers = data[:headers].merge!(options)
|
||||
response = request(
|
||||
:body => data[:body],
|
||||
|
|
Loading…
Reference in a new issue