0
我一直在用Heroku敲我的腦袋,同時試圖用我所有的收據文件數據下載一個zip文件。在Heroku下載所有文件到一個zip文件
的文件存儲在Amazon S3和它所有的作品在我的開發機上很好..
我認爲它與將它視爲做的,放棄了以前的解決方案,因爲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 %>