fix: 尝试修复播放卡顿问题 see #124

This commit is contained in:
涵曦 2024-07-15 15:16:38 +00:00
parent 5ef1f2d940
commit 560ea1aeca

View File

@ -4,7 +4,6 @@ import os
import secrets import secrets
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from dataclasses import asdict from dataclasses import asdict
from pathlib import Path
from typing import Annotated from typing import Annotated
from fastapi import Depends, FastAPI, HTTPException, Request, status from fastapi import Depends, FastAPI, HTTPException, Request, status
@ -75,6 +74,10 @@ def reset_http_server():
else: else:
app.dependency_overrides = {} 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): def HttpInit(_xiaomusic):
global xiaomusic, config, log global xiaomusic, config, log
@ -87,23 +90,6 @@ def HttpInit(_xiaomusic):
reset_http_server() 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("/") @app.get("/")
async def read_index(): async def read_index():
return FileResponse("xiaomusic/static/index.html") return FileResponse("xiaomusic/static/index.html")