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')