Class: ActiveStorageValidations::Metadata

Inherits:
Object
  • Object
show all
Includes:
ASVLoggable
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

Methods included from ASVLoggable

#logger

Constructor Details

#initialize(attachable) ⇒ Metadata

Returns a new instance of Metadata.



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

def initialize(attachable)
  require_image_processor
  @attachable = attachable
end

Instance Attribute Details

#attachableObject (readonly)

Returns the value of attribute attachable.



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

def attachable
  @attachable
end

Instance Method Details

#metadataObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_storage_validations/metadata.rb', line 27

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)


20
21
22
23
24
25
# File 'lib/active_storage_validations/metadata.rb', line 20

def valid?
  read_image
  true
rescue InvalidImageError
  false
end