|
|
@ -141,7 +141,7 @@ def load_files( |
|
|
|
batch_size=batch_size if batch_size is not None else 8, |
|
|
|
force_rebuild=force_rebuild, |
|
|
|
) |
|
|
|
return {"message": "成功加载"} |
|
|
|
return {"message": "加载完成"} |
|
|
|
except Exception as e: |
|
|
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
|
@ -385,43 +385,43 @@ def clear_messages(): |
|
|
|
def serve_file(file_path: str, download: bool = Query(False, description="Whether to download the file")): |
|
|
|
""" |
|
|
|
Serve local files for file:// URIs in generated reports. |
|
|
|
|
|
|
|
This endpoint allows accessing local files that are referenced in the |
|
|
|
generated reports. The file_path parameter should be the URL-encoded |
|
|
|
|
|
|
|
This endpoint allows accessing local files that are referenced in the |
|
|
|
generated reports. The file_path parameter should be the URL-encoded |
|
|
|
path to the file. |
|
|
|
|
|
|
|
|
|
|
|
Args: |
|
|
|
file_path (str): The URL-encoded file path |
|
|
|
|
|
|
|
|
|
|
|
Returns: |
|
|
|
HTMLResponse or PlainTextResponse: The file content displayed in browser |
|
|
|
|
|
|
|
|
|
|
|
Raises: |
|
|
|
HTTPException: If the file is not found or access is denied |
|
|
|
""" |
|
|
|
import urllib.parse |
|
|
|
import mimetypes |
|
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
# URL解码文件路径 |
|
|
|
decoded_path = urllib.parse.unquote(file_path) |
|
|
|
|
|
|
|
|
|
|
|
# 转换为Path对象 |
|
|
|
file_path_obj = Path(decoded_path) |
|
|
|
|
|
|
|
|
|
|
|
# 安全检查:确保文件路径是绝对路径 |
|
|
|
if not file_path_obj.is_absolute(): |
|
|
|
raise HTTPException(status_code=400, detail="Only absolute file paths are allowed") |
|
|
|
|
|
|
|
|
|
|
|
# 安全检查:确保文件存在 |
|
|
|
if not file_path_obj.exists(): |
|
|
|
raise HTTPException(status_code=404, detail=f"File not found: {decoded_path}") |
|
|
|
|
|
|
|
|
|
|
|
# 安全检查:确保是文件而不是目录 |
|
|
|
if not file_path_obj.is_file(): |
|
|
|
raise HTTPException(status_code=400, detail=f"Path is not a file: {decoded_path}") |
|
|
|
|
|
|
|
|
|
|
|
# 如果请求下载,直接返回文件 |
|
|
|
if download: |
|
|
|
return FileResponse( |
|
|
@ -429,24 +429,24 @@ def serve_file(file_path: str, download: bool = Query(False, description="Whethe |
|
|
|
filename=file_path_obj.name, |
|
|
|
media_type='application/octet-stream' |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
# 尝试读取文件内容 |
|
|
|
try: |
|
|
|
with open(file_path_obj, 'r', encoding='utf-8') as f: |
|
|
|
with open(file_path_obj, encoding='utf-8') as f: |
|
|
|
content = f.read() |
|
|
|
except UnicodeDecodeError: |
|
|
|
# 如果UTF-8解码失败,尝试其他编码 |
|
|
|
try: |
|
|
|
with open(file_path_obj, 'r', encoding='latin-1') as f: |
|
|
|
with open(file_path_obj, encoding='latin-1') as f: |
|
|
|
content = f.read() |
|
|
|
except Exception as e: |
|
|
|
raise HTTPException(status_code=500, detail=f"Error reading file: {str(e)}") |
|
|
|
except Exception as e: |
|
|
|
raise HTTPException(status_code=500, detail=f"Error reading file: {str(e)}") |
|
|
|
|
|
|
|
|
|
|
|
# 获取文件类型 |
|
|
|
mime_type, _ = mimetypes.guess_type(str(file_path_obj)) |
|
|
|
|
|
|
|
|
|
|
|
# 根据文件类型决定如何显示 |
|
|
|
if mime_type and mime_type.startswith('text/'): |
|
|
|
# 文本文件直接在浏览器中显示 |
|
|
@ -454,97 +454,97 @@ def serve_file(file_path: str, download: bool = Query(False, description="Whethe |
|
|
|
else: |
|
|
|
# 其他文件类型创建HTML页面显示 |
|
|
|
html_content = f""" |
|
|
|
<!DOCTYPE html> |
|
|
|
<html lang="zh-CN"> |
|
|
|
<head> |
|
|
|
<meta charset="UTF-8"> |
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
|
|
<title>文件查看器 - {file_path_obj.name}</title> |
|
|
|
<style> |
|
|
|
body {{ |
|
|
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; |
|
|
|
margin: 0; |
|
|
|
padding: 20px; |
|
|
|
background-color: #f5f5f5; |
|
|
|
}} |
|
|
|
.container {{ |
|
|
|
max-width: 1200px; |
|
|
|
margin: 0 auto; |
|
|
|
background-color: white; |
|
|
|
border-radius: 8px; |
|
|
|
box-shadow: 0 2px 10px rgba(0,0,0,0.1); |
|
|
|
overflow: hidden; |
|
|
|
}} |
|
|
|
.header {{ |
|
|
|
background-color: #2c3e50; |
|
|
|
color: white; |
|
|
|
padding: 15px 20px; |
|
|
|
display: flex; |
|
|
|
justify-content: space-between; |
|
|
|
align-items: center; |
|
|
|
}} |
|
|
|
.header h1 {{ |
|
|
|
margin: 0; |
|
|
|
font-size: 1.5em; |
|
|
|
}} |
|
|
|
.file-info {{ |
|
|
|
font-size: 0.9em; |
|
|
|
opacity: 0.8; |
|
|
|
}} |
|
|
|
.content {{ |
|
|
|
padding: 20px; |
|
|
|
}} |
|
|
|
.download-btn {{ |
|
|
|
background-color: #3498db; |
|
|
|
color: white; |
|
|
|
border: none; |
|
|
|
padding: 8px 16px; |
|
|
|
border-radius: 4px; |
|
|
|
cursor: pointer; |
|
|
|
text-decoration: none; |
|
|
|
display: inline-block; |
|
|
|
margin-left: 10px; |
|
|
|
}} |
|
|
|
.download-btn:hover {{ |
|
|
|
background-color: #2980b9; |
|
|
|
}} |
|
|
|
pre {{ |
|
|
|
background-color: #f8f9fa; |
|
|
|
border: 1px solid #e9ecef; |
|
|
|
border-radius: 4px; |
|
|
|
padding: 15px; |
|
|
|
overflow-x: auto; |
|
|
|
white-space: pre-wrap; |
|
|
|
word-wrap: break-word; |
|
|
|
font-family: 'Courier New', monospace; |
|
|
|
font-size: 14px; |
|
|
|
line-height: 1.5; |
|
|
|
}} |
|
|
|
.binary-notice {{ |
|
|
|
background-color: #fff3cd; |
|
|
|
border: 1px solid #ffeaa7; |
|
|
|
border-radius: 4px; |
|
|
|
padding: 15px; |
|
|
|
margin-bottom: 20px; |
|
|
|
color: #856404; |
|
|
|
}} |
|
|
|
</style> |
|
|
|
</head> |
|
|
|
<body> |
|
|
|
<div class="container"> |
|
|
|
<div class="header"> |
|
|
|
<div> |
|
|
|
<h1>{file_path_obj.name}</h1> |
|
|
|
<div class="file-info"> |
|
|
|
路径: {decoded_path}<br> |
|
|
|
大小: {file_path_obj.stat().st_size:,} 字节 |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
<a href="/file/{file_path}?download=true" class="download-btn">下载文件</a> |
|
|
|
</div> |
|
|
|
<div class="content"> |
|
|
|
""" |
|
|
|
|
|
|
|
<!DOCTYPE html> |
|
|
|
<html lang="zh-CN"> |
|
|
|
<head> |
|
|
|
<meta charset="UTF-8"> |
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
|
|
<title>文件查看器 - {file_path_obj.name}</title> |
|
|
|
<style> |
|
|
|
body {{ |
|
|
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; |
|
|
|
margin: 0; |
|
|
|
padding: 20px; |
|
|
|
background-color: #f5f5f5; |
|
|
|
}} |
|
|
|
.container {{ |
|
|
|
max-width: 1200px; |
|
|
|
margin: 0 auto; |
|
|
|
background-color: white; |
|
|
|
border-radius: 8px; |
|
|
|
box-shadow: 0 2px 10px rgba(0,0,0,0.1); |
|
|
|
overflow: hidden; |
|
|
|
}} |
|
|
|
.header {{ |
|
|
|
background-color: #2c3e50; |
|
|
|
color: white; |
|
|
|
padding: 15px 20px; |
|
|
|
display: flex; |
|
|
|
justify-content: space-between; |
|
|
|
align-items: center; |
|
|
|
}} |
|
|
|
.header h1 {{ |
|
|
|
margin: 0; |
|
|
|
font-size: 1.5em; |
|
|
|
}} |
|
|
|
.file-info {{ |
|
|
|
font-size: 0.9em; |
|
|
|
opacity: 0.8; |
|
|
|
}} |
|
|
|
.content {{ |
|
|
|
padding: 20px; |
|
|
|
}} |
|
|
|
.download-btn {{ |
|
|
|
background-color: #3498db; |
|
|
|
color: white; |
|
|
|
border: none; |
|
|
|
padding: 8px 16px; |
|
|
|
border-radius: 4px; |
|
|
|
cursor: pointer; |
|
|
|
text-decoration: none; |
|
|
|
display: inline-block; |
|
|
|
margin-left: 10px; |
|
|
|
}} |
|
|
|
.download-btn:hover {{ |
|
|
|
background-color: #2980b9; |
|
|
|
}} |
|
|
|
pre {{ |
|
|
|
background-color: #f8f9fa; |
|
|
|
border: 1px solid #e9ecef; |
|
|
|
border-radius: 4px; |
|
|
|
padding: 15px; |
|
|
|
overflow-x: auto; |
|
|
|
white-space: pre-wrap; |
|
|
|
word-wrap: break-word; |
|
|
|
font-family: 'Courier New', monospace; |
|
|
|
font-size: 14px; |
|
|
|
line-height: 1.5; |
|
|
|
}} |
|
|
|
.binary-notice {{ |
|
|
|
background-color: #fff3cd; |
|
|
|
border: 1px solid #ffeaa7; |
|
|
|
border-radius: 4px; |
|
|
|
padding: 15px; |
|
|
|
margin-bottom: 20px; |
|
|
|
color: #856404; |
|
|
|
}} |
|
|
|
</style> |
|
|
|
</head> |
|
|
|
<body> |
|
|
|
<div class="container"> |
|
|
|
<div class="header"> |
|
|
|
<div> |
|
|
|
<h1>{file_path_obj.name}</h1> |
|
|
|
<div class="file-info"> |
|
|
|
路径: {decoded_path}<br> |
|
|
|
大小: {file_path_obj.stat().st_size:,} 字节 |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
<a href="/file/{file_path}?download=true" class="download-btn">下载文件</a> |
|
|
|
</div> |
|
|
|
<div class="content"> |
|
|
|
""" |
|
|
|
|
|
|
|
# 检查是否为二进制文件 |
|
|
|
try: |
|
|
|
# 尝试读取前1024字节来检测是否为二进制文件 |
|
|
@ -552,39 +552,39 @@ def serve_file(file_path: str, download: bool = Query(False, description="Whethe |
|
|
|
sample = f.read(1024) |
|
|
|
# 检查是否包含null字节,这是二进制文件的特征 |
|
|
|
if b'\x00' in sample: |
|
|
|
html_content += f""" |
|
|
|
<div class="binary-notice"> |
|
|
|
<strong>注意:</strong>这是一个二进制文件,无法在浏览器中直接显示内容。 |
|
|
|
</div> |
|
|
|
""" |
|
|
|
html_content += """ |
|
|
|
<div class="binary-notice"> |
|
|
|
<strong>注意:</strong>这是一个二进制文件,无法在浏览器中直接显示内容。 |
|
|
|
</div> |
|
|
|
""" |
|
|
|
else: |
|
|
|
# 尝试以文本形式显示 |
|
|
|
try: |
|
|
|
text_content = sample.decode('utf-8') |
|
|
|
html_content += f""" |
|
|
|
<pre>{text_content}</pre> |
|
|
|
""" |
|
|
|
<pre>{text_content}</pre> |
|
|
|
""" |
|
|
|
except UnicodeDecodeError: |
|
|
|
html_content += f""" |
|
|
|
<div class="binary-notice"> |
|
|
|
<strong>注意:</strong>此文件包含非文本内容,无法在浏览器中直接显示。 |
|
|
|
</div> |
|
|
|
""" |
|
|
|
html_content += """ |
|
|
|
<div class="binary-notice"> |
|
|
|
<strong>注意:</strong>此文件包含非文本内容,无法在浏览器中直接显示。 |
|
|
|
</div> |
|
|
|
""" |
|
|
|
except Exception: |
|
|
|
html_content += f""" |
|
|
|
<div class="binary-notice"> |
|
|
|
<strong>注意:</strong>无法读取文件内容。 |
|
|
|
</div> |
|
|
|
""" |
|
|
|
|
|
|
|
html_content += """ |
|
|
|
<div class="binary-notice"> |
|
|
|
<strong>注意:</strong>无法读取文件内容。 |
|
|
|
</div> |
|
|
|
""" |
|
|
|
|
|
|
|
html_content += """ |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
</body> |
|
|
|
</html> |
|
|
|
""" |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
</body> |
|
|
|
</html> |
|
|
|
""" |
|
|
|
return HTMLResponse(content=html_content) |
|
|
|
|
|
|
|
|
|
|
|
except HTTPException: |
|
|
|
# 重新抛出HTTP异常 |
|
|
|
raise |
|
|
|