repair garbage collected tempfile

This commit is contained in:
viehlieb 2023-09-28 15:43:12 +02:00
parent 2d0c163f13
commit 6c0c207d1f

View file

@ -3,20 +3,28 @@ class GroupOrderInvoicesController < ApplicationController
before_action :authenticate_finance before_action :authenticate_finance
def show def show
begin @group_order_invoice = GroupOrderInvoice.find(params[:id])
@group_order_invoice = GroupOrderInvoice.find(params[:id]) raise RecordInvalid unless FoodsoftConfig[:contact][:tax_number]
if FoodsoftConfig[:contact][:tax_number]
respond_to do |format| respond_to do |format|
format.pdf do format.pdf do
send_group_order_invoice_pdf @group_order_invoice if FoodsoftConfig[:contact][:tax_number] send_group_order_invoice_pdf @group_order_invoice if FoodsoftConfig[:contact][:tax_number]
end
end
else
raise RecordInvalid
end end
rescue ActiveRecord::RecordInvalid => error
redirect_back fallback_location: root_path, notice: 'Something went wrong', alert: I18n.t('errors.general_msg', msg: "#{error} " + I18n.t('errors.check_tax_number'))
end end
rescue ActiveRecord::RecordInvalid => e
redirect_back fallback_location: root_path, notice: 'Something went wrong', alert: I18n.t('errors.general_msg', msg: "#{e} " + I18n.t('errors.check_tax_number'))
end
def create
go = GroupOrder.find(params[:group_order])
@order = go.order
GroupOrderInvoice.find_or_create_by!(group_order_id: go.id)
respond_to do |format|
format.js
end
redirect_back fallback_location: root_path
rescue StandardError => e
redirect_back fallback_location: root_path, notice: 'Something went wrong', :alert => I18n.t('errors.general_msg', :msg => e)
end end
def destroy def destroy
@ -45,43 +53,33 @@ class GroupOrderInvoicesController < ApplicationController
end end
end end
def create
go = GroupOrder.find(params[:group_order])
@order = go.order
GroupOrderInvoice.find_or_create_by!(group_order_id: go.id)
respond_to do |format|
format.js
end
redirect_back fallback_location: root_path
rescue => error
redirect_back fallback_location: root_path, notice: 'Something went wrong', :alert => I18n.t('errors.general_msg', :msg => error)
end
def download_all def download_all
order = Order.find(params[:order_id]) order = Order.find(params[:order_id])
invoices = order.group_orders.map(&:group_order_invoice) invoices = order.group_orders.map(&:group_order_invoice)
pdf = {} pdf = {}
file_paths = []
temp_file = Tempfile.new("all_invoices_for_order_#{order.id}.zip") temp_file = Tempfile.new("all_invoices_for_order_#{order.id}.zip")
Zip::File.open(temp_file.path, Zip::File::CREATE) do |zipfile| Zip::File.open(temp_file.path, Zip::File::CREATE) do |zipfile|
invoices.each do |invoice| invoices.each do |invoice|
pdf = create_invoice_pdf(invoice) pdf = create_invoice_pdf(invoice)
invoice_file = Tempfile.new("#{pdf.filename}") file_path = File.join("tmp", pdf.filename)
File.open(invoice_file.path, 'w:ASCII-8BIT') do |file| File.open(file_path, 'w:ASCII-8BIT') do |file|
file.write(pdf.to_pdf) file.write(pdf.to_pdf)
end end
zipfile.add("#{pdf.filename}", invoice_file.path) unless zipfile.find_entry("#{pdf.filename}") file_paths << file_path
zipfile.add(pdf.filename, file_path) unless zipfile.find_entry(pdf.filename)
end end
end end
zip_data = File.read(temp_file.path) zip_data = File.read(temp_file.path)
file_paths.each do |file_path|
File.delete(file_path)
end
respond_to do |format| respond_to do |format|
format.html { format.html do
send_data(zip_data, type: 'application/zip', filename: "#{l order.ends, format: :file}-#{order.supplier.name}-#{order.id}.zip", disposition: 'attachment') send_data(zip_data, type: 'application/zip', filename: "#{l order.ends, format: :file}-#{order.supplier.name}-#{order.id}.zip", disposition: 'attachment')
} end
end end
end end
end end