使用carrierwave清理tmp目录 [英] cleanup tmp directory with carrierwave

查看:39
本文介绍了使用carrierwave清理tmp目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用载波来上传图像,在表单中,我添加了一个隐藏字段来进行缓存,如文档中所述。

I use carrierwave for my images upload, in my form i added a hidden field for caching like is described in documentation.

= 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目录中仍然具有所有临时/缓存文件。

but the problem is after uploading images and saving record, tmp directory still having all temporary/caching files.

有没有一种方法可以清理tmp目录?

there is a way to clean up the tmp directory ?

我发现了此帖子此处,但也无法理解,也没有简单的示例说明

i found this post here but can't understand it as well, and there is no simple example explained

编辑

我尝试在控制台上运行此命令

I tried to run this command with console

CarrierWave.clean_cached_files!

它向我输出tmp目录中所有文件的数组,如下所示:

it outputs me an array of all files in tmp directory like this:

["/home/medBo/projects/my_project/public/uploads/tmp/1380732930-5006-6671","/hom‌​e/medBo/projects/my_project/public/uploads/tmp/1380754280-4623-3698" ....

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

but when i go to see what happens, i find that all files still exists in /tmp (not removed)

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

i tried to read more in the link above, i found a special considerations about CarrierWave.clean_cached_files! :


特殊注意事项

Special Considerations

此方法中断定义了多个版本的上传器。
您的第一个版本将被保存,但是之后清理代码将运行
并删除用于生成其他
版本的tmp文件。在那种情况下,最好创建一个
定期清理tmp文件夹的rake任务。

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.

这意味着: 此方法破坏了具有多个版本的上传器? (因为我在上载器类缩略图和大版本中使用了两个版本):

what means : "This method breaks uploaders that have more than one version" ? (because i use two versions in my uploader class "thumb and large versions") :

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 /目录中的文件夹是否将被删除,但该任务不起作用:

I also try to run a task to see if folders inside tmp/ directory will be removed but the task doesn't work :

task :delete_tmp_files do
   FileUtils.rm_rf Dir.glob("#{Rails.root}/public/uploads/tmp/*")
end


推荐答案

在一切正常运行时,CarrierWave会为您整理大多数tmp文件和文件夹。要捕获异常,请创建一个自定义的rake任务来清理垃圾,然后使用每时每时 gem安排该任务每天,每小时等运行。

CarrierWave will take care of tidying most of the tmp files and folders for you when everything is working properly. To catch the anomalies create a custom rake task to clean up the garbage and then use the Whenever gem to schedule this task to run every day, every hour etc.

my_custom_task。耙

my_custom_task.rake

task :delete_tmp_files do
  FileUtils.rm_rf Dir.glob("#{Rails.root}/where/you/store/your/tmp_images/*") #public/tmp/screenshots etc
  #note the asterisk which deletes folders and files whilst retaining the parent folder
end

使用 rake delete_tmp_files

Ryan Bates随时随地都在设置方面做得很好。 http://railscasts.com/episodes/164-cron-in-ruby-revised

Ryan Bates has done a great railscast on setting up whenever in rails. http://railscasts.com/episodes/164-cron-in-ruby-revised

这篇关于使用carrierwave清理tmp目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆