require 'fog/core/model' module Fog module Google class Storage class File < Fog::Model identity :key, :aliases => 'Key' attr_writer :body 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' 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 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 connection.copy_object(directory.key, @key, target_directory_key, target_file_key) target_directory = connection.directories.new(:key => target_directory_key) target_directory.files.get(target_file_key) end def destroy requires :directory, :key begin connection.delete_object(directory.key, @key) rescue Excon::Errors::NotFound end true end remove_method :owner= def owner=(new_owner) if new_owner @owner = { :display_name => new_owner['DisplayName'], :id => new_owner['ID'] } end end 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 def save(options = {}) requires :body, :directory, :key 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 if content_type options['Content-Type'] = content_type end 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