2011-05-03 23 views
7

在我的Rails應用程序,我想允許用戶通過Carrierwave無論是上傳圖片還是非圖像文件。目前,Carrierwave正在精細地處理和處理圖像文件,但不幸的是,它完全丟棄了非圖像文件。有沒有一種單一的Carrierwave上傳器處理圖像和非圖像文件的簡潔方法?隨着Carrierwave和Rails 3是可以很好地管理與同一上傳圖像和非圖像文件?

我會包括我的低於目前上傳:

class AssetUploader < CarrierWave::Uploader::Base 

    include CarrierWave::MiniMagick 

    storage :file 

    def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 

    version :thumb do 
    process :resize_to_fill => [300, 300] 
    end 

    version :icon do 
    process :resize_to_fill => [48, 48] 
    end 

    def extension_white_list 
    %w(jpg jpeg gif png pdf doc xls docx xlsx ppt) 
    end 

end 
+2

我沒有用過CarrierWave寶石,但與回形針這是非常微不足道的。查看CW的自述文件,使用它看起來比使用Paperclip更復雜。只要我的$ 0.02 – Yardboy 2011-05-12 02:45:34

+2

你說得對。我實際上最終轉回回形針,嘿。 – jklina 2011-05-12 13:48:00

回答

2

我有這個確切的問題。動態決定基於文件擴展名是否處理蹦牀/形實轉換方法:我與一多 - 電平 - 的-間接的灰白排版SCI溶液解決它。

您可以在這裏找到實現:https://gist.github.com/995663

(在版本塊引入的邏輯其實並不因爲CarrierWave DSL是如何工作的工作的簡單方法 - 邏輯需要被推遲,直到調用)

我希望幫助。

+0

這是一個開始,但不完整,因爲它仍然創建版本。處理不會發生,但您最終會得到原始文件的多個副本。 – keithepley 2011-09-23 19:29:02

1

我知道這已經回答了,但阿龍的回答並不能完全解決問題。你可以用他在https://gist.github.com/995663建議,如果你只需要調用process,但如果你想有選擇地與version處理。

對於版本,請carrierwave wiki頁面在這裏:https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Do-conditional-processing

現在可以做到這一點在你的代碼,它只會處理上的圖像版本塊

version :thumb, :if => :image? do 
    process ... 
end 

protected 

def image?(new_file) 
    new_file.content_type.include? 'image' 
end