将所有文件下载到Heroku中的zip文件中



我一直在用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

我也写了我的视图和路由,所以它可能会服务于任何人试图实现下载所有的功能

routes.rb

resources :users do
 post 'download_all'
end

我的观点

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

问题似乎与搜索散列和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

最新更新