From 560ea1aeca563ab8275b5151b529671824f0e65e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Mon, 15 Jul 2024 15:16:38 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B0=9D=E8=AF=95=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=92=AD=E6=94=BE=E5=8D=A1=E9=A1=BF=E9=97=AE=E9=A2=98=20see=20?= =?UTF-8?q?#124?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xiaomusic/httpserver.py | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/xiaomusic/httpserver.py b/xiaomusic/httpserver.py index e205f7794..cc9ccd386 100644 --- a/xiaomusic/httpserver.py +++ b/xiaomusic/httpserver.py @@ -4,7 +4,6 @@ import secrets from contextlib import asynccontextmanager from dataclasses import asdict -from pathlib import Path from typing import Annotated from fastapi import Depends, FastAPI, HTTPException, Request, status @@ -75,6 +74,10 @@ def reset_http_server(): else: app.dependency_overrides = {} + # 更新 music 链接 + app.router.routes = [route for route in app.router.routes if route.path != "/music"] + app.mount("/music", StaticFiles(directory=config.music_path), name="music") + def HttpInit(_xiaomusic): global xiaomusic, config, log @@ -87,23 +90,6 @@ def HttpInit(_xiaomusic): reset_http_server() -@app.get("/music/{file_path:path}") -async def read_music_file(file_path: str): - base_dir = os.path.abspath(config.music_path) - real_path = os.path.normpath(os.path.join(base_dir, file_path)) - log.info(f"read_music_file. file_path:{file_path} real_path:{real_path}") - if not real_path.startswith(base_dir): - raise HTTPException( - status_code=403, detail="Access to this file is not permitted" - ) - - file_location = Path(real_path).resolve() - if not file_location.exists() or not file_location.is_file(): - raise HTTPException(status_code=404, detail="File not found") - - return FileResponse(file_location) - - @app.get("/") async def read_index(): return FileResponse("xiaomusic/static/index.html")