Module: Paperclip::Storage::Gcs
- Defined in:
- lib/paperclip/storage/gcs.rb,
lib/paperclip/storage/gcs/bucket_repository.rb,
lib/paperclip/storage/gcs/client_repository.rb,
lib/paperclip/storage/gcs/credentials_resolver.rb
Defined Under Namespace
Modules: CredentialsResolver
Classes: BucketRepository, ClientRepository
Constant Summary
collapse
- DEFAULT_GCS_HOST_NAME =
"storage.googleapis.com"
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.extended(base) ⇒ Object
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
|
# File 'lib/paperclip/storage/gcs.rb', line 13
def self.extended(base)
base.instance_eval do
@gcs_options = @options[:gcs_options] || {}
@gcs_credentials = @options[:gcs_credentials]
@gcs_bucket_name = @options[:gcs_bucket]
@gcs_protocol = @options[:gcs_protocol]
@gcs_host_alias = @options[:gcs_host_alias]
@gcs_host_name = @options[:gcs_host_name]
@gcs_encryption_key = @options[:gcs_encryption_key]
@gcs_metadata = normalize_style(@options[:gcs_metadata])
@gcs_permissions = normalize_style(@options[:gcs_permissions])
@gcs_storage_class = normalize_style(@options[:gcs_storage_class])
@gcs_cache_control = normalize_style(@options[:gcs_cache_control])
@gcs_content_type = normalize_style(@options[:gcs_content_type])
@gcs_content_disposition = normalize_style(@options[:gcs_content_disposition])
unless @options[:url].to_s.match(%r{\A:gcs_(alias|path|domain)_url\z}) || @options[:url] == ":asset_host".freeze
@options[:path] = path_option.gsub(/:url/, @options[:url]).sub(%r{\A:rails_root/public/system}, "".freeze)
@options[:url] = ":gcs_path_url".freeze
end
end
Paperclip.interpolates(:gcs_alias_url) do |attachment, style|
"#{attachment.gcs_protocol}//#{attachment.gcs_host_alias}/#{attachment.path(style)}"
end unless Paperclip::Interpolations.respond_to?(:gcs_alias_url)
Paperclip.interpolates(:gcs_path_url) do |attachment, style|
"#{attachment.gcs_protocol}//#{attachment.gcs_host_name}/#{attachment.gcs_bucket_name}/#{attachment.path(style)}"
end unless Paperclip::Interpolations.respond_to?(:gcs_path_url)
Paperclip.interpolates(:gcs_domain_url) do |attachment, style|
"#{attachment.gcs_protocol}//#{attachment.gcs_bucket_name}.#{attachment.gcs_host_name}/#{attachment.path(style)}"
end unless Paperclip::Interpolations.respond_to?(:gcs_domain_url)
Paperclip.interpolates(:asset_host) do |attachment, style|
"#{attachment.path(style)}"
end unless Paperclip::Interpolations.respond_to?(:asset_host)
end
|
Instance Method Details
#copy_to_local_file(style, local_dest_path) ⇒ Object
102
103
104
105
106
107
108
109
|
# File 'lib/paperclip/storage/gcs.rb', line 102
def copy_to_local_file(style, local_dest_path)
log("copying #{path(style)} to local file #{local_dest_path}")
gcs_bucket.file(path(style)).download(local_dest_path, encryption_key: gcs_encryption_key)
rescue Google::Cloud::Error => e
warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}")
false
end
|
#exists?(style = default_style) ⇒ Boolean
60
61
62
63
64
65
66
67
68
|
# File 'lib/paperclip/storage/gcs.rb', line 60
def exists?(style = default_style)
if original_filename
gcs_bucket.find_file(path(style))
else
false
end
rescue Google::Cloud::Error
false
end
|
#expiring_url(time = 3600, style = default_style) ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/paperclip/storage/gcs.rb', line 52
def expiring_url(time = 3600, style = default_style)
if file_path = path(style)
gcs_bucket.signed_url(file_path, expires: time)
else
url(style)
end
end
|
#flush_deletes ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/paperclip/storage/gcs.rb', line 89
def flush_deletes
@queued_for_delete.each do |path|
begin
log("deleting #{path}")
gcs_bucket.file(path).delete
rescue Google::Cloud::Error
end
end
@queued_for_delete = []
end
|
#flush_writes ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/paperclip/storage/gcs.rb', line 70
def flush_writes
@queued_for_write.each do |style, file|
log("saving #{path(style)}")
opts = {
content_type: gcs_content_type(file, style),
content_disposition: gcs_content_disposition(style),
cache_control: gcs_cache_control(style),
encryption_key: gcs_encryption_key,
acl: gcs_permissions(style),
storage_class: gcs_storage_class(style),
metadata: gcs_metadata(style),
}
gcs_bucket.upload_file(file.path, path(style), **opts)
end
after_flush_writes
@queued_for_write = {}
end
|
#gcs_bucket_name ⇒ Object
123
124
125
126
|
# File 'lib/paperclip/storage/gcs.rb', line 123
def gcs_bucket_name
(unwrap_proc(@gcs_bucket_name, self) || gcs_credentials[:bucket]) or
raise ArgumentError, "missing required :gcs_bucket option"
end
|
#gcs_host_alias ⇒ Object
115
116
117
|
# File 'lib/paperclip/storage/gcs.rb', line 115
def gcs_host_alias
unwrap_proc(@gcs_host_alias, self)
end
|
#gcs_host_name ⇒ Object
119
120
121
|
# File 'lib/paperclip/storage/gcs.rb', line 119
def gcs_host_name
unwrap_proc(@gcs_host_name, self) || DEFAULT_GCS_HOST_NAME
end
|
#gcs_protocol ⇒ Object
111
112
113
|
# File 'lib/paperclip/storage/gcs.rb', line 111
def gcs_protocol
unwrap_proc(@gcs_protocol, self)
end
|