2010-10-04 17:02:08 -04:00
|
|
|
require 'fog/core/model'
|
2010-09-18 13:40:48 -04:00
|
|
|
|
|
|
|
module Fog
|
2011-06-15 17:26:43 -04:00
|
|
|
module Storage
|
|
|
|
class Google
|
2010-09-18 13:40:48 -04:00
|
|
|
|
|
|
|
class File < Fog::Model
|
|
|
|
|
|
|
|
identity :key, :aliases => 'Key'
|
|
|
|
|
2010-11-08 20:14:21 -05:00
|
|
|
attribute :cache_control, :aliases => 'Cache-Control'
|
|
|
|
attribute :content_disposition, :aliases => 'Content-Disposition'
|
|
|
|
attribute :content_encoding, :aliases => 'Content-Encoding'
|
2011-01-26 18:56:50 -05:00
|
|
|
attribute :content_length, :aliases => ['Content-Length', 'Size'], :type => :integer
|
2010-11-08 20:14:21 -05:00
|
|
|
attribute :content_md5, :aliases => 'Content-MD5'
|
|
|
|
attribute :content_type, :aliases => 'Content-Type'
|
|
|
|
attribute :etag, :aliases => ['Etag', 'ETag']
|
|
|
|
attribute :expires, :aliases => 'Expires'
|
2010-12-30 15:54:22 -05:00
|
|
|
attribute :last_modified, :aliases => ['Last-Modified', 'LastModified']
|
2011-01-05 14:02:08 -05:00
|
|
|
attribute :metadata
|
2010-11-08 20:14:21 -05:00
|
|
|
attribute :owner, :aliases => 'Owner'
|
|
|
|
attribute :storage_class, :aliases => ['x-goog-storage-class', 'StorageClass']
|
2010-09-18 13:40:48 -04:00
|
|
|
|
2010-10-29 19:38:17 -04:00
|
|
|
def acl=(new_acl)
|
|
|
|
valid_acls = ['private', 'public-read', 'public-read-write', 'authenticated-read']
|
|
|
|
unless valid_acls.include?(new_acl)
|
|
|
|
raise ArgumentError.new("acl must be one of [#{valid_acls.join(', ')}]")
|
|
|
|
end
|
|
|
|
@acl = new_acl
|
|
|
|
end
|
|
|
|
|
2010-09-18 13:40:48 -04:00
|
|
|
def body
|
2010-11-19 16:45:45 -05:00
|
|
|
attributes[:body] ||= if last_modified && (file = collection.get(identity))
|
2010-09-18 13:40:48 -04:00
|
|
|
file.body
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-19 16:45:45 -05:00
|
|
|
def body=(new_body)
|
|
|
|
attributes[:body] = new_body
|
|
|
|
end
|
|
|
|
|
2010-09-18 13:40:48 -04:00
|
|
|
def directory
|
|
|
|
@directory
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy(target_directory_key, target_file_key)
|
|
|
|
requires :directory, :key
|
2012-12-22 18:26:39 -05:00
|
|
|
service.copy_object(directory.key, key, target_directory_key, target_file_key)
|
|
|
|
target_directory = service.directories.new(:key => target_directory_key)
|
2010-11-02 14:42:53 -04:00
|
|
|
target_directory.files.get(target_file_key)
|
2010-09-18 13:40:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
requires :directory, :key
|
2010-09-20 18:33:34 -04:00
|
|
|
begin
|
2012-12-22 18:26:39 -05:00
|
|
|
service.delete_object(directory.key, key)
|
2010-09-20 18:33:34 -04:00
|
|
|
rescue Excon::Errors::NotFound
|
|
|
|
end
|
2010-09-18 13:40:48 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2011-01-05 14:02:08 -05:00
|
|
|
remove_method :metadata
|
|
|
|
def metadata
|
|
|
|
attributes.reject {|key, value| !(key.to_s =~ /^x-goog-meta-/)}
|
|
|
|
end
|
|
|
|
|
|
|
|
remove_method :metadata=
|
|
|
|
def metadata=(new_metadata)
|
|
|
|
merge_attributes(new_metadata)
|
|
|
|
end
|
|
|
|
|
2010-10-12 19:22:12 -04:00
|
|
|
remove_method :owner=
|
2010-09-18 13:40:48 -04:00
|
|
|
def owner=(new_owner)
|
|
|
|
if new_owner
|
2010-11-19 16:45:45 -05:00
|
|
|
attributes[:owner] = {
|
2010-09-18 13:40:48 -04:00
|
|
|
:display_name => new_owner['DisplayName'],
|
|
|
|
:id => new_owner['ID']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-18 14:18:46 -05:00
|
|
|
def public=(new_public)
|
|
|
|
if new_public
|
|
|
|
@acl = 'public-read'
|
|
|
|
else
|
|
|
|
@acl = 'private'
|
|
|
|
end
|
|
|
|
new_public
|
|
|
|
end
|
|
|
|
|
2010-11-05 14:37:12 -04:00
|
|
|
def public_url
|
|
|
|
requires :directory, :key
|
2012-12-22 18:26:39 -05:00
|
|
|
if service.get_object_acl(directory.key, key).body['AccessControlList'].detect {|entry| entry['Scope']['type'] == 'AllUsers' && entry['Permission'] == 'READ'}
|
2010-11-05 14:37:12 -04:00
|
|
|
if directory.key.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/
|
2012-09-11 21:02:43 -04:00
|
|
|
"https://#{directory.key}.storage.googleapis.com/#{key}"
|
2010-11-05 14:37:12 -04:00
|
|
|
else
|
2012-09-11 21:02:43 -04:00
|
|
|
"https://storage.googleapis.com/#{directory.key}/#{key}"
|
2010-11-05 14:37:12 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-18 13:40:48 -04:00
|
|
|
def save(options = {})
|
|
|
|
requires :body, :directory, :key
|
2010-10-29 19:38:17 -04:00
|
|
|
if options != {}
|
2011-10-19 15:49:34 -04:00
|
|
|
Fog::Logger.deprecation("options param is deprecated, use acl= instead [light_black](#{caller.first})[/]")
|
2010-10-29 19:38:17 -04:00
|
|
|
end
|
2010-11-08 20:14:21 -05:00
|
|
|
options['x-goog-acl'] ||= @acl if @acl
|
|
|
|
options['Cache-Control'] = cache_control if cache_control
|
|
|
|
options['Content-Disposition'] = content_disposition if content_disposition
|
|
|
|
options['Content-Encoding'] = content_encoding if content_encoding
|
|
|
|
options['Content-MD5'] = content_md5 if content_md5
|
|
|
|
options['Content-Type'] = content_type if content_type
|
|
|
|
options['Expires'] = expires if expires
|
2011-01-05 14:02:08 -05:00
|
|
|
options.merge(metadata)
|
2010-11-08 20:14:21 -05:00
|
|
|
|
2012-12-22 18:26:39 -05:00
|
|
|
data = service.put_object(directory.key, key, body, options)
|
2012-03-26 20:14:02 -04:00
|
|
|
merge_attributes(data.headers.reject {|key, value| ['Content-Length', 'Content-Type'].include?(key)})
|
2011-05-25 17:37:47 -04:00
|
|
|
self.content_length = Fog::Storage.get_body_size(body)
|
2012-03-26 20:14:02 -04:00
|
|
|
self.content_type ||= Fog::Storage.get_content_type(body)
|
2010-09-18 13:40:48 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def url(expires)
|
|
|
|
requires :key
|
2011-08-12 12:00:47 -04:00
|
|
|
collection.get_http_url(key, expires)
|
2010-09-18 13:40:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def directory=(new_directory)
|
|
|
|
@directory = new_directory
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|