Handle invalid UTF-8 with replacement decoding

This commit is contained in:
jxxghp
2026-06-21 09:56:34 +08:00
parent 99e369aaa4
commit 68f18db374
29 changed files with 139 additions and 121 deletions

View File

@@ -44,7 +44,7 @@ async def user_message(
args = request.query_params
source = args.get("source")
content_type = request.headers.get("content-type", "")
body_text = body.decode("utf-8", errors="ignore")
body_text = body.decode("utf-8", errors="replace")
image_markers = [
marker
for marker in (

View File

@@ -1002,7 +1002,7 @@ async def get_logging(
# 读取历史日志
async with aiofiles.open(
log_path, mode="r", encoding="utf-8", errors="ignore"
log_path, mode="r", encoding="utf-8", errors="replace"
) as f:
# 优化大文件读取策略
if file_size > 100 * 1024:
@@ -1031,7 +1031,7 @@ async def get_logging(
# 实时监听新日志
async with aiofiles.open(
log_path, mode="r", encoding="utf-8", errors="ignore"
log_path, mode="r", encoding="utf-8", errors="replace"
) as f:
# 移动文件指针到文件末尾,继续监听新增内容
await f.seek(0, 2)
@@ -1070,7 +1070,7 @@ async def get_logging(
try:
# 使用 aiofiles 异步读取文件
async with aiofiles.open(
log_path, mode="r", encoding="utf-8", errors="ignore"
log_path, mode="r", encoding="utf-8", errors="replace"
) as file:
text = await file.read()
# 倒序输出

View File

@@ -70,7 +70,7 @@ async def update_cookie(req: schemas.CookieData):
content = json.dumps({"encrypted": req.encrypted})
async with aiofiles.open(file_path, encoding="utf-8", mode="w") as file:
await file.write(content)
async with aiofiles.open(file_path, encoding="utf-8", mode="r") as file:
async with aiofiles.open(file_path, encoding="utf-8", errors="replace", mode="r") as file:
read_content = await file.read()
if read_content == content:
return {"action": "done"}
@@ -89,7 +89,7 @@ async def load_encrypt_data(uuid: str) -> Dict[str, Any]:
raise HTTPException(status_code=404, detail="Item not found")
# 读取文件
async with aiofiles.open(file_path, encoding="utf-8", mode="r") as file:
async with aiofiles.open(file_path, encoding="utf-8", errors="replace", mode="r") as file:
read_content = await file.read()
data = json.loads(read_content.encode("utf-8"))
return data