如何從服務器目錄中制作Paperclip流程文件?

[英]How can I make Paperclip process files from a server directory?


I want to run Paperclip on all files in a directory on the server. Basically, I would like to allow users to FTP some files to my webserver, then I can manually run a rake task to have Paperclip process all of the files (resize the images, update the database, etc).

我想在服務器上的目錄中的所有文件上運行Paperclip。基本上,我想允許用戶將一些文件FTP到我的網絡服務器,然后我可以手動運行rake任務讓Paperclip處理所有文件(調整圖像大小,更新數據庫等)。

How can I do this?

我怎樣才能做到這一點?

1 个解决方案

#1


9  

I'm not sure if I understood your question - are you asking to run the rake task remotely or how to import images?

我不確定我是否理解你的問題 - 你是要求遠程運行rake任務還是如何導入圖像?

In the later case there is an answer.

在后一種情況下,有一個答案。

First you need some Model to keep the images and maybe some other data, something like this:

首先,你需要一些模型來保存圖像和其他一些數據,如下所示:

class Picture < ActiveRecord::Base
    has_attached_file :image, :styles => {
        :thumb => "100x100>",
        :big => "500x500>"
      }
end

You can create simple rake task in your lib/tasks folder (you should name the file with .rake extension)

您可以在lib / tasks文件夾中創建簡單的rake任務(您應該使用.rake擴展名命名該文件)

namespace :import do

  desc "import all images from SOURCE_DIR folder"
  task :images => :environment do
    # get all images from given folder
    Dir.glob(File.join(ENV["SOURCE_DIR"], "*")) do |file_path|
      # create new model for every picture found and save it to db
      open(file_path) do |f|
        pict = Picture.new(:name => File.basename(file_path),
                           :image => f)
        # a side affect of saving is that paperclip transformation will
        # happen
        pict.save!
      end


      # Move processed image somewhere else or just remove it. It is
      # necessary as there is a risk of "double import"
      #FileUtils.mv(file_path, "....")
      #FileUtils.rm(file_path)
    end
  end

end

Then you can call manually rake task from the console providing SOURCE_DIR parameter that will be the folder on the server (it can be real folder or mounted remote)

然后,您可以從控制台手動調用rake任務,提供SOURCE_DIR參數,該參數將是服務器上的文件夾(可以是真實文件夾或安裝的遠程)

rake import:images SOURCE_DIR=~/my_images/to/be/imported

If you are planning to run this automatically I'd recommend you to go for Resque Scheduler gem.

如果您打算自動運行,我建議您使用Resque Scheduler gem。

Update: To keep things simple I've deliberately omitted exception handling

更新:為了簡單起見,我故意省略了異常處理


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/09/27/94c6b767a72ff4e687db6235b255791d.html



 
  © 2014-2022 ITdaan.com 联系我们: