|
|
@ -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)) |
|
|
|
|
|
|
@ -432,12 +432,12 @@ def serve_file(file_path: str, download: bool = Query(False, description="Whethe |
|
|
|
|
|
|
|
# 尝试读取文件内容 |
|
|
|
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)}") |
|
|
@ -454,96 +454,96 @@ 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: |
|
|
@ -552,37 +552,37 @@ 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: |
|
|
|