2

I keep getting

ActiveStorage::FileNotFoundError (ActiveStorage::FileNotFoundError):
app/models/service.rb:24:in `process_images'

When trying to load an image into ImageProcessing::MiniMagick using ImageProcessing::MiniMagick.source(self.image.download)

I have also tried

ImageProcessing::MiniMagick.source(self.image) ImageProcessing::MiniMagick.source(self.image.attachment) ImageProcessing::MiniMagick.source(self.image.attachment.download) ImageProcessing::MiniMagick.source(self.image.blob)

ImageProcessing doesn't like those as its looking for a url so I tried using a url as well like this url = Rails.application.routes.url_helpers.rails_blob_path(self.image, only_path: true) and passing that in instead of self.image When I do that I get the error

failed with error:
convert: unable to open image `rails/active_storage/blobs/foo...`

Can someone please steer me in the right direction.

here is the relevant part of my Model as it looks now.

class Service < ApplicationRecord
  has_one_attached :image, dependent: :destroy
  has_one_attached :featured_image, dependent: :destroy

  after_create :process_images
  after_save :process_images

  private  
  def process_images 
    url = Rails.application.routes.url_helpers.rails_blob_path(self.image, only_path: true)
    pipeline1 = ImageProcessing::MiniMagick.source(url)
    #can't continue until I load an image
  end
end

I have also called the method from the controller. I made the model method not private and called it and it solved the nil attachment issue. The following but then I get this error. ArgumentError (string contains null byte): flagged at this line. pipeline1.resize_to_limit(400,400).convert("jpg").call

All I'm trying to do is resize images when the instance is created or updated and then save the processed image as the main attachment.

3
  • 1
    Can you try this? ImageProcessing::MiniMagick.source(ActiveStorage::Blob.service.send(:path_for, self.image.key)) Aug 2, 2020 at 4:54
  • Patricio thanks that worked. If you're available can you please explain why this method of passing an image source works and none of the other methods did?
    – yoshinator
    Aug 2, 2020 at 14:26
  • I wrote an answer explaining that. Aug 2, 2020 at 14:44

2 Answers 2

1

According to the documentation "The source object needs to responds to .path, or be a String, a Pathname, or a Vips::Image/MiniMagick::Tool object", none of the arguments you provided met the requirements.

My approach was to get the full path of your ActiveStorage file, for this we can use ActiveStorage::Blob.service.send(:path_for, <attachment>), and then use this as an argument in the ImageProcessing::MiniMagick.source method.

That's why the following works:

ImageProcessing::MiniMagick.source(ActiveStorage::Blob.service.send(:path_for, self.image.key))
2
  • On Rails 6.0.3.3 I get this error: NoMethodError (undefined method `path_for' for #<ActiveStorage::Service::S3Service:0x00007ffb08017a10>):
    – Ivan Raszl
    Oct 16, 2020 at 16:27
  • 1
    @IvanRaszl Well, it seems you are using an S3 bucket, so it wouldn't work anyway. Oct 16, 2020 at 18:46
1

The issue is probably not related to MiniMagick, but to ActiveStorage. Avoid attaching attachments inside transactions and all will run smooth. This applies to migrations, seeds or callbacks.

What I finally did to avoid my logic being wrapped inside a transaction is to run the logic inside a thread:

Thread.new do
  # your logic here
end.join
1
  • 1
    This helped me as I was using a transaction to create an active storage attachment and then generating variants. Once I remove the transaction, it works. This also seems to be a Rails 7 issue. See stackoverflow.com/questions/72706012/…
    – oky_sabeni
    Jul 6, 2022 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.