New Discussion

Notifications

You’re not receiving notifications from this thread.

Custom Image Analyzer

2
Rails

I have a custom ActiveStorage analyzer that (will) exact a bit more metadata out of an image and save it in ActiveStorage (i.e.: camera, shutter, etc...) For simplicity sake, the metadata method has a basic hash. Here's what I have:

  • I add ExifAnalyzer to lib/analyzers/exif_analyzer.rb
  • Next, created file called active_storage_analyzers.rb in /config with Rails.application.config.active_storage.Analyzers.prepend ExifAnalyzer
  • In my application.rb I make sure the class is loaded config.autoload_paths += %W[#{config.root}/lib]
  • Restarted the app
  • I receive this error on startup. Any ideas would be welcome. uninitialized constant ExifAnalyzer (NameError) Rails.application.config.active_storage.Analyzers.prepend ExifAnalyzer Might be helpful. -- Rails 7.0.3, Ruby 3.1.2, Vips

ExifAnalyzer

lib/analyzers/exif_analyzer.rb

class ExifAnalyzer < ActiveStorage::Analyzer
  require 'exifr/jpeg'

  def self.accept?(blob)
    ['image/jpg', 'image/jpeg'].include? blob.content_type
  end

  def metadata
    { test: true }
  end
end

Hello,
The problem could be autoloading because config files loads before the actual code
You could use something like this in application.rb

    config.after_initialize do
      config.active_storage.analyzers.prepend ExifAnalyzer
    end

your analyzer is helpful with me, i can save many time with your algorithm

Join the discussion
Create an account Log in

Learning Ruby on Rails? Join our newsletter.

We won't send you spam. Unsubscribe at any time.