card-mod-carrierwave 0.13.2 → 0.14.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 07c72b8dff10b57cf9069cf660f32d736cf67df866ea979c6518c46b1285afcb
4
- data.tar.gz: f4442c5099e32a4fbfae63a44fe9e9a172ae11b26509f7d0a56a21a5f1f44a42
3
+ metadata.gz: a9f40c24667705b62968b1a43a7934b3579fc7d67e3442c73182c4c9c85a4242
4
+ data.tar.gz: 84f324d97c7d194785b5566a64799e0533334d560f91450f1a9517d6bd70cf42
5
5
  SHA512:
6
- metadata.gz: 38e990a4f6232af77d5204a1f4799b08f67e7d0948b5c64211a0eeaeb6900c1b431abac5e519f8d5e567f98c1c7dda174fddc037c6b67eb683f57708eaa541cd
7
- data.tar.gz: b4064e332a60251c6bcce1d39a7cbbde7152f7e4109de401fbe643cfcbff4bff0561dcaa53da6c30356299a56b73eccaa071fcdfb6fd40e980a212a0e6be5b4d
6
+ metadata.gz: 85e5549724e44cb356b0c19290f0eb4941e7b2507b1f582f14d8b20ffce5e43b77954351b590393dbfc1d3815ae5739ccc795fceb661f91213c8aaed4023d7c4
7
+ data.tar.gz: 9c3818ab31b033e689adc355779c36481c781f6f2468a633ca0d5666908ed7be1734dafc10d483445e27afecfb22b5ad8cc27498b2200aee5824445c3e1e56c4
@@ -0,0 +1,4 @@
1
+ require "mini_magick"
2
+
3
+ # This call fails if ImageMagick isn't installed (common installation problem)
4
+ MiniMagick.cli
@@ -29,8 +29,7 @@ module CarrierWave
29
29
  remove_#{column}!
30
30
  end
31
31
 
32
- event :mark_remove_#{column}_false_event, :finalize,
33
- on: :update do
32
+ event :mark_remove_#{column}_false_event, :finalize, on: :update do
34
33
  mark_remove_#{column}_false
35
34
  end
36
35
 
@@ -54,6 +53,7 @@ module CarrierWave
54
53
  end
55
54
 
56
55
  def store_attachment!
56
+ set_specific.delete :#{column}
57
57
  store_#{column}!
58
58
  end
59
59
 
@@ -116,21 +116,17 @@ module CarrierWave
116
116
  @#{column}_changed
117
117
  end
118
118
 
119
- def serializable_hash(options=nil)
120
- hash = {}
119
+ def serializable_hash(opts=nil)
120
+ except = opts&.dig(:except) && Array.wrap(opts[:except]).map(&:to_s)
121
+ only = opts&.dig(:only) && Array.wrap(opts[:only]).map(&:to_s)
121
122
 
122
- except = options && options[:except] &&
123
- Array.wrap(options[:except]).map(&:to_s)
124
- only = options && options[:only] &&
125
- Array.wrap(options[:only]).map(&:to_s)
126
-
127
- self.class.uploaders.each do |column, uploader|
128
- if (!only && !except) || (only && only.include?(column.to_s)) ||
123
+ self.class.uploaders.each_with_object(super(opts)) do |(column, uploader), hash|
124
+ if (!only && !except) ||
125
+ (only && only.include?(column.to_s)) ||
129
126
  (!only && except && !except.include?(column.to_s))
130
127
  hash[column.to_s] = _mounter(column).uploader.serializable_hash
131
128
  end
132
129
  end
133
- super(options).merge(hash)
134
130
  end
135
131
  RUBY
136
132
  end
@@ -7,7 +7,7 @@ module CarrierWave
7
7
  # path-related methods for uploader
8
8
  module Path
9
9
  def local_url opts={}
10
- "%s/%s/%s" % [local_url_base(opts), file_dir, full_filename(url_filename(opts))]
10
+ "%s/%s/%s" % [local_url_base(opts), file_dir, full_filename(url_filename)]
11
11
  end
12
12
 
13
13
  def local_url_base opts={}
@@ -145,7 +145,7 @@ module CarrierWave
145
145
 
146
146
  def filename
147
147
  if model.coded?
148
- "#{model.type_code}#{extension}"
148
+ "#{model.attachment_name}#{extension}"
149
149
  else
150
150
  "#{action_id}#{extension}"
151
151
  end
@@ -167,22 +167,18 @@ module CarrierWave
167
167
  # @option opts [Symbol] storage_type
168
168
  # @option opts [String] mod
169
169
  # @option opts [Symbol] bucket
170
- def db_content opts={}
171
- model.with_storage_options opts do
172
- return model.content if model.web?
173
- return "" unless file.present?
170
+ def db_content
171
+ return model.content if model.web?
172
+ return "" unless file.present?
174
173
 
175
- "%s/%s" % [file_dir, url_filename]
176
- end
174
+ "%s/%s" % [file_dir, url_filename]
177
175
  end
178
176
 
179
- def url_filename opts={}
180
- model.with_storage_options opts do
181
- if model.coded?
182
- "#{model.mod}#{extension}"
183
- else
184
- "#{action_id}#{extension}"
185
- end
177
+ def url_filename
178
+ if model.coded?
179
+ "#{model.mod}#{extension}"
180
+ else
181
+ "#{action_id}#{extension}"
186
182
  end
187
183
  end
188
184
 
@@ -206,7 +202,9 @@ module CarrierWave
206
202
  end
207
203
 
208
204
  def action_id
209
- model.selected_content_action_id || action_id_stand_in
205
+ model.selected_content_action_id ||
206
+ model.last_content_action_id ||
207
+ action_id_stand_in
210
208
  end
211
209
 
212
210
  # delegate carrierwave's fog config methods to bucket configuration
@@ -227,7 +225,7 @@ module CarrierWave
227
225
  # used as action_id in the filename
228
226
  # if card is not #actionable?
229
227
  def action_id_stand_in
230
- Time.now.to_i
228
+ @action_id_stand_in ||= Time.now.to_i
231
229
  end
232
230
 
233
231
  def storage
data/locales/de.yml ADDED
@@ -0,0 +1,13 @@
1
+ de:
2
+ carrierwave_add_item: "%{attachment_name} hinzufügen..."
3
+ carrierwave_codename_needed_for_storage: Codename erforderlich für Speichertyp coded
4
+ carrierwave_delete: Löschen
5
+ carrierwave_download: Download %{title}
6
+ carrierwave_error_invalid_storage_type: "%{type} ist keine gültige Dateispeicheroption"
7
+ carrierwave_file_error: Dateifehler
8
+ carrierwave_is_missing: fehlt
9
+ carrierwave_mod_argument_needed_to_save: mod-Argument erforderlich um Karte als coded zu speichern.
10
+ carrierwave_moving_files_is_not_supported: verschieben von Dateien aus einer Cloud wird nicht unterstützt
11
+ carrierwave_replace_item: Ersetze %{attachment_name}...
12
+ carrierwave_unknown_storage_type: unbekannter Speichertyp %{new_storage_type}
13
+ carrierwave_url_is_missing: Url fehlt
@@ -42,6 +42,7 @@ event :assign_attachment_on_update, :initialize,
42
42
  end
43
43
 
44
44
  def assign_attachment file, original_filename
45
+ @attaching = true
45
46
  send "#{attachment_name}=", file
46
47
  write_identifier
47
48
  @current_action&.update! comment: original_filename
@@ -63,7 +64,7 @@ end
63
64
 
64
65
  event :delete_cached_upload_file_on_update, :integrate,
65
66
  on: :update, when: :save_preliminary_upload? do
66
- return unless (action = Card::Action.fetch(@action_id_of_cached_upload))
67
+ return unless (action = Card::Action.fetch(@action_id_of_cached_upload))
67
68
 
68
69
  delete_files_for_action action
69
70
  action.delete
@@ -76,7 +77,7 @@ def upload_cache_card
76
77
  end
77
78
 
78
79
  def preliminary_upload?
79
- Card::Env && Card::Env.params[:attachment_upload]
80
+ Card::Env.params[:attachment_upload]
80
81
  end
81
82
 
82
83
  def save_preliminary_upload?
@@ -0,0 +1,105 @@
1
+ attr_writer :bucket, :new_storage_type
2
+
3
+ event :update_storage, :store, on: :update, when: :storage_changed? do
4
+ send "#{attachment_name}=", current.attachment.file unless @attaching
5
+ write_identifier
6
+ end
7
+
8
+ event :validate_storage_type, :validate, on: :save do
9
+ errors.add :storage_type, unknown_storage_type unless known_storage_type?
10
+ end
11
+
12
+ def current
13
+ @current ||= refresh true
14
+ end
15
+
16
+ def read_only?
17
+ web? || (cloud? && bucket_config[:read_only])
18
+ end
19
+
20
+ def cloud?
21
+ storage_type == :cloud
22
+ end
23
+
24
+ def web?
25
+ storage_type == :web
26
+ end
27
+
28
+ def local?
29
+ storage_type == :local
30
+ end
31
+
32
+ def coded?
33
+ storage_type == :coded
34
+ end
35
+
36
+ def remote_storage?
37
+ cloud? || web?
38
+ end
39
+
40
+ def storage_type= value
41
+ @explicit_storage_type = true
42
+ @storage_type = value&.to_sym
43
+ end
44
+
45
+ def storage_type
46
+ @storage_type ||= new_card? ? storage_type_from_config : storage_type_from_content
47
+ end
48
+
49
+ def storage_type_from_config
50
+ valid_storage_type ENV["FILE_STORAGE"] || Cardio.config.file_storage
51
+ end
52
+
53
+ def valid_storage_type storage_type
54
+ storage_type.to_sym.tap do |type|
55
+ invalid_storage_type! type unless type.in? valid_storage_type_list
56
+ end
57
+ end
58
+
59
+ def valid_storage_type_list
60
+ CarrierWave::FileCardUploader::STORAGE_TYPES
61
+ end
62
+
63
+ def invalid_storage_type! type
64
+ raise Card::Error, t(:carrierwave_error_invalid_storage_type, type: type)
65
+ end
66
+
67
+ def storage_type_from_content
68
+ @storage_type_from_content ||=
69
+ case content
70
+ when /^\(/ then :cloud
71
+ when %r{/^https?:/} then :web
72
+ when /^~/ then :local
73
+ when /^:/ then :coded
74
+ else
75
+ if deprecated_mod_file?
76
+ :coded
77
+ else
78
+ storage_type_from_config
79
+ end
80
+ end
81
+ end
82
+
83
+ def storage_changed?
84
+ (storage_type != current.storage_type) ||
85
+ (bucket != current.bucket) ||
86
+ (mod != current.mod)
87
+ end
88
+
89
+ def known_storage_type?
90
+ storage_type.in? CarrierWave::FileCardUploader::STORAGE_TYPES
91
+ end
92
+
93
+ def unknown_storage_type
94
+ t :carrierwave_unknown_storage_type, new_storage_type: storage_type
95
+ end
96
+
97
+ def file_updated_at
98
+ if coded?
99
+ File.mtime file.path
100
+ else
101
+ updated_at
102
+ end
103
+ rescue Errno::ENOENT # no file at path
104
+ nil
105
+ end
@@ -1,17 +1,8 @@
1
+ attr_writer :bucket
2
+
1
3
  event :change_bucket_if_read_only, :initialize,
2
4
  on: :update, when: :change_bucket_if_read_only? do
3
- @new_storage_type = storage_type_from_config
4
- end
5
-
6
- event :validate_storage_type_update, :validate, on: :update, when: :cloud? do
7
- # FIXME: make it possible to retrieve the file from cloud storage
8
- # to store it somewhere else. Currently, it only works to change the
9
- # storage type if a new file is provided
10
- # i.e. `update storage_type: :local` fails but
11
- # `update storage_type: :local, file: [file handle]` is ok
12
- return unless storage_type_changed? && !attachment_is_changing?
13
-
14
- errors.add :storage_type, t(:carrierwave_moving_files_is_not_supported)
5
+ @storage_type = storage_type_from_config
15
6
  end
16
7
 
17
8
  def bucket
@@ -127,11 +118,3 @@ end
127
118
  def change_bucket_if_read_only?
128
119
  cloud? && bucket_config[:read_only] && attachment_is_changing?
129
120
  end
130
-
131
- def bucket= value
132
- if @action == :update
133
- @new_bucket = value
134
- else
135
- @bucket = value
136
- end
137
- end
@@ -1,27 +1,62 @@
1
- event :lose_coded_status_on_update, :initialize, on: :update, when: :coded? do
2
- # unless explicit
3
- return if @new_mod
1
+ MOD_FILE_DIR = "data/files".freeze
4
2
 
5
- @new_storage_type ||= storage_type_from_config
3
+ event :uncode_attachment_storage, :initialize, on: :update, when: :uncode? do
4
+ @storage_type = storage_type_from_config unless @explicit_storage_type
6
5
  end
7
6
 
8
- event :validate_coded_storage_type, :validate, on: :save, when: :will_become_coded? do
9
- storage_type_error :mod_argument_needed_to_save unless mod || @new_mod
7
+ event :validate_coded_storage_type, :validate, on: :save, when: :coded? do
8
+ storage_type_error :mod_argument_needed_to_save unless mod
10
9
  storage_type_error :codename_needed_for_storage if codename.blank?
11
10
  end
12
11
 
13
- def storage_type_error error_name
14
- errors.add :storage_type, t("carrierwave_#{error_name}")
12
+ def mod= value
13
+ @mod = value.to_s
15
14
  end
16
15
 
17
- def will_become_coded?
18
- will_be_stored_as == :coded
16
+ def mod
17
+ @mod ||= coded? && mod_from_content
19
18
  end
20
19
 
21
- def mod= value
22
- if @action == :update && mod != value
23
- @new_mod = value.to_s
20
+ private
21
+
22
+ def uncode?
23
+ (@explicit_storage_type != :coded) && !set_specific[:mod].present? && current.coded?
24
+ # (@explicit_storage_type != :coded) && !mod && current.coded?
25
+ end
26
+
27
+ def storage_type_error error_name
28
+ errors.add :storage_type, t("carrierwave_#{error_name}")
29
+ end
30
+
31
+ def mod_from_content
32
+ if (m = content.match %r{^:[^/]+/([^.]+)})
33
+ m[1] # current mod_file format
24
34
  else
25
- @mod = value.to_s
35
+ mod_from_deprecated_content
26
36
  end
27
37
  end
38
+
39
+ # place for files of mod file cards
40
+ def coded_dir new_mod=nil
41
+ dir = File.join mod_dir(new_mod), MOD_FILE_DIR, codename.to_s
42
+ FileUtils.mkdir_p(dir) unless File.directory?(dir)
43
+ dir
44
+ end
45
+
46
+ def mod_dir new_mod=nil
47
+ mod_name = new_mod || mod
48
+ dir = Cardio::Mod.dirs.path(mod_name) || (mod_name.to_sym == :test && "test")
49
+
50
+ raise Error, "can't find mod \"#{mod_name}\"" unless dir
51
+
52
+ dir
53
+ end
54
+
55
+ def deprecated_mod_file?
56
+ content.present? && !content.match?(/^~/) && content.split("\n")&.size == 4
57
+ end
58
+
59
+ # old format is still used in card_changes
60
+ def mod_from_deprecated_content
61
+ content.split("\n").last if deprecated_mod_file?
62
+ end
@@ -1,7 +1,5 @@
1
- MOD_FILE_DIR = "file".freeze
2
-
3
1
  def store_dir
4
- will_become_coded? ? coded_dir(@new_mod) : upload_dir
2
+ coded? ? coded_dir : upload_dir
5
3
  end
6
4
 
7
5
  def retrieve_dir
@@ -13,22 +11,6 @@ def upload_dir
13
11
  id ? "#{files_base_dir}/#{id}" : tmp_upload_dir
14
12
  end
15
13
 
16
- # place for files of mod file cards
17
- def coded_dir new_mod=nil
18
- dir = File.join mod_dir(new_mod), MOD_FILE_DIR, codename.to_s
19
- FileUtils.mkdir_p(dir) unless File.directory?(dir)
20
- dir
21
- end
22
-
23
- def mod_dir new_mod=nil
24
- mod_name = new_mod || mod
25
- dir = Cardio::Mod.dirs.path(mod_name) || (mod_name.to_sym == :test && "test")
26
-
27
- raise Error, "can't find mod \"#{mod_name}\"" unless dir
28
-
29
- dir
30
- end
31
-
32
14
  def files_base_dir
33
15
  dir = bucket ? bucket_config[:subdirectory] : Card.paths["files"].existent.first
34
16
  dir || files_base_dir_configuration_error
@@ -11,7 +11,7 @@ end
11
11
  # we need a card id for the path so we have to update db_content when we have
12
12
  # an id
13
13
  event :correct_identifier, :finalize, on: :create, when: proc { |c| !c.web? } do
14
- update_column(:db_content, attachment.db_content)
14
+ update_column :db_content, attachment.db_content
15
15
  expire
16
16
  end
17
17
 
@@ -24,7 +24,7 @@ end
24
24
  event :validate_file_exist, :validate, on: :create do
25
25
  return if empty_ok?
26
26
 
27
- if will_be_stored_as == :web
27
+ if web?
28
28
  errors.add "url is missing" if content.blank?
29
29
  elsif !attachment.file.present?
30
30
  errors.add attachment_name, "is missing"
@@ -54,7 +54,7 @@ def original_filename
54
54
  end
55
55
 
56
56
  def unfilled?
57
- !attachment.present? && !save_preliminary_upload? && !subcards? && blank_content?
57
+ !attachment.present? && !save_preliminary_upload? && super
58
58
  end
59
59
 
60
60
  def attachment_changed?
@@ -78,8 +78,9 @@ def empty_ok?
78
78
  end
79
79
 
80
80
  def assign_set_specific_attributes
81
+ @attaching = set_specific[attachment_name].present?
81
82
  # reset content if we really have something to upload
82
- self.content = nil if set_specific[attachment_name].present?
83
+ self.content = nil if @attaching
83
84
  super
84
85
  end
85
86
 
data/set/self/admin.rb CHANGED
@@ -1,23 +1,22 @@
1
- add_to_basket(
2
- :tasks,
3
- name: :update_file_storage_locations,
4
- execute_policy: -> { Card.update_all_storage_locations },
5
- stats: {
6
- title: "cards with attachment",
7
- count: -> { Card.count_cards_with_attachment }
8
- # link_text: "update storage locations",
9
- # task: "update_file_storage_locations"
1
+ basket[:tasks] += [
2
+ {
3
+ name: :update_file_storage_locations,
4
+ execute_policy: -> { Card.update_all_storage_locations },
5
+ stats: {
6
+ title: "cards with attachment",
7
+ count: -> { Card.count_cards_with_attachment }
8
+ # link_text: "update storage locations",
9
+ # task: "update_file_storage_locations"
10
+ }
11
+ },
12
+ {
13
+ name: :delete_upload_tmp_files,
14
+ execute_policy: -> { Card.delete_tmp_files_of_cached_uploads },
15
+ stats: {
16
+ title: "tmp files of canceled uploads",
17
+ count: -> { ::Card.draft_actions_with_attachment },
18
+ link_text: "delete tmp files",
19
+ task: "delete_tmp_files_of_cached_uploads"
20
+ }
10
21
  }
11
- )
12
-
13
- add_to_basket(
14
- :tasks,
15
- name: :delete_upload_tmp_files,
16
- execute_policy: -> { Card.delete_tmp_files_of_cached_uploads },
17
- stats: {
18
- title: "tmp files of canceled uploads",
19
- count: -> { ::Card.draft_actions_with_attachment },
20
- link_text: "delete tmp files",
21
- task: "delete_tmp_files_of_cached_uploads"
22
- }
23
- )
22
+ ]
data/set/type/file.rb CHANGED
@@ -8,8 +8,6 @@ module SelectedAction
8
8
  end
9
9
 
10
10
  def last_content_action_id
11
- return super if temporary_storage_type_change?
12
-
13
11
  # find action id from content (saves lookups)
14
12
  db_content.to_s.split(%r{[/.]})[-2]
15
13
  end
data/set/type/image.rb CHANGED
@@ -21,7 +21,7 @@ format do
21
21
  render_core size: :icon
22
22
  end
23
23
 
24
- view :source do
24
+ view :source, unknown: :blank do
25
25
  return card.content if card.web?
26
26
 
27
27
  image = selected_version
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: card-mod-carrierwave
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.2
4
+ version: 0.14.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ethan McCutchen
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-08-06 00:00:00.000000000 Z
13
+ date: 2022-01-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: card
@@ -18,14 +18,14 @@ dependencies:
18
18
  requirements:
19
19
  - - '='
20
20
  - !ruby/object:Gem::Version
21
- version: 1.103.2
21
+ version: 1.104.1
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - '='
27
27
  - !ruby/object:Gem::Version
28
- version: 1.103.2
28
+ version: 1.104.1
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: carrierwave
31
31
  requirement: !ruby/object:Gem::Requirement
@@ -60,28 +60,28 @@ dependencies:
60
60
  requirements:
61
61
  - - '='
62
62
  - !ruby/object:Gem::Version
63
- version: 0.13.2
63
+ version: 0.14.1
64
64
  type: :runtime
65
65
  prerelease: false
66
66
  version_requirements: !ruby/object:Gem::Requirement
67
67
  requirements:
68
68
  - - '='
69
69
  - !ruby/object:Gem::Version
70
- version: 0.13.2
70
+ version: 0.14.1
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: card-mod-permissions
73
73
  requirement: !ruby/object:Gem::Requirement
74
74
  requirements:
75
75
  - - '='
76
76
  - !ruby/object:Gem::Version
77
- version: 0.13.2
77
+ version: 0.14.1
78
78
  type: :runtime
79
79
  prerelease: false
80
80
  version_requirements: !ruby/object:Gem::Requirement
81
81
  requirements:
82
82
  - - '='
83
83
  - !ruby/object:Gem::Version
84
- version: 0.13.2
84
+ version: 0.14.1
85
85
  description: ''
86
86
  email:
87
87
  - info@decko.org
@@ -90,18 +90,20 @@ extensions: []
90
90
  extra_rdoc_files: []
91
91
  files:
92
92
  - init/early/carrierwave.rb
93
+ - init/early/mini_magick.rb
93
94
  - lib/carrier_wave/card_mount.rb
94
95
  - lib/carrier_wave/file_card_uploader.rb
95
96
  - lib/carrier_wave/file_card_uploader/path.rb
96
97
  - lib/carrier_wave/image_card_uploader.rb
98
+ - locales/de.yml
97
99
  - locales/en.yml
98
100
  - set/abstract/attachment.rb
101
+ - set/abstract/attachment/00_upload_cache.rb
102
+ - set/abstract/attachment/01_storage_type.rb
99
103
  - set/abstract/attachment/cloud.rb
100
104
  - set/abstract/attachment/coded.rb
101
105
  - set/abstract/attachment/local.rb
102
106
  - set/abstract/attachment/paths.rb
103
- - set/abstract/attachment/storage_type.rb
104
- - set/abstract/attachment/upload_cache.rb
105
107
  - set/abstract/attachment/web.rb
106
108
  - set/all/file_utils.rb
107
109
  - set/self/admin.rb
@@ -139,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
141
  - !ruby/object:Gem::Version
140
142
  version: '0'
141
143
  requirements: []
142
- rubygems_version: 3.1.6
144
+ rubygems_version: 3.2.15
143
145
  signing_key:
144
146
  specification_version: 4
145
147
  summary: File and Image handling
@@ -1,171 +0,0 @@
1
- attr_writer :bucket, :new_storage_type
2
-
3
- event :storage_type_change, :store, on: :update, when: :storage_type_changed? do
4
- # carrierwave stores file if @cache_id is not nil
5
- attachment.cache_stored_file!
6
- # attachment.retrieve_from_cache!(attachment.cache_name)
7
- update_storage_attributes
8
- # next line might be necessary to move files to cloud
9
-
10
- # make sure that we get the new identifier
11
- # otherwise action_id will return wrong id for new identifier
12
- db_content_will_change!
13
- write_identifier
14
- end
15
-
16
- event :validate_storage_type, :validate, on: :save do
17
- return if known_storage_type? will_be_stored_as
18
-
19
- errors.add :storage_type, unknown_storage_type(@new_storage_type)
20
- end
21
-
22
- def will_be_stored_as
23
- @new_storage_type || storage_type
24
- end
25
-
26
- def read_only?
27
- web? || (cloud? && bucket_config[:read_only])
28
- end
29
-
30
- def cloud?
31
- storage_type == :cloud
32
- end
33
-
34
- def web?
35
- storage_type == :web
36
- end
37
-
38
- def local?
39
- storage_type == :local
40
- end
41
-
42
- def coded?
43
- storage_type == :coded
44
- end
45
-
46
- def remote_storage?
47
- cloud? || web?
48
- end
49
-
50
- def storage_type
51
- @storage_type ||=
52
- new_card? ? storage_type_from_config : storage_type_from_content
53
- end
54
-
55
- def deprecated_mod_file?
56
- content && (lines = content.split("\n")) && lines.size == 4
57
- end
58
-
59
- def mod
60
- @mod ||= coded? && mod_from_content
61
- end
62
-
63
- def mod_from_content
64
- if (m = content.match %r{^:[^/]+/([^.]+)})
65
- m[1] # current mod_file format
66
- else
67
- mod_from_deprecated_content
68
- end
69
- end
70
-
71
- # old format is still used in card_changes
72
- def mod_from_deprecated_content
73
- return if content.match?(/^~/)
74
- return unless (lines = content.split("\n")) && lines.size == 4
75
-
76
- lines.last
77
- end
78
-
79
- def storage_type_from_config
80
- valid_storage_type ENV["FILE_STORAGE"] || Cardio.config.file_storage
81
- end
82
-
83
- def valid_storage_type storage_type
84
- storage_type.to_sym.tap do |type|
85
- invalid_storage_type! type unless type.in? valid_storage_type_list
86
- end
87
- end
88
-
89
- def valid_storage_type_list
90
- CarrierWave::FileCardUploader::STORAGE_TYPES
91
- end
92
-
93
- def invalid_storage_type! type
94
- raise Card::Error, t(:carrierwave_error_invalid_storage_type, type: type)
95
- end
96
-
97
- def storage_type_from_content
98
- case content
99
- when /^\(/ then :cloud
100
- when %r{/^https?:/} then :web
101
- when /^~/ then :local
102
- when /^:/ then :coded
103
- else
104
- if deprecated_mod_file?
105
- :coded
106
- else
107
- storage_type_from_config
108
- end
109
- end
110
- end
111
-
112
- def update_storage_attributes
113
- @mod = @new_mod if @new_mod
114
- @bucket = @new_bucket if @new_bucket
115
- @storage_type = @new_storage_type
116
- end
117
-
118
- def storage_type_changed?
119
- @new_bucket || (@new_storage_type && @new_storage_type != storage_type) || @new_mod
120
- end
121
-
122
- def storage_type= value
123
- known_storage_type? value
124
- if @action == :update # && storage_type != value
125
- # we cant update the storage type directly here
126
- # if we do then the uploader doesn't find the file we want to update
127
- @new_storage_type = value
128
- else
129
- @storage_type = value
130
- end
131
- end
132
-
133
- def with_storage_options opts={}
134
- old_values = stash_and_set_storage_options opts
135
- validate_temporary_storage_type_change opts[:storage_type]
136
- @temp_storage_type = true
137
- yield
138
- ensure
139
- @temp_storage_type = false
140
- old_values.each { |key, val| instance_variable_set "@#{key}", val }
141
- end
142
-
143
- def stash_and_set_storage_options opts
144
- %i[storage_type mod bucket].each_with_object({}) do |opt_name, old_values|
145
- next unless opts[opt_name]
146
-
147
- old_values[opt_name] = instance_variable_get "@#{opt_name}"
148
- instance_variable_set "@#{opt_name}", opts[opt_name]
149
- old_values
150
- end
151
- end
152
-
153
- def temporary_storage_type_change?
154
- @temp_storage_type
155
- end
156
-
157
- def validate_temporary_storage_type_change type=nil
158
- return unless type ||= @new_storage_type
159
- raise Error, unknown_storage_type(type) unless known_storage_type? type
160
- if type == :coded && codename.blank?
161
- raise Error, "codename needed for storage type :coded"
162
- end
163
- end
164
-
165
- def known_storage_type? type=storage_type
166
- type.in? CarrierWave::FileCardUploader::STORAGE_TYPES
167
- end
168
-
169
- def unknown_storage_type type
170
- t :carrierwave_unknown_storage_type, new_storage_type: type
171
- end