0

我一直在用 Heroku 敲我的头,同时试图下载一个包含我所有收据文件数据的 zip 文件。

这些文件存储在 amazon s3 上,在我的开发机器上一切正常。

我认为它与 Tempfile 有关,并放弃了以前的解决方案,因为 heroku 对其文件系统有一些严格的政策,所以我使用了 tmp 文件夹,但问题似乎并不存在。我已经尝试直接从 s3(使用 openUri)加载到 zip 文件,但它似乎在 Heroku 上也不起作用。

我的 Heroku 代码没有将文件加载到 zip 可能有什么问题?

这是我的模型方法:

def zip_receipts(search_hash=nil)

  require 'zip/zip'
  require 'zip/zipfilesystem'

  t=File.open("#{Rails.root}/tmp/#{Digest::MD5.hexdigest(rand(12).to_s)}_#{Process.pid}",'w')

 # t = Tempfile.new(Digest::MD5.hexdigest(rand(12).to_s))     

  # Give the path of the temp file to the zip outputstream, it won't try to open it as an archive.
  Zip::ZipOutputStream.open(t.path) do |zos|
   logger.debug("search hash Zip: #{search_hash.inspect}")
     self.feed(search_hash).each do |receipt|
      begin
        require 'open-uri'
        require 'tempfile'

        #configures filename
        filen = File.basename(receipt.receipt_file_file_name)
        ext= File.extname(filen)
        filen_noext = File.basename(receipt.receipt_file_file_name, '.*')
        filen=filen_noext+SecureRandom.hex(10)+ext
        logger.info("Info Zip - Filename: #{filen}")
        # Create a new entry on the zip file
        zos.put_next_entry(filen)
     #    logger.info("Info Zip - Added entry: #{zos.inspect}")
        # Add the contents of the file, reading directly from amazon
        tfilepath= "#{Rails.root}/tmp/#{File.basename(filen,ext)}_#{Process.pid}"

      open(tfilepath,"wb") do |file|
        file << open(receipt.authenticated_url(:original),:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read
      end

        zos.print IO.binread tfilepath
      #   logger.info("Info Zip - Extracted from amazon: #{zos.inspect}")
        rescue Exception => e
          logger.info("exception #{e}")
        end  # closes the exception begin
     end #closes receipts cycle
  end #closes zip file stream cycle
  # The temp file will be deleted some time...
   t.close

  #returns the path for send file controller to act
   t.path

  end

我的控制器:

 def download_all
    @user = User.find_by_id(params[:user_id])

    filepath = @user.zip_receipts
    # Send it using the right mime type, with a download window and some nice file name.
    send_file(filepath,type: 'application/zip', disposition: 'attachment',filename:"MyReceipts.zip")
  end

我还写了我的视图和路线,以便它可以为任何其他试图实现下载所有功能的人提供服务

路线.rb

resources :users do
 post 'download_all'
end

我的观点

<%= link_to "Download receipts", user_download_all_path(user_id:user.id), method: :post %>
4

1 回答 1

1

问题似乎出在搜索哈希和 sql 查询上,而不是代码本身。出于某种原因,收据会被列出,但不会被下载。所以这是一个完全不同的问题

最后我有这个模型的代码

 def zip_receipts(search_hash=nil)

  require 'zip/zip'
  require 'zip/zipfilesystem'

  t=File.open("#{Rails.root}/tmp/MyReceipts.zip_#{Process.pid}","w")

 # t = Tempfile.new(Digest::MD5.hexdigest(rand(12).to_s))
  #"#{Rails.root}/tmp/RecibosOnline#{SecureRandom.hex(10)}.zip"

 puts "Zip- Receipts About to enter"
  # Give the path of the temp file to the zip outputstream, it won't try to open it as an archive.
  Zip::ZipOutputStream.open(t.path) do |zos|
     self.feed(search_hash).each do |receipt|
      begin
        require 'open-uri'
        require 'tempfile'

        filen = File.basename(receipt.receipt_file_file_name)
        ext= File.extname(filen)
        filen_noext = File.basename(receipt.receipt_file_file_name, '.*')
        filen=filen_noext+SecureRandom.hex(10)+ext
      #  puts "Info Zip - Filename: #{filen}"
        # Create a new entry on the zip file
        zos.put_next_entry(filen)
        zos.print open(receipt.authenticated_url(:original),:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read
        rescue Exception => e
          puts "exception #{e}"
        end  # closes the exception begin
     end #closes receipts cycle
  end #closes zip file stream cycle
  # The temp file will be deleted some time...
   t.close

  #returns the path for send file controller to act
   t.path

  end
于 2013-03-26T18:43:09.790 回答