2010-10-04 17:02:08 -04:00
|
|
|
require 'fog/core/model'
|
2010-09-18 13:40:48 -04:00
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Google
|
|
|
|
class Storage
|
|
|
|
|
|
|
|
class File < Fog::Model
|
|
|
|
|
|
|
|
identity :key, :aliases => 'Key'
|
|
|
|
|
2010-10-12 21:07:36 -04:00
|
|
|
attr_writer :body
|
2010-09-18 13:40:48 -04:00
|
|
|
attribute :content_length, :aliases => 'Content-Length'
|
|
|
|
attribute :content_type, :aliases => 'Content-Type'
|
|
|
|
attribute :etag, :aliases => ['Etag', 'ETag']
|
|
|
|
attribute :last_modified, :aliases => ['Last-Modified', 'LastModified']
|
|
|
|
attribute :owner, :aliases => 'Owner'
|
|
|
|
attribute :size, :aliases => 'Size'
|
|
|
|
attribute :storage_class, :aliases => 'StorageClass'
|
|
|
|
|
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
|
|
|
|
@body ||= if last_modified && (file = collection.get(identity))
|
|
|
|
file.body
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def directory
|
|
|
|
@directory
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy(target_directory_key, target_file_key)
|
|
|
|
requires :directory, :key
|
2010-11-02 14:42:53 -04:00
|
|
|
connection.copy_object(directory.key, @key, target_directory_key, target_file_key)
|
2010-09-18 13:40:48 -04:00
|
|
|
target_directory = connection.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
|
|
|
|
connection.delete_object(directory.key, @key)
|
|
|
|
rescue Excon::Errors::NotFound
|
|
|
|
end
|
2010-09-18 13:40:48 -04:00
|
|
|
true
|
|
|
|
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
|
|
|
|
@owner = {
|
|
|
|
:display_name => new_owner['DisplayName'],
|
|
|
|
:id => new_owner['ID']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-05 14:37:12 -04:00
|
|
|
def public_url
|
|
|
|
requires :directory, :key
|
|
|
|
if directory.public_url
|
|
|
|
"#{directory.public_url}/#{key}"
|
|
|
|
elsif connection.get_object_acl(directory.key, key).body['AccessControlList'].detect {|entry| entry['Scope']['type'] == 'AllUsers' && entry['Permission'] == 'READ'}
|
|
|
|
if directory.key.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/
|
|
|
|
"https://#{directory.key}.commondatastorage.googleapis/#{key}"
|
|
|
|
else
|
|
|
|
"https://commondatastorage.googleapis.com/#{directory.key}/#{key}"
|
|
|
|
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 != {}
|
|
|
|
Formatador.display_line("[yellow][WARN] options param is deprecated, use acl= instead[/] [light_black](#{caller.first})[/]")
|
|
|
|
end
|
|
|
|
if @acl
|
|
|
|
options['x-amz-acl'] ||= @acl
|
|
|
|
end
|
2010-11-01 14:01:45 -04:00
|
|
|
if content_type
|
|
|
|
options['Content-Type'] = content_type
|
|
|
|
end
|
2010-09-18 13:40:48 -04:00
|
|
|
data = connection.put_object(directory.key, @key, @body, options)
|
|
|
|
@etag = data.headers['ETag']
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def url(expires)
|
|
|
|
requires :key
|
|
|
|
collection.get_url(key, expires)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def directory=(new_directory)
|
|
|
|
@directory = new_directory
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|