fix: PDF对比功能无法显示原文 - uploads和outputs使用同一UUID,添加upload_path字段

This commit is contained in:
2026-04-17 09:32:21 +08:00
parent 3479cbd04c
commit 9569898f33
4 changed files with 210 additions and 13 deletions

34
app.py
View File

@@ -374,16 +374,17 @@ def upload_pdf():
output_path = cache_path output_path = cache_path
else: else:
# 需要翻译 # 需要翻译
# 保存上传文件 # 保存上传文件 - 使用同一个UUID确保uploads和outputs目录关联
upload_dir = os.path.join(UPLOAD_DIR, str(uuid.uuid4())) session_uuid = str(uuid.uuid4())
upload_dir = os.path.join(UPLOAD_DIR, session_uuid)
os.makedirs(upload_dir, exist_ok=True) os.makedirs(upload_dir, exist_ok=True)
upload_path = os.path.join(upload_dir, filename) upload_path = os.path.join(upload_dir, filename)
with open(upload_path, 'wb') as f: with open(upload_path, 'wb') as f:
f.write(file_content) f.write(file_content)
# 创建输出路径 # 创建输出路径 - 使用相同的UUID
output_dir = os.path.join(OUTPUT_DIR, str(uuid.uuid4())) output_dir = os.path.join(OUTPUT_DIR, session_uuid)
os.makedirs(output_dir, exist_ok=True) os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, f"{filename}_translated.md") output_path = os.path.join(output_dir, f"{filename}_translated.md")
@@ -400,6 +401,7 @@ def upload_pdf():
translate_params=json.dumps({'instruction': instruction}) if instruction else None, translate_params=json.dumps({'instruction': instruction}) if instruction else None,
status='processing' if not from_cache else 'completed', status='processing' if not from_cache else 'completed',
progress=0 if not from_cache else 100, progress=0 if not from_cache else 100,
upload_path=upload_path if not from_cache else None, # 保存上传路径
output_path=output_path, output_path=output_path,
from_cache=from_cache from_cache=from_cache
) )
@@ -639,18 +641,24 @@ def compare_view(translation_id):
}) })
# 如果有原文内容存储,获取原文 # 如果有原文内容存储,获取原文
# 目前翻译服务没有单独存储原文需要从PDF重新提取或从缓存获取
original_content = '' original_content = ''
# 尝试从上传目录找原PDF # 优先从数据库存储的upload_path获取原PDF
upload_dir = os.path.dirname(translation.output_path.replace('outputs', 'uploads').replace('_translated.md', '')) possible_paths = []
possible_paths = [
translation.output_path.replace('outputs', 'uploads').replace('_translated.md', ''), if translation.upload_path:
os.path.join(upload_dir, translation.original_filename), possible_paths.append(translation.upload_path)
]
# 备用方案:尝试从路径推断(兼容旧数据)
upload_dir = os.path.dirname(translation.output_path.replace('outputs', 'uploads').replace('_translated.md', '')) if translation.output_path else ''
if upload_dir:
possible_paths.append(
translation.output_path.replace('outputs', 'uploads').replace('_translated.md', '') if translation.output_path else ''
)
possible_paths.append(os.path.join(upload_dir, translation.original_filename))
for pdf_path in possible_paths: for pdf_path in possible_paths:
if os.path.exists(pdf_path) and pdf_path.endswith('.pdf'): if pdf_path and os.path.exists(pdf_path) and pdf_path.endswith('.pdf'):
try: try:
from pypdf import PdfReader from pypdf import PdfReader
reader = PdfReader(pdf_path) reader = PdfReader(pdf_path)
@@ -665,7 +673,7 @@ def compare_view(translation_id):
return jsonify({ return jsonify({
'id': translation.id, 'id': translation.id,
'filename': translation.original_filename, 'filename': translation.original_filename,
'original': original_content or '原文内容从扫描版PDF提取', 'original': original_content or '原文内容未找到可能PDF已被删除或为扫描版',
'translated': translated_content, 'translated': translated_content,
'pages': translated_pages 'pages': translated_pages
}) })

187
logs/app.log Normal file
View File

@@ -0,0 +1,187 @@
* Serving Flask app 'app'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:19000
* Running on http://192.168.2.17:19000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/services.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/app.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/app.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/pdf_translate.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/pdf_translate.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/pdf_translate.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/pdf_translate.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/pdf_translate.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/pdf_translate.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/pdf_translate.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/pdf_translate.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/pdf_translate.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/pdf_translate.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/pdf_translate.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
127.0.0.1 - - [16/Apr/2026 21:50:38] "GET /api/config HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:08] "GET /admin/translations HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:08] "GET /static/img/favicon.svg HTTP/1.1" 304 -
/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/app.py:53: LegacyAPIWarning: The Query.get() method is considered legacy as of the 1.x series of SQLAlchemy and becomes a legacy construct in 2.0. The method is now available as Session.get() (deprecated since: 2.0) (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
return User.query.get(user_id)
192.168.2.10 - - [16/Apr/2026 21:58:13] "GET / HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:13] "GET /static/css/style.css HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 21:58:13] "GET /static/js/main.js HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 21:58:13] "GET /api/user/info HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:13] "GET /static/img/favicon.svg HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 21:58:16] "GET /history HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:16] "GET /static/css/style.css HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 21:58:18] "GET / HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:18] "GET /static/css/style.css HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 21:58:18] "GET /static/js/main.js HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 21:58:18] "GET /api/user/info HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:26] "POST /api/upload HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:26] "GET /api/task/17baff33-894b-4e56-8975-cc2e6359cd66 HTTP/1.1" 200 -
/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/app.py:481: LegacyAPIWarning: The Query.get() method is considered legacy as of the 1.x series of SQLAlchemy and becomes a legacy construct in 2.0. The method is now available as Session.get() (deprecated since: 2.0) (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
translation = Translation.query.get(translation_id)
192.168.2.10 - - [16/Apr/2026 21:58:26] "GET /api/status/4 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:28] "GET /api/task/17baff33-894b-4e56-8975-cc2e6359cd66 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:28] "GET /api/status/4 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:30] "GET /api/task/17baff33-894b-4e56-8975-cc2e6359cd66 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:30] "GET /api/status/4 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:32] "GET /api/task/17baff33-894b-4e56-8975-cc2e6359cd66 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:32] "GET /api/status/4 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:34] "GET /api/task/17baff33-894b-4e56-8975-cc2e6359cd66 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:34] "GET /api/status/4 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:37] "GET /api/task/17baff33-894b-4e56-8975-cc2e6359cd66 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 21:58:37] "GET /api/status/4 HTTP/1.1" 200 -
/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/app.py:519: LegacyAPIWarning: The Query.get() method is considered legacy as of the 1.x series of SQLAlchemy and becomes a legacy construct in 2.0. The method is now available as Session.get() (deprecated since: 2.0) (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
translation = Translation.query.get(translation_id)
192.168.2.10 - - [16/Apr/2026 21:58:37] "GET /api/result/4 HTTP/1.1" 200 -
127.0.0.1 - - [16/Apr/2026 22:00:01] "GET /api/health HTTP/1.1" 404 -
127.0.0.1 - - [16/Apr/2026 22:00:33] "POST /api/upload HTTP/1.1" 200 -
127.0.0.1 - - [16/Apr/2026 22:00:38] "GET /api/status/5 HTTP/1.1" 200 -
127.0.0.1 - - [16/Apr/2026 22:01:15] "GET /api/status/5 HTTP/1.1" 200 -
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/services.py', reloading
[翻译任务] 开始翻译,使用配置: https://open.bigmodel.cn/api/paas/v4
[翻译任务] 开始翻译,使用配置: https://open.bigmodel.cn/api/paas/v4
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
127.0.0.1 - - [16/Apr/2026 22:20:01] "GET /api/health HTTP/1.1" 404 -
/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/app.py:53: LegacyAPIWarning: The Query.get() method is considered legacy as of the 1.x series of SQLAlchemy and becomes a legacy construct in 2.0. The method is now available as Session.get() (deprecated since: 2.0) (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
return User.query.get(user_id)
192.168.2.10 - - [16/Apr/2026 22:24:27] "GET / HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:27] "GET /static/css/style.css HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 22:24:27] "GET /static/js/main.js HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 22:24:27] "GET /api/user/info HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:27] "GET /static/img/favicon.svg HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 22:24:31] "GET /admin/translations HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:31] "GET /static/img/favicon.svg HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 22:24:33] "POST /admin/translation/5/toggle-share HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:33] "GET /admin/translations HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:33] "GET /static/img/favicon.svg HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 22:24:34] "POST /admin/translation/4/toggle-share HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:34] "GET /admin/translations HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:34] "GET /static/img/favicon.svg HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 22:24:42] "GET /admin/translation/5 HTTP/1.1" 200 -
/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/app.py:519: LegacyAPIWarning: The Query.get() method is considered legacy as of the 1.x series of SQLAlchemy and becomes a legacy construct in 2.0. The method is now available as Session.get() (deprecated since: 2.0) (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
translation = Translation.query.get(translation_id)
192.168.2.10 - - [16/Apr/2026 22:24:42] "GET /api/result/5 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:45] "GET /admin/translations HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:46] "GET /admin/translation/4 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:47] "GET /api/result/4 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:48] "GET /admin/translations HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:52] "GET / HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:52] "GET /static/css/style.css HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 22:24:52] "GET /static/js/main.js HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 22:24:52] "GET /static/img/favicon.svg HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 22:24:52] "GET /api/user/info HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:54] "GET / HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:54] "GET /static/css/style.css HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 22:24:54] "GET /static/js/main.js HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 22:24:54] "GET /api/user/info HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:24:54] "GET /static/img/favicon.svg HTTP/1.1" 304 -
192.168.2.10 - - [16/Apr/2026 22:25:00] "POST /api/upload HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:25:00] "GET /api/task/ec82bf8c-70b3-474a-b94f-64598c81f7fa HTTP/1.1" 200 -
/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/app.py:481: LegacyAPIWarning: The Query.get() method is considered legacy as of the 1.x series of SQLAlchemy and becomes a legacy construct in 2.0. The method is now available as Session.get() (deprecated since: 2.0) (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
translation = Translation.query.get(translation_id)
192.168.2.10 - - [16/Apr/2026 22:25:00] "GET /api/status/6 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:25:02] "GET /api/task/ec82bf8c-70b3-474a-b94f-64598c81f7fa HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:25:02] "GET /api/status/6 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:25:05] "GET /api/task/ec82bf8c-70b3-474a-b94f-64598c81f7fa HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:25:05] "GET /api/status/6 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:25:07] "GET /api/task/ec82bf8c-70b3-474a-b94f-64598c81f7fa HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:25:07] "GET /api/status/6 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:25:09] "GET /api/task/ec82bf8c-70b3-474a-b94f-64598c81f7fa HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:25:09] "GET /api/status/6 HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:25:11] "GET /api/task/ec82bf8c-70b3-474a-b94f-64598c81f7fa HTTP/1.1" 200 -
192.168.2.10 - - [16/Apr/2026 22:25:11] "GET /api/status/6 HTTP/1.1" 200 -
127.0.0.1 - - [16/Apr/2026 22:40:01] "GET /api/health HTTP/1.1" 404 -
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/services.py', reloading
[翻译任务] 开始翻译,使用配置: https://open.bigmodel.cn/api/paas/v4
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/services.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/services.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/services.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/services.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
* Detected change in '/home/xian/.openclaw/workspace-coder/works/pdf-translate-web-v2/services.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 154-698-244
127.0.0.1 - - [16/Apr/2026 23:00:01] "GET /api/health HTTP/1.1" 404 -
127.0.0.1 - - [16/Apr/2026 23:03:32] "GET / HTTP/1.1" 200 -

View File

@@ -169,6 +169,7 @@ class Translation(db.Model):
error_message = db.Column(db.Text, nullable=True) error_message = db.Column(db.Text, nullable=True)
# 输出 # 输出
upload_path = db.Column(db.String(255), nullable=True) # 原始PDF文件路径
output_path = db.Column(db.String(255), nullable=True) # 翻译结果文件路径 output_path = db.Column(db.String(255), nullable=True) # 翻译结果文件路径
# 时间戳 # 时间戳

1
test_simple.txt Normal file
View File

@@ -0,0 +1 @@
This is a test PDF file. Hello World. This is a simple test document.