()
| 91 | |
| 92 | pdf_path = os.path.join(base_dir, 'input_pdf', staged_name) |
| 93 | pdf_file.save(pdf_path) |
| 94 | write_initial_json(base_dir, staged_name, original_filename) |
| 95 | |
| 96 | return jsonify({ |
| 97 | 'status': 'success', |
| 98 | 'message': '文件上传成功', |
| 99 | 'session_id': session_id |
| 100 | }) |
| 101 | |
| 102 | except Exception as e: |
| 103 | return jsonify({'status': 'error', 'message': str(e)}) |
| 104 | |
| 105 | @app.route('/download_result/<session_id>') |
| 106 | def download_result(session_id): |
| 107 | output_folder = os.path.join('data', session_id, 'output_pdf') |
| 108 | input_folder = os.path.join('data', session_id, 'input_pdf') |
| 109 | |
| 110 | pdf_files = [f for f in os.listdir(output_folder) if f.endswith('.pdf')] |
| 111 | if not pdf_files: |
| 112 | return jsonify({'status': 'error', 'message': '未找到输出 PDF 文件'}) |
| 113 | file_path = os.path.join(output_folder, pdf_files[0]) |
| 114 | |
| 115 | book_name = "处理结果" |
| 116 | try: |
| 117 | json_files = [f for f in os.listdir(input_folder) if f.endswith('.json')] |
| 118 | if json_files: |
| 119 | json_path = os.path.join(input_folder, json_files[0]) |
| 120 | with open(json_path, 'r', encoding='utf-8') as f: |
| 121 | json_data = json.load(f) |
| 122 | extracted_name = json_data.get('book_name', '') |
| 123 | if extracted_name: |
| 124 | book_name = extracted_name |
| 125 | except Exception as e: |
| 126 | logger.error(f"读取 JSON 时出错:{e}") |
| 127 | |
| 128 | # 清理文件名中的非法字符,仅保留字母、数字、中文、下划线、连字符和空格 |
| 129 | # Windows/Linux/macOS 通用安全字符集 |
| 130 | safe_book_name = re.sub(r'[<>:"/\\|?*]', '', book_name) |
| 131 | # 去除首尾空格 |
| 132 | safe_book_name = safe_book_name.strip() |
| 133 | |
| 134 | if not safe_book_name: |
| 135 | safe_book_name = "处理结果" |
| 136 | |
| 137 | # 直接使用书名作为文件名,去掉时间和 TOC 标注 |
| 138 | download_filename = f"{safe_book_name}.pdf" |
| 139 | |
| 140 | response = send_file(file_path, as_attachment=True, download_name=download_filename) |
| 141 |
nothing calls this directly
no test coverage detected