2013-01-22 12 views
0

每當我使用安裝的上傳程序保存模型時,只要我保存版本就會刪除版本(並且看起來好像在正常上傳圖像時甚至不會創建版本)。請注意,過去幾天/幾周前它一直運行正常(我沒有注意到確切的時間,雖然它可能發生了移動到Rails 3.2.11,我在3.2.8之前)。保存我的ActiveRecord模型將刪除我的CarrierWave圖像版本

我的模式是這樣定義的:

class Profile < ActiveRecord::Base 
    attr_accessible :profile_picture 
    mount_uploader :profile_picture, ProfilePictureUploader 
    ... 
end 

而且我ProfilePictureUploader

class ProfilePictureUploader < BaseUploader 
    process :resize_to_fill => [248, 248] 

    version :tiny do 
    process :resize_to_fill => [34, 34] 
    def full_filename(for_file = model.photo.file) 
     "#{model.to_s.parameterize}_tiny.jpg" 
    end 
    end 

    def extension_white_list 
    %w(jpg jpeg gif png) 
    end 

    # Override the filename of the uploaded files: 
    # Avoid using model.id or version_name here, see uploader/store.rb for details. 
    def filename 
    # John Snow -> john-snow-20120913162935.jpg 
    # ACME inc. -> acme-inc-20120913162935.png 
    "#{model.to_s.parameterize}-#{Time.now.strftime("%Y%m%d%H%M%S")}#{File.extname(original_filename)}" if original_filename 
    end 
end 

最後我BaseUploader

class BaseUploader < CarrierWave::Uploader::Base 
    include CarrierWave::RMagick 

    # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: 
    include Sprockets::Helpers::RailsHelper 
    include Sprockets::Helpers::IsolatedHelper 

    # Override the directory where uploaded files will be stored. 
    def store_dir 
    "system/uploads/#{model.class.to_s.underscore}/#{model.id}" 
    end 

    # Override the filename of the uploaded files: 
    def filename 
    "#{mounted_as}#{File.extname(original_filename)}" if original_filename 
    end 
end 

我使用的文件存儲在開發和霧存儲在生產中,但是這個問題在兩種環境中都會發生,所以我們假設它是這樣的使用文件存儲。

這裏是我的典型場景:

  • 我上傳圖片的檔案,只有標準的圖像被創建(例如:mbillard-20130122102833.jpg)。
  • 我在配置文件圖片上調用recreate_versions!,標準圖像和微小版本都被創建(由於我的特定命名方案,標準被重新創建,我很好)。
  • 我保存我的模型,文件夾中的所有內容(system/uploads/profile/1/)被刪除,但文件名爲profile_picture屬性的圖像除外。

我使用carrierwave 0.8.0。我相信這與保存觸發器有關,但通過查看代碼無法解決這個問題。

(我還並行開設了GitHub issue

回答

0

我終於想通了。基本上,mount.rb中的remove_previously_stored_#{column}方法似乎刪除了我新創建的版本,因爲它們與以前的版本具有相同的名稱。

我修改了我的小版本文件名包含時間戳(這樣不會有相同的文件名前面的微型版本):

我簡化我的filename邏輯來簡單定義的文件名與時間戳:

# Override the filename of the uploaded files: 
# Avoid using model.id or version_name here, see uploader/store.rb for details. 
def filename 
    # John Snow -> john-snow-20120913162935.jpg 
    # ACME inc. -> acme-inc-20120913162935.png 

    "#{model.to_s.parameterize}-#{timestamp}#{File.extname(original_filename)}" if original_filename.present? 
end 

我的時間戳功能,無論是重建版本或上傳新文件(當original_filename存在)時從文件名中提取時間戳或產生一個新問題:

protected 

def timestamp 
    if original_filename.blank? 
    match_data = /^.+-(\d{14})\.\D{3,4}$/.match(File.basename(model.read_attribute(mounted_as))) 

    return match_data[1] if match_data 
    end 

    var = :"@#{mounted_as}_timestamp" 
    model.instance_variable_get(var) || model.instance_variable_set(var, Time.now.strftime("%Y%m%d%H%M%S")) 
end 

請注意,以前的時間戳可以存儲在我的模型的屬性中,但這對我很有用。

現在唯一讓我惱火的是,爲了在重新創建版本時刪除以前的文件,我必須保存我的模型。

model.recreate_versions! # recreates versions but leaves the previous files there 
model.save # removes the previous files 

你可以在GitHub issue上看到我的整個思維過程。

相關問題