繁体   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