簡體   English   中英

上傳后,載波解壓縮RAR文件

[英]Carrierwave uncompress RAR file after upload

實際上,我在尋找建議而不是純粹的編碼答案,這些建議是關於如何在上傳后如何解壓縮RAR / ZIP文件同時保持最大數據完整性的。

這是我的問題:我的應用程序的用戶正在上傳由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的路徑
  • 更新數據庫內部的引用

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

我有類似的問題: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.configure do |config|
  config.storage_engines.merge!(my_storage: 'MyStorage')
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM