Class: Fog::Storage::GoogleJSON::File

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/storage/google_json/models/file.rb

Constant Summary collapse

VALID_PREDEFINED_ACLS =
[
  "authenticatedRead",
  "bucketOwnerFullControl",
  "bucketOwnerRead",
  "private",
  "projectPrivate",
  "publicRead",
  "publicReadWrite"
].freeze
FILE_INSERTABLE_FIELDS =
%i(
  content_type
  predefined_acl
  cache_control
  content_disposition
  content_encoding
  metadata
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#directoryObject

Returns the value of attribute directory


55
56
57
# File 'lib/fog/storage/google_json/models/file.rb', line 55

def directory
  @directory
end

Instance Method Details

#bodyObject


47
48
49
# File 'lib/fog/storage/google_json/models/file.rb', line 47

def body
  last_modified && (file = collection.get(identity)) ? attributes[:body] ||= file.body : attributes[:body] ||= ""
end

#body=(new_body) ⇒ Object


51
52
53
# File 'lib/fog/storage/google_json/models/file.rb', line 51

def body=(new_body)
  attributes[:body] = new_body
end

#copy(target_directory_key, target_file_key, options = {}) ⇒ Object


57
58
59
60
61
62
# File 'lib/fog/storage/google_json/models/file.rb', line 57

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

#destroyObject


64
65
66
67
68
69
70
71
# File 'lib/fog/storage/google_json/models/file.rb', line 64

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

#predefined_acl=(new_predefined_acl) ⇒ Object


40
41
42
43
44
45
# File 'lib/fog/storage/google_json/models/file.rb', line 40

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

#public=(new_public) ⇒ Object


73
74
75
76
77
78
79
80
81
82
# File 'lib/fog/storage/google_json/models/file.rb', line 73

def public=(new_public)
  unless new_public.nil?
    if new_public
      @predefined_acl = "publicRead"
    else
      @predefined_acl = "projectPrivate"
    end
  end
  new_public
end

#public_urlObject


84
85
86
87
# File 'lib/fog/storage/google_json/models/file.rb', line 84

def public_url
  requires :directory, :key
  "https://storage.googleapis.com/#{directory.key}/#{key}"
end

#saveObject


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fog/storage/google_json/models/file.rb', line 98

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

#url(expires, options = {}) ⇒ Object

params : Eg: Time.now to integer value.


115
116
117
118
# File 'lib/fog/storage/google_json/models/file.rb', line 115

def url(expires, options = {})
  requires :key
  collection.get_https_url(key, expires, options)
end