2013-06-20 33 views
0

我一直在搜索如何不保存原始文件在亞馬遜S3的服務器上。每次,我的代碼都會生成相同圖像的3個版本。 no_version.png,thumb.png和original.png。如何不使用霧和載波保存原始圖像

我看StackOverflow上,我已經使用這個代碼的嘗試:

after :store, :unlink_original 

    def unlink_original(file) 
    File.delete if version_name.blank? 
    end 

但是,它仍然會產生3個版本。

我也嘗試使用路徑的變化:

after :store, :unlink_original 

    def unlink_original(file) 
    File.delete path if version_name.blank? 
    end 

然而,這將產生錯誤:沒有這樣的文件或目錄- 上傳/ L4l8n.jpg

這是我ImageUploader。任何幫助是極大的讚賞。

# encoding: utf-8 

class ImageUploader < CarrierWave::Uploader::Base 
    # include CarrierWaveDirect::Uploader 

    # Include RMagick or MiniMagick support: 
    include CarrierWave::RMagick 
    # include CarrierWave::MiniMagick 

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

    # Choose what kind of storage to use for this uploader: 
    # storage :file 
    storage :fog 

    include CarrierWave::MimeTypes 
    process :set_content_type 

    after :store, :unlink_original 

    def unlink_original(file) 
    File.delete if version_name.blank? 
    end 

    # 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 

    # Provide a default URL as a default if there hasn't been a file uploaded: 
    def default_url 
    # For Rails 3.1+ asset pipeline compatibility: 
    asset_path("fallback/" + [version_name, "default_link.png"].compact.join('_')) 

    # "/images/fallback/" + [version_name, "default.png"].compact.join('_') 
    end 

    # Process files as they are uploaded: 
    # process :scale => [200, 300] 
    # 
    # def scale(width, height) 
    # # do something 
    # end 

    # Create different versions of your uploaded files: 
    version :original, :if => :is_file_upload? 
    # process :resize_to_limit => [200, 200], :if => :is_not_file_upload? 

    # Default version always saves thumbs 
    version :thumb do 
    process :resize_to_limit => [200, 200] 
    process :convert => 'png' 
    end 

    # Add a white list of extensions which are allowed to be uploaded. 
    # For images you might use something like this: 
    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 
    # "something.jpg" if original_filename 
    # end 

    protected 
    def is_file_upload? picture 
     model.remote_image_url.blank? 
    end 

    # def is_not_file_upload? picture 
    # not model.remote_image_url.blank? 
    # end 
end 
+0

也許與此相關的SO問題的圖像:http://stackoverflow.com/questions/8579425/how-can-i- make-carrierwave-not-save-the-original-file-after-versions-are-process – Arkan

+0

@Arkan嘿我嘗試了建議的解決方案,並且我剛剛爲# Dasun

回答

0

這是有點不清楚你以後。情景:用戶將上傳圖片A,您將圖片A處理成版本B和C,並且想丟棄圖片A並保留版本B和C?如果是這樣,你可以試試

class ImageUploader < CarrierWave::Uploader::Base 
    # include CarrierWaveDirect::Uploader 

    # Include RMagick or MiniMagick support: 
    include CarrierWave::RMagick 
    # include CarrierWave::MiniMagick 

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

    # Choose what kind of storage to use for this uploader: 
    # storage :file 
    storage :fog 

    process :set_content_type 
    process :resize_to_limit => [1000, 1000] 
    process :convert => 'png' 

    version :C do 
    process :resize_to_limit => [200, 200] 
    process :convert => 'png' 
    end 

在這種情況下,你將不得不存儲在原始圖像上傳兩個版本:第一個是1000xwhatever,第二是200xwhatever。原始圖像未保存。第一個版本可以直接從.remote_image訪問,而第二個版本可以從.remote_image.C訪問。合理?

+0

這是有道理的,但它不完全是我所追求的。我想知道是否有一種方法可以保持版本C. – Dasun

+0

只是將進程語句放在上傳器的頂層(不嵌套在版本中)。例如,如果你有一個沒有版本的上傳器,並且它們處理'process:resize_to_limit => [200,200]'和'process:convert =>'png'',你不會保存原始的,只是一個副本適合200x200平方。 – cgat

0

我找到了解決辦法

after :store, :remove_original_file 

    def remove_original_file(p) 
    if self.version_name.nil? 
     self.file.delete if self.file.exists? 
    end 
    end 

這將刪除從AWS

相關問題