carrierwaveuploader / carrierwave

Classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks
https://github.com/carrierwaveuploader/carrierwave
8.78k stars 1.66k forks source link

NoMethodError: undefined method `read' for nil:NilClass when calling du.recreate_versions!(:some_version) #2186

Closed fatuhoku closed 1 year ago

fatuhoku commented 7 years ago

Carrierwave version: 1.1.0. Code:

# document_image_uploader.rb

   ...
  version :some_version, if: :image? do
    process :resize_to_fit => [2048, 2048]
  end

  # See http://stackoverflow.com/a/30897174/590767
  def image?(new_file)
    new_file.content_type.start_with? 'image'
  end

In the console:

> du = DocumentImageUploader.new # this is a CarrierWave::Uploader::Base subclass
> du.recreate_versions!(:some_version)
NoMethodError: undefined method `read' for nil:NilClass
    from ~/.rvm/gems/ruby-2.2.7/gems/carrierwave-1.1.0/lib/carrierwave/uploader/cache.rb:81:in `sanitized_file'
    from ~/.rvm/gems/ruby-2.2.7/gems/carrierwave-1.1.0/lib/carrierwave/uploader/versions.rb:224:in `recreate_versions!'
    from (irb):5
    from ~/.rvm/gems/ruby-2.2.7/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
    from ~/.rvm/gems/ruby-2.2.7/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
    from ~/.rvm/gems/ruby-2.2.7/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
    from ~/.rvm/gems/ruby-2.2.7/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from ~/.rvm/gems/ruby-2.2.7/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:8:in `require'
    from bin/rails:8:in `<main>'
fatuhoku commented 7 years ago

Pretty stumped. Now I must find another way to recreate all the versions :S

pipo24 commented 7 years ago

@fatuhoku yeah this is quite annoying

fatuhoku commented 7 years ago

I found that this does work for some images and not for others. Guess I can skip over them?

# Find out the offending models...
Model.all.each{|m|m.try(:image).try(:recreate_versions!) rescue (puts m.id) }
fatuhoku commented 7 years ago

Found out that the document uploader exists but the size of the file stored is zero. Not sure why.

scottbullard commented 6 years ago

Anecdotally, I found that our recreate_versions! issue was because the file dimensions were too big. We've implemented carrierwave-bombshelter & carrierwave-imageoptimizer.

When the image exceeds 4096x4096 on some particular images we receive the:

NoMethodError: undefined methodread' for nil:NilClass`

Other times we receive the appropriate bombshelter error:

CarrierWave::IntegrityError Image size should be less than or equal to 4096x4096

I'm not sure why the read error is shown on some images while the integrity error is shown on others. If I re-crop images to be below 4096x4096 the image versions are processed without issue.

Sample code:

class BannerUploader < CarrierWave::Uploader::Base
  include CarrierWave::BombShelter
  include CarrierWave::MiniMagick
  include CarrierWave::ImageOptimizer
  process :optimize

  ...
  # uploader details
end

Worker: due to direct-to-S3 uploads, we utilize the remote_#{column}_url method.

class BannerWorker
  def self.perform(obj_id, key)
    # object info...
    obj.remote_banner_url = "https://s3-bucket/#{key}"
    obj.banner.recreate_versions!
  end
end
mshibuya commented 5 years ago

1879 have fixed this issue, I guess.

pepetorres1998 commented 5 years ago

I have bundled carrierwave from master (2.0.0.alpha) and stills give me the same error.

NoMethodError: undefined methodread' for nil:NilClass`

After running @fatuhoku code: Model.all.each{|m|m.try(:image).try(:recreate_versions!) rescue (puts m.id) }, it seems that the error is caused by nil pictures.