heroku with carrierwave dropbox storage and rails showing always same image

heroku with carrierwave dropbox storage and rails showing always same image

所以我有一个在开发中运行良好的 Web 应用程序,并且使用 carrierwave 和 imagemagick 我对需要上传的照片进行了一些更改。问题是,当我在 heroku 上请求照片的主要版本时,它仍然给我拇指版本

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

这是我的上传器

然后在我看来,我有 image_tag(model.banner_url(:thumb) 获得缩略图版本和 image_tag @model.banner_url 获得大版本。问题是我本地机器上的第二个运行得很好,但是在 heroku 上它给了我与第一个相同的图像。它确实创建了正确的文件夹和文件,并且确实正确地裁剪了它们,但它没有检索到正确的文件夹和文件。我正在使用

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

作为 heroku 存储,显然有一个 dropbox 帐户

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*

我认为问题在于您定义 store_dir 方法的方式。

这样定义:

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

而且 CarrierWave 文档也建议以这种方式使用上传器:

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

来源:https://github.com/carrierwaveuploader/carrierwave