def download checked_file = params[:checked_file]&.split "," zip = PackageZip.new(@order_items, checked_file) zip.generate_zip do |zip_path| send_file( zip_path, filename: "iChemical-Package Document-Order-#{rand(99999)}.zip", type: "application/zip", disposition: "attachment" ) end zip.delete_temp_zip end
require 'zip' class PackageZip def initialize(order_items, checked_file) @order_items = order_items @order = order_items.last&.order @checked_file = checked_file @temp_dir = Dir.mktmpdir @zip_list = [] @zip_name_list = [] end def generate_zip(&block) chooise_file @order zip_path = File.join(@temp_dir, 'temp.zip') Zip::File.open(zip_path, Zip::File::CREATE) do |zipfile| @zip_name_list.each_with_index do |name, index| zipfile.get_output_stream(name) do |io| io.write @zip_list[index] end end end block.call zip_path ensure end def chooise_file(order) checked_file = @checked_file || order.order_items&.first&.package_documents&.first&.checked_file&.split(",") if !checked_file.nil? checked_file.each do |file| attachments = order.order_attachments.where(file_type: file) @ufile = attachments.last case file when "coa" if @ufile.nil? @order_items.each do |item| qitem = nil if item.order.type == "OfflineOrder" chemical = item.chemical certificates = item.quotation_item.certificates.pluck(:item, :specification, :result).compact qitem = item.quotation_item else chemical = item.chemical certificates = item.certificates.pluck(:item, :specification, :result).compact qitem = item end certificate_pdf = CertificatePdf.new(certificates, chemical, qitem).render certificate_pdf_name = "cctv-COA-CAS[#{chemical.cas}]-#{rand(99999)}.pdf" @zip_name_list.push certificate_pdf_name @zip_list.push certificate_pdf end else certificate_pdf_name = @ufile.attachment_filename certificate_pdf = IO.read("#{Rails.root}/tmp/uploads/store/" + @ufile.attachment_id) @zip_name_list.push certificate_pdf_name @zip_list.push certificate_pdf end when "msds" if attachments.present? attachments.zip_file do |zip_path| @zip_name_list.push "cctv-MSDS-Package-Order-#{rand(99999)}.zip" @zip_list.push IO.read zip_path end else @order_items.each do |item| msds = Msds.new(item) result, flag = msds.generate msds_file_name = 'cctv' << (flag ? '' : '-GT') << '-MSDS-' << item.order_item_no << '.pdf' msds_file = msds.render @zip_name_list.push msds_file_name @zip_list.push msds_file end end when "pi" if ProformaInvoiceRecord.updated.find_by(order_id: @order.id).present? proforma_invoice_xls_name = "cctv-Proforma Invoice-Order-#{order.uuid}-#{rand(99999)}.xls" r = ProformaInvoiceRecord.create_with_auto(@order_items, nil, nil, "modified") proforma_invoice_xls = PerformanceInvoiceExcel.new(r).render else proforma_invoice_xls_name = "cctv-Proforma Invoice-Order-#{order.uuid}-#{rand(99999)}.xls" r = ProformaInvoiceRecord.create_with_auto(@order_items, nil, nil, "original") proforma_invoice_xls = PerformanceInvoiceExcel.new(r).render end @zip_name_list.push proforma_invoice_xls_name @zip_list.push proforma_invoice_xls when "ci" if CommercialInvoiceRecord.updated.find_by(order_id: @order.id).present? invoice_xls_name = "cctv Invoice-#{order.uuid}-#{rand(99999)}.xls" r = CommercialInvoiceRecord.create_with_auto(@order_items, nil, nil, "modified") invoice_xls = CommercialInvoiceExcel.new(r).render else invoice_xls_name = "cctv Invoice-#{order.uuid}-#{rand(99999)}.xls" r = CommercialInvoiceRecord.create_with_auto(@order_items, nil, nil, "original") invoice_xls = CommercialInvoiceExcel.new(r).render end @zip_name_list.push invoice_xls_name @zip_list.push invoice_xls when "pl" if PackageListRecord.updated.find_by(order_id: @order.id).present? pack_list_xls_name = "cctv-Packing List-Order-#{order.uuid}-#{rand(99999)}.xls" r = PackageListRecord.create_with_auto(@order_items, nil, nil, "modified") pack_list_xls = PackingListExcel.new(r).render else pack_list_xls_name = "cctv-Packing List-Order-#{order.uuid}-#{rand(99999)}.xls" r = PackageListRecord.create_with_auto(@order_items, nil, nil, "original") pack_list_xls = PackingListExcel.new(r).render end @zip_name_list.push pack_list_xls_name @zip_list.push pack_list_xls when "qc_report" @order_items.each do |item| testing_records = item.testing_records if testing_records.present? testing_records.each do |file| if file.file_path.present? file_name = file.file_name file = file.file_path new_file = download_img(file, file_name) if new_file.present? @zip_name_list.push file_name @zip_list.push IO.read new_file if File.exist?(new_file) File.delete(new_file) end end end end end end else if [email protected]? file_name = @ufile.attachment_filename file = "#{Rails.root}/tmp/uploads/store/" + @ufile.attachment_id @zip_name_list.push file_name @zip_list.push IO.read file end end end end end ########下载图片 def download_img(img_url,file_name) image_temp_dir = "tmp/uploads/qc_report/" FileUtils.mkdir(image_temp_dir) unless File.exists?(image_temp_dir) begin _file_path = image_temp_dir+file_name unless File.exists?(_file_path) img_file = open(img_url) { |f| f.read } open(image_temp_dir+file_name, "wb") { |f| f.write(img_file) } end return _file_path rescue => err puts err return '' end end def delete_temp_zip Thread.new do sleep 2 FileUtils.rm_rf @temp_dir if @temp_dir end end end