gpt4 book ai didi

ruby-on-rails - 使用carrierwave清理tmp目录

转载 作者:行者123 更新时间:2023-12-03 16:06:21 25 4
gpt4 key购买 nike

我使用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 目录?

我找到了这个帖子 here但是也看不懂,没有简单的例子解释

编辑

我试图用控制台运行这个命令
CarrierWave.clean_cached_files!

它向我输出 tmp 目录中所有文件的数组,如下所示:
["/home/medBo/projects/my_project/public/uploads/tmp/1380732930-5006-6671","/hom‌​e/medBo/projects/my_project/public/uploads/tmp/1380754280-4623-3698" ....

但是当我去看看会发生什么时,我发现/tmp 中仍然存在所有文件(未删除)

我试图在上面的链接中阅读更多内容,我发现了一个关于 的特殊注意事项CarrierWave.clean_cached_files! :

Special Considerations

This method breaks uploaders that have more than one version defined. Your first version will be saved, but afterwards the cleanup code will run and remove the tmp file that is used to generate additional versions. In that case, you are better off creating a rake task that cleans out the tmp folders periodically.



什么意思:“这种方法破坏了具有多个版本的上传者”? (因为我在上传器类“拇指和大版本”中使用了两个版本):
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

最佳答案

你有没有试过打电话

CarrierWave.clean_cached_files!

在您的代码中还是从 rails 控制台手动?它起作用了吗?如果是这样,你可以把它放在日常任务中。你可以使用类似 whenever gem 的东西.它看起来像:
every 1.day, :at => '4:30 am' do
runner "CarrierWave.clean_cached_files!"
end

关于ruby-on-rails - 使用carrierwave清理tmp目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19126504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com