Ask A Question

Notifications

You’re not receiving notifications from this thread.

Custom Image Analyzer

Mountaindog asked in 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
Reply

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
Reply

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

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,400+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.