Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 30527a5ce6 | |||
| 2e428b2500 |
23
app.py
23
app.py
@@ -673,12 +673,33 @@ 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 '',
|
||||||
|
'original_pdf_url': f'/api/original-pdf/{translation.id}' if translation.upload_path else None,
|
||||||
'translated': translated_content,
|
'translated': translated_content,
|
||||||
'pages': translated_pages
|
'pages': translated_pages
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/api/original-pdf/<int:translation_id>')
|
||||||
|
def get_original_pdf(translation_id):
|
||||||
|
"""获取原始PDF文件"""
|
||||||
|
user = get_current_user()
|
||||||
|
if not user:
|
||||||
|
return jsonify({'error': '请登录后使用此功能'}), 401
|
||||||
|
|
||||||
|
translation = Translation.query.get(translation_id)
|
||||||
|
if not translation or (translation.user_id != user.id and user.user_type != 'admin'):
|
||||||
|
return jsonify({'error': '无权访问'}), 403
|
||||||
|
|
||||||
|
if not translation.upload_path or not os.path.exists(translation.upload_path):
|
||||||
|
return jsonify({'error': '原PDF文件不存在'}), 404
|
||||||
|
|
||||||
|
return send_file(translation.upload_path,
|
||||||
|
mimetype='application/pdf',
|
||||||
|
as_attachment=False,
|
||||||
|
download_name=translation.original_filename)
|
||||||
|
|
||||||
|
|
||||||
# ==================== 路由: 用户系统 ====================
|
# ==================== 路由: 用户系统 ====================
|
||||||
@app.route('/login', methods=['GET', 'POST'])
|
@app.route('/login', methods=['GET', 'POST'])
|
||||||
def login():
|
def login():
|
||||||
|
|||||||
67
scripts/fix_upload_path.py
Normal file
67
scripts/fix_upload_path.py
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
修复旧数据的 upload_path 字段
|
||||||
|
通过 file_hash 匹配 uploads 目录中的 PDF 文件
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sqlite3
|
||||||
|
import os
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
DB_PATH = 'instance/pdf_translate.db'
|
||||||
|
UPLOADS_DIR = 'uploads'
|
||||||
|
|
||||||
|
def compute_file_hash(filepath):
|
||||||
|
"""计算文件MD5"""
|
||||||
|
hasher = hashlib.md5()
|
||||||
|
with open(filepath, 'rb') as f:
|
||||||
|
hasher.update(f.read())
|
||||||
|
return hasher.hexdigest()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
conn = sqlite3.connect(DB_PATH)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
# 获取所有 upload_path 为空的翻译记录
|
||||||
|
cursor.execute('SELECT id, file_hash, original_filename, output_path FROM translations WHERE upload_path IS NULL')
|
||||||
|
records = cursor.fetchall()
|
||||||
|
|
||||||
|
print(f"找到 {len(records)} 条需要修复的记录")
|
||||||
|
|
||||||
|
if not records:
|
||||||
|
print("无需修复")
|
||||||
|
conn.close()
|
||||||
|
return
|
||||||
|
|
||||||
|
# 找所有上传的PDF文件
|
||||||
|
pdf_files = []
|
||||||
|
for root, dirs, files in os.walk(UPLOADS_DIR):
|
||||||
|
for f in files:
|
||||||
|
if f.endswith('.pdf'):
|
||||||
|
pdf_files.append(os.path.join(root, f))
|
||||||
|
|
||||||
|
print(f"找到 {len(pdf_files)} 个PDF文件")
|
||||||
|
|
||||||
|
# 按hash匹配
|
||||||
|
fixed_count = 0
|
||||||
|
for record in records:
|
||||||
|
id, file_hash, filename, output_path = record
|
||||||
|
|
||||||
|
# 找匹配hash的文件
|
||||||
|
for pdf_path in pdf_files:
|
||||||
|
try:
|
||||||
|
pdf_hash = compute_file_hash(pdf_path)
|
||||||
|
if pdf_hash == file_hash:
|
||||||
|
print(f"ID {id}: 找到匹配 {pdf_path}")
|
||||||
|
cursor.execute('UPDATE translations SET upload_path = ? WHERE id = ?', (pdf_path, id))
|
||||||
|
conn.commit()
|
||||||
|
fixed_count += 1
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
print(f"处理 {pdf_path} 失败: {e}")
|
||||||
|
|
||||||
|
conn.close()
|
||||||
|
print(f"修复完成,共修复 {fixed_count} 条记录")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
@@ -84,11 +84,21 @@
|
|||||||
const response = await fetch(`/api/compare/${translationId}`);
|
const response = await fetch(`/api/compare/${translationId}`);
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
|
|
||||||
|
// 原文面板:如果有PDF URL用iframe显示,否则显示提取的文本
|
||||||
|
let originalHtml = '';
|
||||||
|
if (result.original_pdf_url) {
|
||||||
|
originalHtml = `<iframe src="${result.original_pdf_url}" style="width:100%;height:500px;border:none;"></iframe>`;
|
||||||
|
} else if (result.original && result.original.length > 0) {
|
||||||
|
originalHtml = `<div style="white-space:pre-wrap;font-family:monospace;">${escapeHtml(result.original)}</div>`;
|
||||||
|
} else {
|
||||||
|
originalHtml = '<div class="text-muted">原文内容未找到(可能PDF已被删除)</div>';
|
||||||
|
}
|
||||||
|
|
||||||
document.getElementById('resultContent').innerHTML = `
|
document.getElementById('resultContent').innerHTML = `
|
||||||
<div class="compare-container">
|
<div class="compare-container">
|
||||||
<div class="compare-panel original">
|
<div class="compare-panel original">
|
||||||
<h5>原文</h5>
|
<h5>原文 PDF</h5>
|
||||||
<div>${escapeHtml(result.original)}</div>
|
${originalHtml}
|
||||||
</div>
|
</div>
|
||||||
<div class="compare-panel translated">
|
<div class="compare-panel translated">
|
||||||
<h5>译文</h5>
|
<h5>译文</h5>
|
||||||
@@ -98,7 +108,7 @@
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert('加载对比失败');
|
alert('加载对比失败: ' + error.message);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
loadResult();
|
loadResult();
|
||||||
|
|||||||
Reference in New Issue
Block a user