Custom Image Analyzer
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
withRails.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