Search code examples
ruby-on-railsherokuruby-on-rails-5dropboxcarrierwave

heroku with carrierwave dropbox storage and rails showing always same image


so I have a web application that runs fine in development, and with carrierwave and imagemagick I make some changes on the photos that I need to upload. The problem is that when on heroku when i ask for the main version of the photo it still gives me the thumb version

class BannerUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  if Rails.env.development?
    storage :file
  else
    storage :dropbox
  end

  def store_dir
    if version_name.nil?
      "uploads/#{model.class.to_s.underscore}/#{mounted_as}"
    else
      "uploads/#{model.class.to_s.underscore}/#{mounted_as}_#{version_name}"
    end
  end

  process convert: :jpg
  process :crop
  process resize_to_limit: [1200, 260]

  def crop
    if model.crop_x.present?
      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
        r = model.crop_r.to_i
        img.rotate r
        img.crop([[w, h].join('x'), [x, y].join('+')].join('+'))
      end
    end
  end

  version :thumb do
    process resize_to_fill: [640, 320]
  end

  def extension_whitelist
    %w(jpg jpeg png)
  end

  def filename
    if original_filename
      "#{secure_token}.#{file.extension}"
    elsif file
      file.filename
    end
  end

protected
  def secure_token
    var = :"@#{mounted_as}_secure_token"
    model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
  end
end

this is my uploader

then in my views i have image_tag(model.banner_url(:thumb) to get the thumb version and image_tag @model.banner_url to get the large version. The problem is that the second one on my local machine runs just fine, but when on heroku it gives me the same image of the first one. It does create the right folders and files, and it does crop them right, but it doesn't retrieve the correct one. I am using the

gem 'dropbox-sdk-v2', '~> 0.0.3'
gem 'carrierwave-dropbox', '~> 2.0'

as heroku storage, with obviously a dropbox account


Solution

  • Often you'll want to add different versions of the same file. The classic example is image thumbnails. There is built in support for this*

    I think the problem is the way you define the store_dir method.

    Define it this way:

    def store_dir
        "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end
    

    And also CarrierWave documentation recommends to use the uploader this way:

    uploader = AvatarUploader.new
    uploader.store!(my_file)                              # size: 1024x768
    
    uploader.url # => '/url/to/my_file.png'               # size: 800x800
    uploader.thumb.url # => '/url/to/thumb_my_file.png'   # size: 200x200
    

    Source: https://github.com/carrierwaveuploader/carrierwave