我使用carrierwave进行图像上传,在我的表单中,我添加了一个用于缓存的隐藏字段,如文档中所述。
= form_for @user, html: {multipart: true} do |f|
%p
= f.label :image, "your image"
= f.file_field :image, id: "img"
= f.hidden_field :image_cache
但问题是在上传图像并保存记录后,tmp 目录仍然包含所有临时/缓存文件。
有没有办法清理 tmp 目录?
我在这里找到了这篇文章,但也无法理解,并且没有简单的示例解释
编辑
我试图用控制台运行这个命令
CarrierWave.clean_cached_files!
它向我输出 tmp 目录中所有文件的数组,如下所示:
["/home/medBo/projects/my_project/public/uploads/tmp/1380732930-5006-6671","/home/medBo/projects/my_project/public/uploads/tmp/1380754280-4623-3698" ....
但是当我去看看会发生什么时,我发现所有文件仍然存在于 /tmp 中(未删除)
我试图在上面的链接中阅读更多内容,我发现了有关CarrierWave.clean_cached_files 的特殊注意事项!:
特别注意事项
此方法会破坏定义了多个版本的上传者。您的第一个版本将被保存,但之后清理代码将运行并删除用于生成其他版本的 tmp 文件。在这种情况下,您最好创建一个定期清理 tmp 文件夹的 rake 任务。
什么意思:“这种方法破坏了拥有多个版本的上传者”?(因为我在我的上传器类“拇指和大版本”中使用了两个版本):
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# 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
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
...
...
version :large do
resize_to_limit(600, 600)
end
version :thumb do
process :crop_image
resize_to_fill(100, 100)
end
...
...
end
我还尝试运行一个任务,看看 tmp/ 目录中的文件夹是否会被删除,但该任务不起作用:
task :delete_tmp_files do
FileUtils.rm_rf Dir.glob("#{Rails.root}/public/uploads/tmp/*")
end