active_storage_validations 0.9.7 → 2.0.2
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 +737 -229
- data/config/locales/da.yml +53 -0
- data/config/locales/de.yml +50 -19
- data/config/locales/en.yml +50 -19
- data/config/locales/es.yml +50 -19
- data/config/locales/fr.yml +50 -19
- data/config/locales/it.yml +50 -19
- data/config/locales/ja.yml +50 -19
- data/config/locales/nl.yml +50 -19
- data/config/locales/pl.yml +50 -19
- data/config/locales/pt-BR.yml +50 -19
- data/config/locales/ru.yml +50 -19
- data/config/locales/sv.yml +53 -0
- data/config/locales/tr.yml +50 -19
- data/config/locales/uk.yml +50 -19
- data/config/locales/vi.yml +50 -19
- data/config/locales/zh-CN.yml +53 -0
- data/lib/active_storage_validations/analyzer/audio_analyzer.rb +58 -0
- data/lib/active_storage_validations/analyzer/content_type_analyzer.rb +60 -0
- data/lib/active_storage_validations/analyzer/image_analyzer/image_magick.rb +47 -0
- data/lib/active_storage_validations/analyzer/image_analyzer/vips.rb +57 -0
- data/lib/active_storage_validations/analyzer/image_analyzer.rb +49 -0
- data/lib/active_storage_validations/analyzer/null_analyzer.rb +18 -0
- data/lib/active_storage_validations/analyzer/shared/asv_ff_probable.rb +61 -0
- data/lib/active_storage_validations/analyzer/video_analyzer.rb +130 -0
- data/lib/active_storage_validations/analyzer.rb +87 -0
- data/lib/active_storage_validations/aspect_ratio_validator.rb +154 -99
- data/lib/active_storage_validations/attached_validator.rb +22 -5
- data/lib/active_storage_validations/base_comparison_validator.rb +71 -0
- data/lib/active_storage_validations/content_type_validator.rb +206 -25
- data/lib/active_storage_validations/dimension_validator.rb +105 -82
- data/lib/active_storage_validations/duration_validator.rb +55 -0
- data/lib/active_storage_validations/extensors/asv_blob_metadatable.rb +49 -0
- data/lib/active_storage_validations/extensors/asv_marcelable.rb +12 -0
- data/lib/active_storage_validations/limit_validator.rb +75 -16
- data/lib/active_storage_validations/matchers/aspect_ratio_validator_matcher.rb +119 -0
- data/lib/active_storage_validations/matchers/attached_validator_matcher.rb +48 -25
- data/lib/active_storage_validations/matchers/base_comparison_validator_matcher.rb +140 -0
- data/lib/active_storage_validations/matchers/content_type_validator_matcher.rb +94 -59
- data/lib/active_storage_validations/matchers/dimension_validator_matcher.rb +97 -55
- data/lib/active_storage_validations/matchers/duration_validator_matcher.rb +39 -0
- data/lib/active_storage_validations/matchers/limit_validator_matcher.rb +127 -0
- data/lib/active_storage_validations/matchers/processable_file_validator_matcher.rb +78 -0
- data/lib/active_storage_validations/matchers/shared/asv_active_storageable.rb +19 -0
- data/lib/active_storage_validations/matchers/shared/asv_allow_blankable.rb +28 -0
- data/lib/active_storage_validations/matchers/shared/asv_attachable.rb +72 -0
- data/lib/active_storage_validations/matchers/shared/asv_contextable.rb +49 -0
- data/lib/active_storage_validations/matchers/shared/asv_messageable.rb +28 -0
- data/lib/active_storage_validations/matchers/shared/asv_rspecable.rb +27 -0
- data/lib/active_storage_validations/matchers/shared/asv_validatable.rb +56 -0
- data/lib/active_storage_validations/matchers/size_validator_matcher.rb +17 -71
- data/lib/active_storage_validations/matchers/total_size_validator_matcher.rb +47 -0
- data/lib/active_storage_validations/matchers.rb +11 -16
- data/lib/active_storage_validations/processable_file_validator.rb +37 -0
- data/lib/active_storage_validations/railtie.rb +11 -0
- data/lib/active_storage_validations/shared/asv_active_storageable.rb +30 -0
- data/lib/active_storage_validations/shared/asv_analyzable.rb +80 -0
- data/lib/active_storage_validations/shared/asv_attachable.rb +204 -0
- data/lib/active_storage_validations/shared/asv_errorable.rb +40 -0
- data/lib/active_storage_validations/shared/asv_loggable.rb +11 -0
- data/lib/active_storage_validations/shared/asv_optionable.rb +29 -0
- data/lib/active_storage_validations/shared/asv_symbolizable.rb +14 -0
- data/lib/active_storage_validations/size_validator.rb +24 -40
- data/lib/active_storage_validations/total_size_validator.rb +51 -0
- data/lib/active_storage_validations/version.rb +1 -1
- data/lib/active_storage_validations.rb +20 -6
- metadata +127 -21
- data/lib/active_storage_validations/metadata.rb +0 -123
@@ -1,123 +0,0 @@
|
|
1
|
-
module ActiveStorageValidations
|
2
|
-
class Metadata
|
3
|
-
attr_reader :file
|
4
|
-
|
5
|
-
def initialize(file)
|
6
|
-
require_image_processor
|
7
|
-
@file = file
|
8
|
-
end
|
9
|
-
|
10
|
-
def image_processor
|
11
|
-
Rails.application.config.active_storage.variant_processor
|
12
|
-
end
|
13
|
-
|
14
|
-
def require_image_processor
|
15
|
-
if image_processor == :vips
|
16
|
-
require 'vips' unless defined?(Vips)
|
17
|
-
else
|
18
|
-
require 'mini_magick' unless defined?(MiniMagick)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def metadata
|
23
|
-
read_image do |image|
|
24
|
-
if rotated_image?(image)
|
25
|
-
{ width: image.height, height: image.width }
|
26
|
-
else
|
27
|
-
{ width: image.width, height: image.height }
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def read_image
|
35
|
-
is_string = file.is_a?(String)
|
36
|
-
if is_string || file.is_a?(ActiveStorage::Blob)
|
37
|
-
blob =
|
38
|
-
if is_string
|
39
|
-
if Rails.gem_version < Gem::Version.new('6.1.0')
|
40
|
-
ActiveStorage::Blob.find_signed(file)
|
41
|
-
else
|
42
|
-
ActiveStorage::Blob.find_signed!(file)
|
43
|
-
end
|
44
|
-
else
|
45
|
-
file
|
46
|
-
end
|
47
|
-
|
48
|
-
tempfile = Tempfile.new(["ActiveStorage-#{blob.id}-", blob.filename.extension_with_delimiter])
|
49
|
-
tempfile.binmode
|
50
|
-
|
51
|
-
blob.download do |chunk|
|
52
|
-
tempfile.write(chunk)
|
53
|
-
end
|
54
|
-
|
55
|
-
tempfile.flush
|
56
|
-
tempfile.rewind
|
57
|
-
|
58
|
-
image = if image_processor == :vips && Vips::get_suffixes.include?(File.extname(tempfile.path))
|
59
|
-
Vips::Image.new_from_file(tempfile.path)
|
60
|
-
else
|
61
|
-
MiniMagick::Image.new(tempfile.path)
|
62
|
-
end
|
63
|
-
else
|
64
|
-
image = if image_processor == :vips && Vips::get_suffixes.include?(File.extname(read_file_path))
|
65
|
-
Vips::Image.new_from_file(read_file_path)
|
66
|
-
else
|
67
|
-
MiniMagick::Image.new(read_file_path)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
if image && valid_image?(image)
|
72
|
-
yield image
|
73
|
-
else
|
74
|
-
logger.info "Skipping image analysis because ImageMagick or Vips doesn't support the file"
|
75
|
-
{}
|
76
|
-
end
|
77
|
-
rescue LoadError, NameError
|
78
|
-
logger.info "Skipping image analysis because the mini_magick or ruby-vips gem isn't installed"
|
79
|
-
{}
|
80
|
-
rescue MiniMagick::Error => error
|
81
|
-
logger.error "Skipping image analysis due to an ImageMagick error: #{error.message}"
|
82
|
-
{}
|
83
|
-
rescue Vips::Error => error
|
84
|
-
logger.error "Skipping image analysis due to a Vips error: #{error.message}"
|
85
|
-
{}
|
86
|
-
ensure
|
87
|
-
image = nil
|
88
|
-
end
|
89
|
-
|
90
|
-
def valid_image?(image)
|
91
|
-
image_processor == :vips ? image.avg : image.valid?
|
92
|
-
rescue Vips::Error
|
93
|
-
false
|
94
|
-
end
|
95
|
-
|
96
|
-
def rotated_image?(image)
|
97
|
-
if image_processor == :vips
|
98
|
-
image.get('exif-ifd0-Orientation').include?('Right-top') ||
|
99
|
-
image.get('exif-ifd0-Orientation').include?('Left-bottom')
|
100
|
-
else
|
101
|
-
%w[ RightTop LeftBottom ].include?(image["%[orientation]"])
|
102
|
-
end
|
103
|
-
rescue Vips::Error # field "exif-ifd0-Orientation" not found
|
104
|
-
false
|
105
|
-
end
|
106
|
-
|
107
|
-
def read_file_path
|
108
|
-
case file
|
109
|
-
when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile
|
110
|
-
file.path
|
111
|
-
when Hash
|
112
|
-
File.open(file.fetch(:io)).path
|
113
|
-
else
|
114
|
-
raise "Something wrong with params."
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
def logger
|
119
|
-
Rails.logger
|
120
|
-
end
|
121
|
-
|
122
|
-
end
|
123
|
-
end
|