active_storage_validations icon indicating copy to clipboard operation
active_storage_validations copied to clipboard

"foobar is not a valid image" for valid image (error only in production)

Open marckohlbrugge opened this issue 1 year ago • 1 comments

I have the following validation in my model:

  validates :input_image,
    attached: true,
    content_type: ["image/png", "image/jpeg"],
    dimension: {
      width: { min: 10, max: 5000 },
      height: { min: 10, max: 5000 }
    },
    size: { less_than: 25.megabytes }

When trying to attach an image that fits these criteria, it works fine on localhost (disk storage). But when trying the same image in production (using S3), I get the error Input message is not a valid image. Unfortunately it does not specify what's not valid about it.

Any suggestions on how to debug this?

I'm using rails (7.0.4), and active_storage_validations (0.9.8).

marckohlbrugge avatar Oct 11 '22 07:10 marckohlbrugge

Hm, my semi-educated guess is: you're either a) using an asynchronous job runner in production but not locally which delays image analyzation and therefore the adding of the width and height metadata the dimension validator needs or b) missing a Vips/ImageMagick processor for the uploaded files in question.

I'd try to reproduce the error using an own "development" S3 bucket locally.

gr8bit avatar Oct 13 '22 01:10 gr8bit

a) using an asynchronous job runner in production but not locally which delays image analyzation and therefore the adding of the width and height metadata the dimension validator needs

That was it!

I have removed the dimensions validation (and kept the rest), and now it works as expected. Thank you!

I guess I can use something like fastimage in a before_validation callback to set the height and width. Perhaps also worth adding as an option to this gem, as I believe analyzing an attachment asynchronously is the default in Rails?

marckohlbrugge avatar Oct 14 '22 11:10 marckohlbrugge

There's a mechanism for this already somewhere in ActiveStorage, I remember seeing some logic like "was the picture analyzed yet? if not, do it right away." in the AS code a few months ago. I will dive back into that.

gr8bit avatar Oct 24 '22 21:10 gr8bit

There's a mechanism for this already somewhere in ActiveStorage, I remember seeing some logic like "was the picture analyzed yet? if not, do it right away." in the AS code a few months ago. I will dive back into that.

I think this gem by already do this. The problem might be my own code disabling analyzers altogether.

(So please don't spend time looking into this just for me.)

marckohlbrugge avatar Oct 26 '22 12:10 marckohlbrugge