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