mirror of
https://github.com/hanxi/xiaomusic.git
synced 2026-05-06 00:06:11 +08:00
feat: 支持自定义temp_path路径,临时文件不再依赖music_path挂载 (#850)
以前临时文件需挂载在music_path下才能访问,现在temp/前缀的文件直接从 config.temp_path提供访问,支持自定义任意临时目录路径。 Co-authored-by: mwjdot888 <mwjdot888@users.noreply.github.com> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user