1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
module Fog
module Storage
class GoogleJSON
class File < Fog::Model
identity :key, :aliases => ["Key", :name]
attribute :acl
attribute :predefined_acl, :aliases => ["predefinedAcl", :predefined_acl]
attribute :cache_control, :aliases => ["cacheControl", :cache_control]
attribute :content_disposition, :aliases => ["contentDisposition", :content_disposition]
attribute :content_encoding, :aliases => ["contentEncoding", :content_encoding]
attribute :content_length, :aliases => ["size", :size], :type => :integer
attribute :content_md5, :aliases => ["md5Hash", :md5_hash]
attribute :content_type, :aliases => ["contentType", :content_type]
attribute :crc32c
attribute :etag, :aliases => ["etag", :etag]
attribute :time_created, :aliases => ["timeCreated", :time_created]
attribute :last_modified, :aliases => ["updated", :updated]
attribute :generation
attribute :metageneration
attribute :metadata, :aliases => ["metadata", :metadata]
attribute :self_link, :aliases => ["selfLink", :self_link]
attribute :media_link, :aliases => ["mediaLink", :media_link]
attribute :owner
attribute :storage_class, :aliases => "storageClass"
# attribute :body
# https://cloud.google.com/storage/docs/access-control/lists#predefined-acls
VALID_PREDEFINED_ACLS = [
"authenticatedRead",
"bucketOwnerFullControl",
"bucketOwnerRead",
"private",
"projectPrivate",
"publicRead",
"publicReadWrite"
].freeze
def predefined_acl=(new_predefined_acl)
unless VALID_PREDEFINED_ACLS.include?(new_predefined_acl)
raise ArgumentError.new("acl must be one of [#{VALID_PREDEFINED_ACLS.join(', ')}]")
end
@predefined_acl = new_predefined_acl
end
def body
last_modified && (file = collection.get(identity)) ? attributes[:body] ||= file.body : attributes[:body] ||= ""
end
def body=(new_body)
attributes[:body] = new_body
end
attr_reader :directory
def copy(target_directory_key, target_file_key, options = {})
requires :directory, :key
service.copy_object(directory.key, key, target_directory_key, target_file_key, options)
target_directory = service.directories.new(:key => target_directory_key)
target_directory.files.get(target_file_key)
end
def destroy
requires :directory, :key
service.delete_object(directory.key, key)
true
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
false
end
def public=(new_public)
unless new_public.nil?
if new_public
@predefined_acl = "publicRead"
else
@predefined_acl = "projectPrivate"
end
end
new_public
end
def public_url
requires :directory, :key
"https://storage.googleapis.com/#{directory.key}/#{key}"
end
FILE_INSERTABLE_FIELDS = %i(
content_type
predefined_acl
cache_control
content_disposition
content_encoding
metadata
).freeze
def save
requires :body, :directory, :key
options = Hash[
FILE_INSERTABLE_FIELDS.map { |k| [k, attributes[k]] }
.reject { |pair| pair[1].nil? }
]
options[:predefined_acl] ||= @predefined_acl
service.put_object(directory.key, key, body, options)
self.content_length = Fog::Storage.get_body_size(body)
self.content_type ||= Fog::Storage.get_content_type(body)
true
end
# params[:expires] : Eg: Time.now to integer value.
def url(expires, options = {})
requires :key
collection.get_https_url(key, expires, options)
end
private
attr_writer :directory
end
end
end
end
|