Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveStorage hook after analyze

I have pictures attached to a model. Those pictures are analyzed and the EXIF data is saved as metadata on the ActiveStorage::Blob.

class Foo < ApplicationRecord
  has_one_attached :picture
end

There is an attribute on this model that I use for sorting the instances called order_date. This attribute has to be updated with the EXIF time after the blob got analyzed.

Using paperclip, a before_commit callback method was sufficient. With ActiveStorage, I also tried before_save and after_touch but both are not working.

How can I run code right after the ActiveStorage::AnalyzeJob has run successfully?

(I want to avoid monkey-patching ActiveStorage::AnalyzeJob, because it is also performed for other attachments.)

Thanks very much for your help!

like image 712
nning Avatar asked Nov 01 '25 13:11

nning


1 Answers

I couldn't find anything official. I ended up overriding the analyze job since it is very simple anyways. It looks like below.

#/app/jobs/active_storage/analyze_job.rb

class ActiveStorage::AnalyzeJob < ActiveStorage::BaseJob
  def perform(blob)
    blob.analyze
    blob.attachments.includes(:record).each do |attachment|
      if attachment.record_type == 'Content'
        record = attachment.record
        record.set_file_info
        record.save!
      end
    end
  end
end
like image 145
user2371713 Avatar answered Nov 04 '25 05:11

user2371713



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!