@@ -133,6 +133,10 @@ async def create_file_code(code, **kwargs):
133133 return await FileCodes .create (code = code , ** kwargs )
134134
135135
136+ def normalize_share_code (code : str ) -> str :
137+ return str (code or "" ).strip ()
138+
139+
136140@share_api .post ("/text/" , dependencies = [Depends (share_required_login )])
137141async def share_text (
138142 text : str = Form (...),
@@ -196,7 +200,10 @@ async def share_file(
196200async def get_code_file_by_code (
197201 code : str , check : bool = True
198202) -> Tuple [bool , Union [FileCodes , str ]]:
199- file_code = await FileCodes .filter (code = code ).first ()
203+ normalized_code = normalize_share_code (code )
204+ if not normalized_code :
205+ return False , "文件不存在"
206+ file_code = await FileCodes .filter (code = normalized_code ).first ()
200207 if not file_code :
201208 return False , "文件不存在"
202209 if await file_code .is_expired () and check :
@@ -298,10 +305,11 @@ async def select_file(data: SelectFileModel, ip: str = Depends(ip_limit["error"]
298305@share_api .get ("/download" )
299306async def download_file (key : str , code : str , ip : str = Depends (ip_limit ["error" ])):
300307 file_storage : FileStorageInterface = storages [settings .file_storage ]()
301- if await get_select_token (code ) != key :
308+ normalized_code = normalize_share_code (code )
309+ if await get_select_token (normalized_code ) != key :
302310 ip_limit ["error" ].add_ip (ip )
303311 raise HTTPException (status_code = 403 , detail = "下载鉴权失败" )
304- has , file_code = await get_code_file_by_code (code , False )
312+ has , file_code = await get_code_file_by_code (normalized_code , False )
305313 if not has :
306314 return APIResponse (code = 404 , detail = "文件不存在" )
307315 assert isinstance (file_code , FileCodes )
0 commit comments