From 246f3ed82d52e0e540df607a8b07958664307f5b Mon Sep 17 00:00:00 2001 From: wzj <244142824@qq.com> Date: Sat, 14 Jun 2025 09:23:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/app.py b/app.py index e65f4c8..e94d3c7 100644 --- a/app.py +++ b/app.py @@ -914,8 +914,6 @@ def export_certificate_view(cert_id): with open(cert['key_path'], 'r') as f: pem_content += f.read() - from io import StringIO - from flask import Response return Response( pem_content, mimetype="application/x-pem-file", @@ -924,32 +922,33 @@ def export_certificate_view(cert_id): } ) elif format_type == 'pem_separate': - # 创建临时zip文件包含两个单独的文件 - import zipfile - from io import BytesIO - # 创建内存中的zip文件 memory_file = BytesIO() - with zipfile.ZipFile(memory_file, 'w') as zf: - # 添加证书文件 - with open(cert['cert_path'], 'r') as f: - cert_content = f.read() - zf.writestr(f"{cert['common_name']}.crt", cert_content) - # 添加私钥文件 - with open(cert['key_path'], 'r') as f: - key_content = f.read() - zf.writestr(f"{cert['common_name']}.key", key_content) + try: + with zipfile.ZipFile(memory_file, 'w', zipfile.ZIP_DEFLATED) as zf: + # 添加证书文件 + with open(cert['cert_path'], 'r') as f: + cert_content = f.read() + zf.writestr(f"{cert['common_name']}.crt", cert_content) - memory_file.seek(0) + # 添加私钥文件 + with open(cert['key_path'], 'r') as f: + key_content = f.read() + zf.writestr(f"{cert['common_name']}.key", key_content) - return Response( - memory_file, - mimetype="application/zip", - headers={ - "Content-Disposition": f"attachment; filename={cert['common_name']}_separate.zip" - } - ) + memory_file.seek(0) + + return Response( + memory_file.getvalue(), + mimetype="application/zip", + headers={ + "Content-Disposition": f"attachment; filename={cert['common_name']}_separate.zip" + } + ) + except Exception as e: + flash(f'创建ZIP文件失败: {str(e)}', 'danger') + return redirect(url_for('export_certificate_view', cert_id=cert_id)) else: flash('不支持的导出格式', 'danger')