active_storage_validations 2.0.1 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +11 -60
- data/config/locales/da.yml +1 -0
- data/config/locales/de.yml +1 -0
- data/config/locales/en-GB.yml +54 -0
- data/config/locales/en.yml +1 -0
- data/config/locales/es.yml +1 -0
- data/config/locales/fr.yml +1 -0
- data/config/locales/it.yml +2 -1
- data/config/locales/ja.yml +1 -0
- data/config/locales/nl.yml +1 -0
- data/config/locales/pl.yml +1 -0
- data/config/locales/pt-BR.yml +1 -0
- data/config/locales/ru.yml +1 -0
- data/config/locales/sv.yml +1 -0
- data/config/locales/tr.yml +1 -0
- data/config/locales/uk.yml +1 -0
- data/config/locales/vi.yml +1 -0
- data/config/locales/zh-CN.yml +1 -0
- data/lib/active_storage_validations/analyzer/audio_analyzer.rb +2 -2
- data/lib/active_storage_validations/analyzer/content_type_analyzer.rb +5 -5
- data/lib/active_storage_validations/analyzer/image_analyzer/image_magick.rb +0 -1
- data/lib/active_storage_validations/analyzer/image_analyzer/vips.rb +0 -1
- data/lib/active_storage_validations/analyzer/video_analyzer.rb +4 -4
- data/lib/active_storage_validations/analyzer.rb +24 -23
- data/lib/active_storage_validations/aspect_ratio_validator.rb +19 -16
- data/lib/active_storage_validations/attached_validator.rb +3 -3
- data/lib/active_storage_validations/base_comparison_validator.rb +6 -6
- data/lib/active_storage_validations/content_type_validator.rb +39 -24
- data/lib/active_storage_validations/dimension_validator.rb +158 -82
- data/lib/active_storage_validations/duration_validator.rb +28 -14
- data/lib/active_storage_validations/extensors/asv_blob_metadatable.rb +15 -8
- data/lib/active_storage_validations/extensors/asv_marcelable.rb +1 -1
- data/lib/active_storage_validations/limit_validator.rb +27 -19
- data/lib/active_storage_validations/matchers/aspect_ratio_validator_matcher.rb +12 -12
- data/lib/active_storage_validations/matchers/attached_validator_matcher.rb +6 -6
- data/lib/active_storage_validations/matchers/base_comparison_validator_matcher.rb +7 -7
- data/lib/active_storage_validations/matchers/content_type_validator_matcher.rb +15 -13
- data/lib/active_storage_validations/matchers/dimension_validator_matcher.rb +8 -8
- data/lib/active_storage_validations/matchers/duration_validator_matcher.rb +2 -2
- data/lib/active_storage_validations/matchers/limit_validator_matcher.rb +8 -8
- data/lib/active_storage_validations/matchers/processable_file_validator_matcher.rb +7 -7
- data/lib/active_storage_validations/matchers/shared/asv_active_storageable.rb +1 -1
- data/lib/active_storage_validations/matchers/shared/asv_allow_blankable.rb +1 -1
- data/lib/active_storage_validations/matchers/shared/asv_attachable.rb +9 -9
- data/lib/active_storage_validations/matchers/shared/asv_contextable.rb +11 -3
- data/lib/active_storage_validations/matchers/shared/asv_messageable.rb +1 -1
- data/lib/active_storage_validations/matchers/shared/asv_rspecable.rb +2 -2
- data/lib/active_storage_validations/matchers/shared/asv_validatable.rb +2 -2
- data/lib/active_storage_validations/matchers/size_validator_matcher.rb +2 -2
- data/lib/active_storage_validations/matchers/total_size_validator_matcher.rb +3 -3
- data/lib/active_storage_validations/matchers.rb +10 -10
- data/lib/active_storage_validations/processable_file_validator.rb +37 -37
- data/lib/active_storage_validations/railtie.rb +7 -1
- data/lib/active_storage_validations/shared/asv_attachable.rb +60 -28
- data/lib/active_storage_validations/shared/asv_errorable.rb +1 -1
- data/lib/active_storage_validations/shared/asv_symbolizable.rb +1 -1
- data/lib/active_storage_validations/size_validator.rb +1 -1
- data/lib/active_storage_validations/total_size_validator.rb +2 -2
- data/lib/active_storage_validations/version.rb +1 -1
- data/lib/active_storage_validations.rb +22 -25
- metadata +3 -2
@@ -18,7 +18,14 @@ module ActiveStorageValidations
|
|
18
18
|
# to perform file analyses.
|
19
19
|
def validate_changed_files_from_metadata(record, attribute, metadata_keys)
|
20
20
|
attachables_and_blobs(record, attribute).each do |attachable, blob|
|
21
|
-
|
21
|
+
metadata = begin
|
22
|
+
metadata_for(blob, attachable, metadata_keys)
|
23
|
+
rescue ActiveStorage::FileNotFoundError
|
24
|
+
add_attachment_missing_error(record, attribute, attachable)
|
25
|
+
next
|
26
|
+
end
|
27
|
+
|
28
|
+
is_valid?(record, attribute, attachable, metadata)
|
22
29
|
end
|
23
30
|
end
|
24
31
|
|
@@ -32,11 +39,7 @@ module ActiveStorageValidations
|
|
32
39
|
# Some file could be passed several times, we just need to perform the
|
33
40
|
# analysis once on the file, therefore the use of #uniq.
|
34
41
|
def attachables_and_blobs(record, attribute)
|
35
|
-
changes =
|
36
|
-
record.attachment_changes[attribute.to_s].presence || record.public_send(attribute)
|
37
|
-
else
|
38
|
-
record.attachment_changes[attribute.to_s]
|
39
|
-
end
|
42
|
+
changes = changes_for(record, attribute)
|
40
43
|
|
41
44
|
return to_enum(:attachables_and_blobs, record, attribute) if changes.blank? || !block_given?
|
42
45
|
|
@@ -49,7 +52,16 @@ module ActiveStorageValidations
|
|
49
52
|
end
|
50
53
|
end
|
51
54
|
|
55
|
+
def changes_for(record, attribute)
|
56
|
+
if record.public_send(attribute).is_a?(ActiveStorage::Attached::One)
|
57
|
+
record.attachment_changes[attribute.to_s].presence || record.public_send(attribute)
|
58
|
+
else
|
59
|
+
record.attachment_changes[attribute.to_s]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
52
63
|
# Retrieve the full declared content_type from attachable.
|
64
|
+
# rubocop:disable Metrics/MethodLength
|
53
65
|
def full_attachable_content_type(attachable)
|
54
66
|
case attachable
|
55
67
|
when ActiveStorage::Blob
|
@@ -71,6 +83,7 @@ module ActiveStorageValidations
|
|
71
83
|
raise_rails_like_error(attachable)
|
72
84
|
end
|
73
85
|
end
|
86
|
+
# rubocop:enable Metrics/MethodLength
|
74
87
|
|
75
88
|
# Retrieve the declared content_type from attachable without potential mime
|
76
89
|
# type parameters (e.g. 'application/x-rar-compressed;version=5')
|
@@ -102,32 +115,36 @@ module ActiveStorageValidations
|
|
102
115
|
end
|
103
116
|
|
104
117
|
# Retrieve the io from attachable.
|
118
|
+
# rubocop:disable Metrics/MethodLength
|
119
|
+
# rubocop:disable Metrics/AbcSize
|
105
120
|
def attachable_io(attachable, max_byte_size: nil)
|
106
121
|
io = case attachable
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
122
|
+
when ActiveStorage::Blob
|
123
|
+
(max_byte_size && supports_blob_download_chunk?) ? attachable.download_chunk(0...max_byte_size) : attachable.download
|
124
|
+
when ActionDispatch::Http::UploadedFile
|
125
|
+
max_byte_size ? attachable.read(max_byte_size) : attachable.read
|
126
|
+
when Rack::Test::UploadedFile
|
127
|
+
max_byte_size ? attachable.read(max_byte_size) : attachable.read
|
128
|
+
when String
|
129
|
+
blob = ActiveStorage::Blob.find_signed!(attachable)
|
130
|
+
(max_byte_size && supports_blob_download_chunk?) ? blob.download_chunk(0...max_byte_size) : blob.download
|
131
|
+
when Hash
|
132
|
+
max_byte_size ? attachable[:io].read(max_byte_size) : attachable[:io].read
|
133
|
+
when File
|
134
|
+
raise_rails_like_error(attachable) unless supports_file_attachment?
|
135
|
+
max_byte_size ? attachable.read(max_byte_size) : attachable.read
|
136
|
+
when Pathname
|
137
|
+
raise_rails_like_error(attachable) unless supports_pathname_attachment?
|
138
|
+
max_byte_size ? attachable.read(max_byte_size) : attachable.read
|
139
|
+
else
|
140
|
+
raise_rails_like_error(attachable)
|
141
|
+
end
|
127
142
|
|
128
143
|
rewind_attachable_io(attachable)
|
129
144
|
io
|
130
145
|
end
|
146
|
+
# rubocop:enable Metrics/MethodLength
|
147
|
+
# rubocop:enable Metrics/AbcSize
|
131
148
|
|
132
149
|
# Rewind the io attachable.
|
133
150
|
def rewind_attachable_io(attachable)
|
@@ -150,6 +167,7 @@ module ActiveStorageValidations
|
|
150
167
|
end
|
151
168
|
|
152
169
|
# Retrieve the declared filename from attachable.
|
170
|
+
# rubocop:disable Metrics/MethodLength
|
153
171
|
def attachable_filename(attachable)
|
154
172
|
case attachable
|
155
173
|
when ActiveStorage::Blob
|
@@ -171,6 +189,7 @@ module ActiveStorageValidations
|
|
171
189
|
raise_rails_like_error(attachable)
|
172
190
|
end
|
173
191
|
end
|
192
|
+
# rubocop:enable Metrics/MethodLength
|
174
193
|
|
175
194
|
# Raise the same Rails error for not-implemented file representations.
|
176
195
|
def raise_rails_like_error(attachable)
|
@@ -185,7 +204,7 @@ module ActiveStorageValidations
|
|
185
204
|
#
|
186
205
|
# https://github.com/rails/rails/blob/7-1-stable/activestorage/CHANGELOG.md#rails-710rc1-september-27-2023
|
187
206
|
def supports_file_attachment?
|
188
|
-
Rails.gem_version >= Gem::Version.new(
|
207
|
+
Rails.gem_version >= Gem::Version.new("7.1.0.rc1")
|
189
208
|
end
|
190
209
|
alias :supports_pathname_attachment? :supports_file_attachment?
|
191
210
|
|
@@ -193,12 +212,25 @@ module ActiveStorageValidations
|
|
193
212
|
#
|
194
213
|
# https://github.com/rails/rails/blob/7-0-stable/activestorage/CHANGELOG.md#rails-700alpha1-september-15-2021
|
195
214
|
def supports_blob_download_chunk?
|
196
|
-
Rails.gem_version >= Gem::Version.new(
|
215
|
+
Rails.gem_version >= Gem::Version.new("7.0.0.alpha1")
|
197
216
|
end
|
198
217
|
|
199
218
|
# Retrieve the content_type from the file name only
|
200
219
|
def marcel_content_type_from_filename(attachable)
|
201
220
|
Marcel::MimeType.for(name: attachable_filename(attachable).to_s)
|
202
221
|
end
|
222
|
+
|
223
|
+
# Add a media metadata missing error when metadata is missing.
|
224
|
+
def add_media_metadata_missing_error(record, attribute, attachable, already_set_errors_options = nil)
|
225
|
+
errors_options = already_set_errors_options || initialize_error_options(options, attachable)
|
226
|
+
add_error(record, attribute, :media_metadata_missing, **errors_options)
|
227
|
+
end
|
228
|
+
|
229
|
+
# Add an attachment missing error when an ActiveStorage::FileNotFoundError
|
230
|
+
# is raised.
|
231
|
+
def add_attachment_missing_error(record, attribute, attachable)
|
232
|
+
errors_options = initialize_error_options(options, attachable)
|
233
|
+
add_error(record, attribute, :attachment_missing, **errors_options)
|
234
|
+
end
|
203
235
|
end
|
204
236
|
end
|
@@ -5,7 +5,7 @@ module ActiveStorageValidations
|
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
def initialize_error_options(options, file = nil)
|
8
|
-
not_explicitly_written_options = %i
|
8
|
+
not_explicitly_written_options = %i[with in]
|
9
9
|
curated_options = options.except(*not_explicitly_written_options)
|
10
10
|
|
11
11
|
active_storage_validations_options = {
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative "base_comparison_validator"
|
4
4
|
|
5
5
|
module ActiveStorageValidations
|
6
6
|
class TotalSizeValidator < BaseComparisonValidator
|
@@ -40,7 +40,7 @@ module ActiveStorageValidations
|
|
40
40
|
# We can't perform this check in the #check_validity! hook because we do not
|
41
41
|
# have enough data (only options & attributes are accessible)
|
42
42
|
unless record.send(attribute).is_a?(ActiveStorage::Attached::Many)
|
43
|
-
raise ArgumentError,
|
43
|
+
raise ArgumentError, "This validator is only available for has_many_attached relations"
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
@@ -1,31 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "active_model"
|
4
|
+
require "active_support/concern"
|
5
5
|
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require
|
11
|
-
require
|
12
|
-
require
|
6
|
+
require "active_storage_validations/analyzer"
|
7
|
+
require "active_storage_validations/analyzer/image_analyzer"
|
8
|
+
require "active_storage_validations/analyzer/image_analyzer/image_magick"
|
9
|
+
require "active_storage_validations/analyzer/image_analyzer/vips"
|
10
|
+
require "active_storage_validations/analyzer/null_analyzer"
|
11
|
+
require "active_storage_validations/analyzer/video_analyzer"
|
12
|
+
require "active_storage_validations/analyzer/audio_analyzer"
|
13
13
|
|
14
|
-
require
|
15
|
-
require
|
14
|
+
require "active_storage_validations/extensors/asv_blob_metadatable"
|
15
|
+
require "active_storage_validations/extensors/asv_marcelable"
|
16
16
|
|
17
|
-
require
|
18
|
-
require
|
19
|
-
require
|
20
|
-
require
|
21
|
-
require
|
22
|
-
require
|
23
|
-
require
|
24
|
-
require
|
25
|
-
require
|
26
|
-
require 'active_storage_validations/size_validator'
|
27
|
-
require 'active_storage_validations/total_size_validator'
|
17
|
+
require "active_storage_validations/attached_validator"
|
18
|
+
require "active_storage_validations/content_type_validator"
|
19
|
+
require "active_storage_validations/limit_validator"
|
20
|
+
require "active_storage_validations/dimension_validator"
|
21
|
+
require "active_storage_validations/duration_validator"
|
22
|
+
require "active_storage_validations/aspect_ratio_validator"
|
23
|
+
require "active_storage_validations/processable_file_validator"
|
24
|
+
require "active_storage_validations/size_validator"
|
25
|
+
require "active_storage_validations/total_size_validator"
|
28
26
|
|
29
|
-
|
30
|
-
|
31
|
-
end
|
27
|
+
require "active_storage_validations/engine"
|
28
|
+
require "active_storage_validations/railtie"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_storage_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Kasyanchuk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
11
|
+
date: 2025-04-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activejob
|
@@ -244,6 +244,7 @@ files:
|
|
244
244
|
- Rakefile
|
245
245
|
- config/locales/da.yml
|
246
246
|
- config/locales/de.yml
|
247
|
+
- config/locales/en-GB.yml
|
247
248
|
- config/locales/en.yml
|
248
249
|
- config/locales/es.yml
|
249
250
|
- config/locales/fr.yml
|