Class: ActiveStorageValidations::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/active_storage_validations/metadata.rb

Defined Under Namespace

Classes: InvalidImageError

Constant Summary collapse

DEFAULT_IMAGE_PROCESSOR =
:mini_magick.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Metadata

Returns a new instance of Metadata.



9
10
11
12
# File 'lib/active_storage_validations/metadata.rb', line 9

def initialize(file)
  require_image_processor
  @file = file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



5
6
7
# File 'lib/active_storage_validations/metadata.rb', line 5

def file
  @file
end

Instance Method Details

#metadataObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_storage_validations/metadata.rb', line 21

def 
  read_image do |image|
    if rotated_image?(image)
      { width: image.height, height: image.width }
    else
      { width: image.width, height: image.height }
    end
  end
rescue InvalidImageError
  logger.info "Skipping image analysis because ImageMagick or Vips doesn't support the file"
  {}
end

#valid?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
# File 'lib/active_storage_validations/metadata.rb', line 14

def valid?
  read_image
  true
rescue InvalidImageError
  false
end