All threads / Custom Image Analyzer
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

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
Join the discussion

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

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

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

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.