Skip to content

Commit

Permalink
fix: 尝试修复播放卡顿问题 see #124
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Jul 15, 2024
1 parent 5ef1f2d commit 560ea1a
Showing 1 changed file with 4 additions and 18 deletions.
22 changes: 4 additions & 18 deletions xiaomusic/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down

0 comments on commit 560ea1a

Please sign in to comment.