From fa0511f4e19f42e6e20989fd96a2f73eceff7499 Mon Sep 17 00:00:00 2001 From: mwjdot888 Date: Sun, 3 May 2026 00:33:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89temp=5Fpath=E8=B7=AF=E5=BE=84=EF=BC=8C=E4=B8=B4?= =?UTF-8?q?=E6=97=B6=E6=96=87=E4=BB=B6=E4=B8=8D=E5=86=8D=E4=BE=9D=E8=B5=96?= =?UTF-8?q?music=5Fpath=E6=8C=82=E8=BD=BD=20(#850)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 以前临时文件需挂载在music_path下才能访问,现在temp/前缀的文件直接从 config.temp_path提供访问,支持自定义任意临时目录路径。 Co-authored-by: mwjdot888 Co-authored-by: Claude Opus 4.7 --- xiaomusic/api/routers/file.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/xiaomusic/api/routers/file.py b/xiaomusic/api/routers/file.py index f23d141..d51edce 100644 --- a/xiaomusic/api/routers/file.py +++ b/xiaomusic/api/routers/file.py @@ -957,20 +957,33 @@ async def music_file(request: Request, file_path: str, key: str = "", code: str if not access_key_verification(f"/music/{file_path}", key, code): raise HTTPException(status_code=404, detail="File not found") - absolute_path = os.path.abspath(config.music_path) - absolute_file_path = os.path.normpath(os.path.join(absolute_path, file_path)) - if not absolute_file_path.startswith(absolute_path): - raise HTTPException(status_code=404, detail="File not found") - if not os.path.exists(absolute_file_path): - raise HTTPException(status_code=404, detail="File not found") + # temp/ 前缀表示文件在 temp_path 中 + if file_path.startswith("temp/"): + temp_file_name = file_path[5:] + if config.temp_path.startswith("/"): + temp_base = config.temp_path + else: + temp_base = os.path.abspath(config.temp_path) + absolute_file_path = os.path.normpath(os.path.join(temp_base, temp_file_name)) + if not absolute_file_path.startswith(temp_base): + raise HTTPException(status_code=404, detail="File not found") + if not os.path.exists(absolute_file_path): + raise HTTPException(status_code=404, detail="File not found") + else: + absolute_path = os.path.abspath(config.music_path) + absolute_file_path = os.path.normpath(os.path.join(absolute_path, file_path)) + if not absolute_file_path.startswith(absolute_path): + raise HTTPException(status_code=404, detail="File not found") + if not os.path.exists(absolute_file_path): + raise HTTPException(status_code=404, detail="File not found") # 移除MP3 ID3 v2标签和填充 if config.remove_id3tag and is_mp3(file_path): log.info(f"remove_id3tag:{config.remove_id3tag}, is_mp3:True ") temp_mp3_file = remove_id3_tags(absolute_file_path, config) if temp_mp3_file: - log.info(f"ID3 tag removed {absolute_file_path} to {temp_mp3_file}") - redirect = safe_redirect(f"/music/{temp_mp3_file}") + mp3_name = os.path.basename(temp_mp3_file) + redirect = safe_redirect(f"/music/temp/{mp3_name}") if redirect: return redirect else: @@ -979,8 +992,8 @@ async def music_file(request: Request, file_path: str, key: str = "", code: str if config.convert_to_mp3 and not is_mp3(file_path): temp_mp3_file = convert_file_to_mp3(absolute_file_path, config) if temp_mp3_file: - log.info(f"Converted file: {absolute_file_path} to {temp_mp3_file}") - redirect = safe_redirect(f"/music/{temp_mp3_file}") + mp3_name = os.path.basename(temp_mp3_file) + redirect = safe_redirect(f"/music/temp/{mp3_name}") if redirect: return redirect else: