2015-02-10 87 views
2

實際上,我正在尋找建議,而不是純粹的編碼答案,就如何在上傳後解壓縮RAR/ZIP文件,同時保持最高數據完整率。Carrierwave在上傳後解壓縮RAR文件

這是我的問題:我的應用程序的用戶正在上傳Adobe Edge生成的文件(我們用它來製作動畫廣告),這些文件都是RAR格式。要上傳文件,這非常簡單。這裏是我的上傳者:

class MediaUploader < CarrierWave::Uploader::Base 
    storage :file 

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

    def extension_white_list 
    %w(jpg jpeg gif png rar zip) 
    end 

    def filename 
    "#{ secure_token }.#{ file.extension }" if original_filename.present? 
    end 

    protected 

    def secure_token 
    var = :"@#{ mounted_as }_secure_token" 
    model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid) 
    end 
end 

現在,在我的情況下,RAR文件實際上並不是我將要使用的。我需要的是檔案中包含的文件。這些文件通常看起來像這樣:

- edge_includes 
| 
- images 
| 
- js 
| 
| ADS_1234_988x160_edge.js 
| ADS_1234_988x160_edgeActions.js 
| ADS_1234_988x160.an 
| ADS_1234_988x160.html 

從上面的例子,我需要參考存儲在數據庫中ADS_1234_988x160.html文件。

爲此,我打算用Carrierwave回調,以:

after :store, :uncompress_and_update_reference 

def uncompress_and_update_reference(file) 
    # uncompress and update reference 
end 
  • 解壓縮檔案(可能使用rubyzip
  • 獲取路徑ADS_1234_988x160.html
  • 更新參考裏面的數據庫

有沒有更好的方法來哈ndle呢?如何處理故障或網絡錯誤?任何想法都歡迎。

回答

0

我有類似的問題:zip上傳,但不應該存儲,只有解壓縮的內容。我通過實施專用存儲解決了它。喜歡的東西:

class MyStorage 
    class App < CarrierWave::Storage::Abstract 
    def store!(file) 
     # invoked when file is moved from cache into store 
     # unzip and update db here 
    end 

    def retrieve!(identifier) 
     MyZipFile.new(identifier) 
    end 
    end 
end 

class MyZipFile 
    def initialize(identifier) 
    @identifier = identifier 
    end 

    def path 
    file.path 
    end 

    def delete 
    # delete unzipped files 
    end 

    def file 
    @file ||= zip_file 
    end 

    def zip_file 
    # create zip file somewhere in tmp dir 
    end 
end 
# in uploader file 
storage MyStorage 
storage :my_storage 

如果使用符號比它需要carrierwave配置中定義:

CarrierWave.configure do |config| 
    config.storage_engines.merge!(my_storage: 'MyStorage') 
end