Skip to content

Commit 62d1a23

Browse files
committed
fix(base): 修复文件名中的中文编码问题
- 在 sanitize_filename 函数中添加 unquote 导入并使用其解码文件名 - 这个修改解决了非 ASCII 字符在文件名中显示为编码形式的问题 - Non-ASCll filename uploads don't comply with RFC 7578
1 parent 9a80d7a commit 62d1a23

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

apps/base/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import hashlib
33
import os
44
import uuid
5+
from urllib.parse import unquote
56

67
from fastapi import UploadFile, HTTPException
78
from typing import Optional, Tuple
@@ -17,7 +18,8 @@ async def get_file_path_name(file: UploadFile) -> Tuple[str, str, str, str, str]
1718
today = datetime.datetime.now()
1819
storage_path = settings.storage_path.strip("/") # 移除开头和结尾的斜杠
1920
file_uuid = uuid.uuid4().hex
20-
filename = await sanitize_filename(file.filename)
21+
# 一些客户端对非ASCII字符会进行编码,需要解码
22+
filename = await sanitize_filename(unquote(file.filename))
2123
# 使用 UUID 作为子目录名
2224
base_path = f"share/data/{today.strftime('%Y/%m/%d')}/{file_uuid}"
2325

0 commit comments

Comments
 (0)