Both the app and db (mongodb) servers were rebooted last night. All carrierwave mounted uploaders are returning the default images for avatars, even though the files still exist.
I am using fog storage on Rackspace CDN. Each user model contains a field of avatar_filename. I tried running user.avatar.recreate_versions! however that errors out due to nil.
Is there any way to restore my images (they still exist!) and prevent this from happening again? I have searched around but it doesn't look like this is a common prom.
In my user model:
# Avatar
mount_uploader :avatar, AvatarUploader
AvatarUploader:
class AvatarUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick
  storage :fog
  def default_url
    "/assets/users/profile-default_#{version_name}.png"
  end
  # Large
  version :large do
    resize_to_limit(600, 600)
  end
  # Small
  version :small do
    process :crop
    resize_to_fill(140, 140)
  end
  # Thumbnail
  version :thumb, :from_version => :small do
    resize_to_fill(35, 35)
  end
  def extension_white_list
    %w(jpg jpeg png)
  end
  def filename
    if @filename_created
      @filename_created
    elsif original_filename
      @name ||= Digest::MD5.hexdigest(File.dirname(current_path))
      @filename_created = "a_#{timestamp}_#{@name}.#{file.extension}"
      @filename_created
    end
  end
  def timestamp
    var = :"@#{mounted_as}_timestamp"
    model.instance_variable_get(var) or model.instance_variable_set(var, Time.now.to_i)
  end
  def crop
    if model.crop_x.present?
      resize_to_limit(600, 600)
      manipulate! do |img|
        x = model.crop_x.to_i
        y = model.crop_y.to_i
        w = model.crop_w.to_i
        h = model.crop_h.to_i
        img.crop!(x, y, w, h)
      end
    end
  end
end
Given that the images are there, you could reupload them as remote files with user.remote_avatar_url = "the url for this avatar"
To avoid this in the future you have to keep in mind how you are processing the file name. That process is reapplied each time you do recreate_versions!. Put this code in your uploader to get around this:
class AvatarUploader < CarrierWave::Uploader::Base
  def filename
    if original_filename
      if model && model.read_attribute(:avatar).present?
        model.read_attribute(:avatar)
      else
        # new filename
      end
    end
  end
end
You can find more information about this in the following wiki article: https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Create-random-and-unique-filenames-for-all-versioned-files
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With