From 6361806dcb5af7b0303afb2ccdf174fd4905c8d9 Mon Sep 17 00:00:00 2001 From: wzj <244142824@qq.com> Date: Sat, 14 Jun 2025 09:34:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=80=89=E6=8B=A9=E8=AF=81?= =?UTF-8?q?=E4=B9=A6=E5=90=8E=E7=BC=80=E4=B8=BA.key=E6=88=96.pem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 70 +++++++++++++++++++------------ templates/export_certificate.html | 9 +++- 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/app.py b/app.py index 81b17bf..2baddc0 100644 --- a/app.py +++ b/app.py @@ -922,39 +922,55 @@ def export_certificate_view(cert_id): } ) elif format_type == 'pem_separate': - # 创建内存中的zip文件 - memory_file = BytesIO() - - 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']}.pem", cert_content) - - # 添加私钥文件 - with open(cert['key_path'], 'r') as f: - key_content = f.read() - zf.writestr(f"{cert['common_name']}.key", key_content) - - 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)) + # 创建内存中的zip文件(.pem + .key) + return generate_separate_files_zip( + cert, + cert_ext=".pem", + zip_suffix="_pem_key" + ) + elif format_type == 'crt_separate': + # 创建内存中的zip文件(.crt + .key) + return generate_separate_files_zip( + cert, + cert_ext=".crt", + zip_suffix="_crt_key" + ) else: flash('不支持的导出格式', 'danger') return render_template('export_certificate.html', cert=cert) +def generate_separate_files_zip(cert, cert_ext, zip_suffix): + """生成包含分开文件的ZIP包通用函数""" + memory_file = BytesIO() + + 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']}{cert_ext}", cert_content) + + # 添加私钥文件 + with open(cert['key_path'], 'r') as f: + key_content = f.read() + zf.writestr(f"{cert['common_name']}.key", key_content) + + memory_file.seek(0) + + return Response( + memory_file.getvalue(), + mimetype="application/zip", + headers={ + "Content-Disposition": f"attachment; filename={cert['common_name']}{zip_suffix}.zip" + } + ) + except Exception as e: + flash(f'创建ZIP文件失败: {str(e)}', 'danger') + return redirect(url_for('export_certificate_view', cert_id=cert['id'])) + + @app.route('/download/') @login_required def download_file(filename): diff --git a/templates/export_certificate.html b/templates/export_certificate.html index 098e917..a5072ad 100644 --- a/templates/export_certificate.html +++ b/templates/export_certificate.html @@ -30,7 +30,14 @@ -
Base64编码的证书和私钥作为两个单独文件
+
Base64编码的证书(.pem)和私钥(.key)作为两个单独文件
+ +
+ + +
Base64编码的证书(.crt)和私钥(.key)作为两个单独文件