fix(book): 修复书籍下载服务中的文件类型验证和错误处理

- 添加 application/vnd.rar 到允许的内容类型列表
- 在响应中添加 raise_for_status() 以处理 HTTP 错误
- 添加 Content-Type 打印用于调试目的
- 改进文件类型验证逻辑
This commit is contained in:
2026-08-14 19:07:33 +08:00
parent 26e1690f84
commit 837bd4df76
+3 -1
View File
@@ -11,7 +11,7 @@ from app.models import Setting
class BookService: class BookService:
ALLOWED_CONTENT_TYPES = ['"application/octet-stream"', "application/octet-stream", "text/plain", ALLOWED_CONTENT_TYPES = ['"application/octet-stream"', "application/octet-stream", "text/plain",
"application/zip"] "application/zip", "application/vnd.rar"]
def __init__(self): def __init__(self):
setting_record = Setting.query.filter_by(name="book_download").first() setting_record = Setting.query.filter_by(name="book_download").first()
@@ -103,10 +103,12 @@ class BookService:
print(f'跳转页面了:{file_url}') print(f'跳转页面了:{file_url}')
response = self.session.get(file_url, headers={"referer": file_url}, stream=True) response = self.session.get(file_url, headers={"referer": file_url}, stream=True)
print(f"下载状态码:{response.status_code} {file_url}") print(f"下载状态码:{response.status_code} {file_url}")
response.raise_for_status()
print(dir_name + "/" + file_name) print(dir_name + "/" + file_name)
content_type = response.headers.get('Content-Type', '') content_type = response.headers.get('Content-Type', '')
if content_type not in self.ALLOWED_CONTENT_TYPES: if content_type not in self.ALLOWED_CONTENT_TYPES:
yield f"event: mes_error\ndata: {self.setting['targetDate']} {file_name}\n\n" yield f"event: mes_error\ndata: {self.setting['targetDate']} {file_name}\n\n"
print(content_type)
print("不是可下载文件") print("不是可下载文件")
return return
target_dir = self.download_path / self.setting["targetDate"] / dir_name / file_name target_dir = self.download_path / self.setting["targetDate"] / dir_name / file_name