Module: Card::Set::Abstract::Attachment::StorageType
- Extended by:
- Card::Set
- Defined in:
- tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb
Constant Summary
Constants included
from Format
Format::TEMPLATE_DIR
Instance Attribute 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
#all_set_format_mod!, #before, #define_on_format, #format, layout_method_name, #register_set_format, #view, #view_for_override, view_method_name, wrapper_method_name
#include_set, #include_set_formats
Methods included from Basket
#abstract_basket, #add_to_basket, #basket
Methods included from Trait
#card_accessor, #card_reader, #card_writer, #require_field
Methods included from Event::Api
#event
Instance Attribute Details
#bucket ⇒ Object
147
148
149
150
151
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 147
def bucket
@bucket ||= cloud? &&
((new_card? && bucket_from_config) || bucket_from_content ||
bucket_from_config)
end
|
#storage_type ⇒ Object
218
219
220
221
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 218
def storage_type
@storage_type ||=
new_card? ? storage_type_from_config : storage_type_from_content
end
|
Instance Method Details
#bucket_config ⇒ Object
153
154
155
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 153
def bucket_config
@bucket_config ||= load_bucket_config
end
|
#bucket_from_config ⇒ Object
213
214
215
216
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 213
def bucket_from_config
Cardio.config.file_default_bucket ||
(Cardio.config.file_buckets && Cardio.config.file_buckets.keys.first)
end
|
#bucket_from_content ⇒ Object
208
209
210
211
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 208
def bucket_from_content
return unless content
content.match(/^\((?<bucket>[^)]+)\)/) { |m| m[:bucket] }
end
|
#cloud? ⇒ Boolean
96
97
98
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 96
def cloud?
storage_type == :cloud
end
|
#coded? ⇒ Boolean
108
109
110
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 108
def coded?
storage_type == :coded
end
|
#create_public_links ⇒ Object
71
72
73
74
75
76
77
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 71
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
79
80
81
82
83
84
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 79
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
116
117
118
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 116
def deprecated_mod_file?
content && (lines = content.split("\n")) && lines.size == 4
end
|
#ensure_bucket_config ⇒ Object
171
172
173
174
175
176
177
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 171
def ensure_bucket_config
config = yield
unless config.present?
raise Card::Error, "couldn't find configuration for bucket #{bucket}"
end
config
end
|
#known_storage_type?(type = storage_type) ⇒ Boolean
#load_bucket_config ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 157
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
179
180
181
182
183
184
185
186
187
188
189
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 179
def load_bucket_config_from_env config
config ||= {}
CarrierWave::FileCardUploader::CONFIG_OPTIONS.each do |key|
next if key.in? [: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
191
192
193
194
195
196
197
198
199
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 191
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
104
105
106
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 104
def local?
storage_type == :local
end
|
#mod ⇒ Object
120
121
122
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 120
def mod
@mod ||= coded? && mod_from_content
end
|
#mod=(value) ⇒ Object
280
281
282
283
284
285
286
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 280
def mod= value
if @action == :update
@new_mod = value.to_s
else
@mod = value.to_s
end
end
|
#mod_from_content ⇒ Object
124
125
126
127
128
129
130
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 124
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
133
134
135
136
137
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 133
def mod_from_deprecated_content
return if content =~ /^\~/
return unless (lines = content.split("\n")) && lines.size == 4
lines.last
end
|
#no_upload? ⇒ Boolean
143
144
145
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 143
def no_upload?
storage_type_from_config == :web
end
|
#remote_storage? ⇒ Boolean
139
140
141
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 139
def remote_storage?
cloud? || web?
end
|
#remove_public_links ⇒ Object
86
87
88
89
90
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 86
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
201
202
203
204
205
206
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 201
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
257
258
259
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 257
def storage_type_changed?
@new_bucket || @new_storage_type || @new_mod
end
|
#storage_type_from_config ⇒ Object
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 223
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 236
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
305
306
307
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 305
def temporary_storage_type_change?
@temp_storage_type
end
|
#update_storage_attributes ⇒ Object
251
252
253
254
255
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 251
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
309
310
311
312
313
314
315
316
317
318
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 309
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, "unknown 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
100
101
102
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 100
def web?
storage_type == :web
end
|
#will_be_stored_as ⇒ Object
92
93
94
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 92
def will_be_stored_as
@new_storage_type || storage_type
end
|
#will_become_coded? ⇒ Boolean
112
113
114
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 112
def will_become_coded?
will_be_stored_as == :coded
end
|
#with_storage_options(opts = {}) ⇒ Object
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
# File 'tmpsets/set/mod015-carrierwave/abstract/attachment/storage_type.rb', line 288
def with_storage_options opts={}
old_values = {}
validate_temporary_storage_type_change opts[:storage_type]
[: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
|