Carrierwave 不会在模型更新后重新创建版本

Carrierwave doesn't recreate versions after the model update

我在 Carrierwave Uploader 上推出了一个新版本。当我创建一个新的 Event 时,它会正确创建两个版本。但是当我更新它时,只有我附加的文件会被上传,但不会重新创建版本。

我正在使用 CarrierWave 1.2.2,并查看更新日志,它似乎不是在较新版本中得到修复的错误

class CoverUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  if Rails.env.development? || Rails.env.test?
    storage :file
  elsif Rails.env.production?
    storage :fog
  end

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    if ENV['HEROKU_APP_NAME'].to_s.include?('-pr-')
      "review_apps/#{model.class.to_s.underscore}/#{model.id}"
    else
      "#{Rails.env}/#{model.class.to_s.underscore}/#{model.id}"
    end
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  def default_url(*args)
    ActionController::Base.helpers.asset_path('test.jpg')
  end

  # Create different versions of your uploaded files:
  version :optimised do
    process convert: 'webp'
    process :set_content_type_to_webp

    def full_filename(_for_file = model.cover.file)
      "cover_#{model.id}.webp"
    end

    def exists?
      file&.exists?
    end
  end

  def extension_blacklist
    %w(webp)
  end

  private

  # Required to actually force Amazon S3 to treat it like an image
  def set_content_type_to_webp
    file.instance_variable_set(:@content_type, 'image/webp')
  end
end

您需要向 Event 添加一个 after_save 回调,然后在您安装的上传器上调用 recreate_versions!

假设您有一个具有以下内容的事件模型,这将解决您的问题。

class Event < ApplicationRecord
  mount_uploader :cover_image, CoverUploader
  after_save :recreate_versions!
  delegate :recreate_versions!, to: :cover_image, allow_nil: true
end

另见 CarrierWave's README

@ogelacinyc 在 full_filename 中发现错误时部分正确。我回去测试正常功能,创建另一个版本,并进行简单的尺寸更改。然后我可以看到更新会自行重新创建版本,就像我预期的那样。

这让我觉得我的 version :optimised 块可能有问题。于是一一评论后,发现full_filename才是罪魁祸首。它可能 model.cover.file 默默地失败了,但我认为它是 model.id,正如在 filename method in Carrierwave

的描述中所见

因此,我直接获取文件名,提取扩展名并将其替换为 webp:

  def full_filename(for_file = model.file_name.file)
    extension = File.extname(for_file)
    "cover_#{for_file.sub(extension, '.webp')}"
  end

哪个没有问题!