Validation issue. Width of the uploaded file is nil (but it isn't really)

168 views
Skip to first unread message

charles...@modolabs.com

unread,
Apr 13, 2018, 1:10:51 PM4/13/18
to Shrine
Hi!

I'm using Shrine 2.10.1 with direct uploads to S3 via Uppy

I am getting the following deprecation warning when I upload image (jpg or png) files:

SHRINE DEPRECATION WARNING: Width of the uploaded file is nil, and Shrine skipped the validation. In Shrine 3 the validation will fail if width is nil.



My shrine.rb contains the following plugins:

Shrine.plugin :activerecord
Shrine.plugin :logging, logger: Rails.logger
Shrine.plugin :cached_attachment_data
Shrine.plugin :validation_helpers
Shrine.plugin :remove_attachment
Shrine.plugin :presign_endpoint
Shrine.plugin :backgrounding

My uploader class looks like this

module Uploaders
  class ImageUploader < Shrine
    include ImageProcessing::MiniMagick

    plugin :determine_mime_type
    plugin :store_dimensions, analyzer: :mini_magick
    plugin :pretty_location
    plugin :default_url
    plugin :default_url_options, store: { public: true }, cache: { public: true }

    plugin :processing
    plugin :versions

    Attacher.validate do
      validate_max_size 2.megabyte, message: 'is too large (max is 2 MB)'
      if validate_mime_type_inclusion ['image/jpg', 'image/jpeg', 'image/png']
        validate_min_width 800
      end
    end

    Attacher.promote { |data| ShrineWorkers::ShrinePromoteWorker.perform_async(data) }
    Attacher.delete { |data| ShrineWorkers::ShrineDeleteWorker.perform_async(data) }

    process(:store) do |io, context|
      if context[:action] == :store
        {
          original: io,
          scaled: ImageProcessing::MiniMagick.resize_to_limit!(io.download, 800, nil),
          icon: ImageProcessing::MiniMagick.resize_and_pad(io.download, 120, 120),
        }
      end
    end

  end
end


I am pretty sure the warning is coming from the validate_min_width validator. 

I've tried :store_dimensions with fastimage and mini_magick, the results are the same

The files upload fine, are promoted as expected, and generally behave the way I want, except the min_width validation is not being run, and the warning is showing.

What's particularly weird to me is that I have another project with a nearly identical uploader class that's working as expected with the same images with no warnings. 

Janko Marohnić

unread,
Apr 14, 2018, 4:45:54 PM4/14/18
to charles...@modolabs.com, Shrine
If you're doing direct uploads, then you need to also load the restore_cached_data plugin if you want metadata to get extracted on the server side before validations are run. By default Shrine will not do that, because it requires retrieving file content from storage, which can be expensive when dealing with large files.

Alternatively you should be able to extract dimensions on the client-side and add them to the metadata hash, I'm sure there are JavaScript libraries that can do that.

Kind regards,
Janko

--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine+unsubscribe@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/9bc91a7c-8052-4ffe-8972-2bacf091fa5a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages