Module: Card::Set::Abstract::Attachment::StorageType
- Extended by:
- Card::Set
- Defined in:
- tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb
Overview
Set: Abstract (Attachment, StorageType)
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from I18nScope
#mod_name, #scope
Methods included from Loader
#clean_empty_module_from_hash, #clean_empty_modules, #extended, #process_base_modules, #register_set
Methods included from Helpers
#abstract_set?, #all_set?, #num_set_parts, #shortname, #underscore
#attachment, #ensure_set, #stage_method
Methods included from Format
#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name
#include_set, #include_set_formats
Methods included from Basket
#abstract_basket, #add_to_basket, #basket, #unshift_basket
Methods included from Trait
#card_accessor, #card_reader, #card_writer, #require_field
Methods included from Event::Api
#event
Instance Attribute Details
#bucket ⇒ Object
160
161
162
163
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 160
def bucket
@bucket ||= cloud? &&
(new_card_bucket || bucket_from_content || bucket_from_config)
end
|
#storage_type ⇒ Object
243
244
245
246
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 243
def storage_type
@storage_type ||=
new_card? ? storage_type_from_config : storage_type_from_content
end
|
Class Method Details
.source_location ⇒ Object
7
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 7
def self.source_location; "/Users/ethan/dev/decko/gem/card/mod/carrierwave/set/abstract/attachment/storage_type.rb"; end
|
Instance Method Details
#bucket_config ⇒ Object
175
176
177
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 175
def bucket_config
@bucket_config ||= load_bucket_config
end
|
#bucket_from_config ⇒ Object
238
239
240
241
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 238
def bucket_from_config
Cardio.config.file_default_bucket ||
(Cardio.config.file_buckets&.keys&.first)
end
|
#bucket_from_content ⇒ Object
233
234
235
236
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 233
def bucket_from_content
return unless content
content.match(/^\((?<bucket>[^)]+)\)/) { |m| m[:bucket] }
end
|
#change_bucket_if_read_only? ⇒ Boolean
101
102
103
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 101
def change_bucket_if_read_only?
cloud? && bucket_config[:read_only] && attachment_is_changing?
end
|
#cloud? ⇒ Boolean
109
110
111
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 109
def cloud?
storage_type == :cloud
end
|
#coded? ⇒ Boolean
121
122
123
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 121
def coded?
storage_type == :coded
end
|
#create_public_links ⇒ Object
76
77
78
79
80
81
82
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 76
def create_public_links
path = attachment.public_path
return if File.exist? path
FileUtils.mkdir_p File.dirname(path)
File.symlink attachment.path, path unless File.symlink? path
create_versions_public_links
end
|
#create_versions_public_links ⇒ Object
84
85
86
87
88
89
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 84
def create_versions_public_links
attachment.versions.each do |_name, version|
next if File.symlink? version.public_path
File.symlink version.path, version.public_path
end
end
|
#deprecated_mod_file? ⇒ Boolean
129
130
131
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 129
def deprecated_mod_file?
content && (lines = content.split("\n")) && lines.size == 4
end
|
#ensure_bucket_config ⇒ Object
193
194
195
196
197
198
199
200
201
202
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 193
def ensure_bucket_config
config = yield
unless config.present?
raise Card::Error, "couldn't find configuration for bucket #{bucket}"
end
unless config[:credentials]
raise Card::Error, "couldn't find credentials for bucket #{bucket}"
end
config
end
|
#known_storage_type?(type = storage_type) ⇒ Boolean
#load_bucket_config ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 179
def load_bucket_config
return {} unless bucket
bucket_config = Cardio.config.file_buckets &&
Cardio.config.file_buckets[bucket.to_sym]
bucket_config &&= bucket_config.symbolize_keys
bucket_config ||= {}
bucket_config[:credentials] &&= bucket_config[:credentials].symbolize_keys
ensure_bucket_config do
load_bucket_config_from_env bucket_config
end
end
|
#load_bucket_config_from_env(config) ⇒ Object
204
205
206
207
208
209
210
211
212
213
214
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 204
def load_bucket_config_from_env config
config ||= {}
CarrierWave::FileCardUploader::CONFIG_OPTIONS.each do |key|
next if key.in? %i[attributes credentials]
replace_with_env_variable config, key
end
config[:credentials] ||= {}
load_bucket_credentials_from_env config[:credentials]
config.delete :credentials unless config[:credentials].present?
config
end
|
#load_bucket_credentials_from_env(cred_config) ⇒ Object
216
217
218
219
220
221
222
223
224
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 216
def load_bucket_credentials_from_env cred_config
cred_opt_pattern =
Regexp.new(/^(?:#{bucket.to_s.upcase}_)?CREDENTIALS_(?<option>.+)$/)
ENV.keys.each do |env_key|
next unless (m = cred_opt_pattern.match(env_key))
replace_with_env_variable cred_config, m[:option].downcase.to_sym,
"credentials"
end
end
|
#local? ⇒ Boolean
117
118
119
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 117
def local?
storage_type == :local
end
|
#mod ⇒ Object
133
134
135
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 133
def mod
@mod ||= coded? && mod_from_content
end
|
#mod=(value) ⇒ Object
305
306
307
308
309
310
311
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 305
def mod= value
if @action == :update && mod != value
@new_mod = value.to_s
else
@mod = value.to_s
end
end
|
#mod_from_content ⇒ Object
137
138
139
140
141
142
143
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 137
def mod_from_content
if content =~ %r{^:[^/]+/([^.]+)}
Regexp.last_match(1) else
mod_from_deprecated_content
end
end
|
#mod_from_deprecated_content ⇒ Object
old format is still used in card_changes
146
147
148
149
150
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 146
def mod_from_deprecated_content
return if content =~ /^\~/
return unless (lines = content.split("\n")) && lines.size == 4
lines.last
end
|
#new_card_bucket ⇒ Object
165
166
167
168
169
170
171
172
173
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 165
def new_card_bucket
return unless new_card?
set_specific[:bucket] || set_specific["bucket"] || bucket_from_config
end
|
#no_upload? ⇒ Boolean
156
157
158
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 156
def no_upload?
storage_type_from_config == :web
end
|
#read_only? ⇒ Boolean
105
106
107
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 105
def read_only?
web? || (cloud? && bucket_config[:read_only])
end
|
#remote_storage? ⇒ Boolean
152
153
154
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 152
def remote_storage?
cloud? || web?
end
|
#remove_public_links ⇒ Object
91
92
93
94
95
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 91
def remove_public_links
symlink_dir = File.dirname attachment.public_path
return unless Dir.exist? symlink_dir
FileUtils.rm_rf symlink_dir
end
|
#replace_with_env_variable(config, option, prefix = nil) ⇒ Object
226
227
228
229
230
231
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 226
def replace_with_env_variable config, option, prefix=nil
env_key = [prefix, option].compact.join("_").upcase
new_value = ENV["#{bucket.to_s.upcase}_#{env_key}"] ||
ENV[env_key]
config[option] = new_value if new_value
end
|
#storage_type_changed? ⇒ Boolean
282
283
284
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 282
def storage_type_changed?
@new_bucket || (@new_storage_type && @new_storage_type != storage_type) || @new_mod
end
|
#storage_type_from_config ⇒ Object
248
249
250
251
252
253
254
255
256
257
258
259
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 248
def storage_type_from_config
type = ENV["FILE_STORAGE"] || Cardio.config.file_storage
return unless type
type = type.to_sym
unless type.in? CarrierWave::FileCardUploader::STORAGE_TYPES
raise Card::Error,
I18n.t(:error_invalid_storage_type,
scope: "mod.carrierwave.set.abstract.attachment",
type: type)
end
type
end
|
#storage_type_from_content ⇒ Object
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 261
def storage_type_from_content
case content
when /^\(/ then :cloud
when %r{/^https?\:/} then :web
when /^~/ then :local
when /^\:/ then :coded
else
if deprecated_mod_file?
:coded
else
storage_type_from_config
end
end
end
|
#temporary_storage_type_change? ⇒ Boolean
330
331
332
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 330
def temporary_storage_type_change?
@temp_storage_type
end
|
#update_storage_attributes ⇒ Object
276
277
278
279
280
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 276
def update_storage_attributes
@mod = @new_mod if @new_mod
@bucket = @new_bucket if @new_bucket
@storage_type = @new_storage_type
end
|
#validate_temporary_storage_type_change(new_storage_type = nil) ⇒ Object
334
335
336
337
338
339
340
341
342
343
344
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 334
def validate_temporary_storage_type_change new_storage_type=nil
new_storage_type ||= @new_storage_type
return unless new_storage_type
unless known_storage_type? new_storage_type
raise Error, tr(:unknown_storage_type, new_storage_type: new_storage_type)
end
if new_storage_type == :coded && codename.blank?
raise Error, "codename needed for storage type :coded"
end
end
|
#web? ⇒ Boolean
113
114
115
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 113
def web?
storage_type == :web
end
|
#will_be_stored_as ⇒ Object
97
98
99
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 97
def will_be_stored_as
@new_storage_type || storage_type
end
|
#will_become_coded? ⇒ Boolean
125
126
127
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 125
def will_become_coded?
will_be_stored_as == :coded
end
|
#with_storage_options(opts = {}) ⇒ Object
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
# File 'tmpsets/set/mod019-carrierwave/abstract/attachment/storage_type.rb', line 313
def with_storage_options opts={}
old_values = {}
validate_temporary_storage_type_change opts[:storage_type]
%i[storage_type mod bucket].each do |opt_name|
next unless opts[opt_name]
old_values[opt_name] = instance_variable_get "@#{opt_name}"
instance_variable_set "@#{opt_name}", opts[opt_name]
@temp_storage_type = true
end
yield
ensure
@temp_storage_type = false
old_values.each do |key, val|
instance_variable_set "@#{key}", val
end
end
|