fix(base): 修复文件名中的中文编码问题

- 在 sanitize_filename 函数中添加 unquote 导入并使用其解码文件名
- 这个修改解决了非 ASCII 字符在文件名中显示为编码形式的问题
- Non-ASCll filename uploads don't comply with RFC 7578
This commit is contained in:
shaw
2025-06-20 15:44:23 +08:00
parent ff0f057042
commit 400cb4caf9
+3 -1
View File
@@ -2,6 +2,7 @@ import datetime
import hashlib import hashlib
import os import os
import uuid import uuid
from urllib.parse import unquote
from fastapi import UploadFile, HTTPException from fastapi import UploadFile, HTTPException
from typing import Optional, Tuple from typing import Optional, Tuple
@@ -17,7 +18,8 @@ async def get_file_path_name(file: UploadFile) -> Tuple[str, str, str, str, str]
today = datetime.datetime.now() today = datetime.datetime.now()
storage_path = settings.storage_path.strip("/") # 移除开头和结尾的斜杠 storage_path = settings.storage_path.strip("/") # 移除开头和结尾的斜杠
file_uuid = uuid.uuid4().hex file_uuid = uuid.uuid4().hex
filename = await sanitize_filename(file.filename) # 一些客户端对非ASCII字符会进行编码,需要解码
filename = await sanitize_filename(unquote(file.filename))
# 使用 UUID 作为子目录名 # 使用 UUID 作为子目录名
base_path = f"share/data/{today.strftime('%Y/%m/%d')}/{file_uuid}" base_path = f"share/data/{today.strftime('%Y/%m/%d')}/{file_uuid}"