main.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. from __future__ import annotations
  2. import json
  3. import mimetypes
  4. import shutil
  5. import threading
  6. import uuid
  7. import base64
  8. import urllib.error
  9. import urllib.parse
  10. import urllib.request
  11. from datetime import datetime, timezone
  12. from pathlib import Path
  13. from typing import Any
  14. from fastapi import FastAPI, File, Form, HTTPException, Request, UploadFile
  15. from fastapi.responses import FileResponse, HTMLResponse, JSONResponse, RedirectResponse, StreamingResponse
  16. from fastapi.staticfiles import StaticFiles
  17. from fastapi.templating import Jinja2Templates
  18. from pydantic import BaseModel
  19. BASE_DIR = Path(__file__).resolve().parent.parent
  20. LOCAL_MUSIC_DIR = BASE_DIR / "mp3file"
  21. CLOUD_MUSIC_DIR = Path("/mnt/baiducloud/百度网盘/mp3file")
  22. CLOUD_WEBDAV_BASE = "http://110.42.102.94:5244/dav"
  23. CLOUD_ALIST_BASE = "http://110.42.102.94:5244"
  24. CLOUD_WEBDAV_USER = "sequoia00"
  25. CLOUD_WEBDAV_PASSWORD = "792199bb"
  26. MUSIC_ROOTS: list[tuple[str, Path, str]] = [
  27. ("", LOCAL_MUSIC_DIR, "本地音乐"),
  28. ("cloud", CLOUD_MUSIC_DIR, "百度网盘"),
  29. ]
  30. PLAYLISTS_FILE = BASE_DIR / "playlists.json"
  31. CACHE_DIR = BASE_DIR / ".cache"
  32. CLOUD_LIBRARY_CACHE_FILE = CACHE_DIR / "cloud_library.json"
  33. CLOUD_COVER_CACHE_DIR = CACHE_DIR / "cloud_covers"
  34. SUPPORTED_EXTENSIONS = {
  35. ".mp3",
  36. ".wav",
  37. ".flac",
  38. ".m3u",
  39. ".m3u8",
  40. ".ogg",
  41. ".aac",
  42. ".wma",
  43. ".opus",
  44. ".oga",
  45. ".mp4",
  46. ".m4a",
  47. ".webm",
  48. }
  49. IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp", ".gif"}
  50. app = FastAPI(title="MusicWeb", version="1.0.0")
  51. app.mount("/static", StaticFiles(directory=BASE_DIR / "static"), name="static")
  52. templates = Jinja2Templates(directory=str(BASE_DIR / "templates"))
  53. CLOUD_REFRESH_STATE = {"running": False}
  54. CLOUD_REFRESH_LOCK = threading.Lock()
  55. class FolderCreateRequest(BaseModel):
  56. path: str
  57. class MoveRequest(BaseModel):
  58. source: str
  59. destination_dir: str
  60. class PlaylistCreateRequest(BaseModel):
  61. name: str
  62. tracks: list[str]
  63. class PlaylistUpdateRequest(BaseModel):
  64. tracks: list[str]
  65. def ensure_storage() -> None:
  66. LOCAL_MUSIC_DIR.mkdir(parents=True, exist_ok=True)
  67. CLOUD_MUSIC_DIR.mkdir(parents=True, exist_ok=True)
  68. CACHE_DIR.mkdir(parents=True, exist_ok=True)
  69. CLOUD_COVER_CACHE_DIR.mkdir(parents=True, exist_ok=True)
  70. if not PLAYLISTS_FILE.exists():
  71. PLAYLISTS_FILE.write_text("[]", encoding="utf-8")
  72. def safe_music_path(relative_path: str) -> Path:
  73. prefix, _, inner_path = relative_path.partition("/")
  74. roots = {key: root.resolve() for key, root, _ in MUSIC_ROOTS}
  75. if prefix in roots:
  76. candidate = (roots[prefix] / inner_path).resolve()
  77. if candidate != roots[prefix] and roots[prefix] not in candidate.parents:
  78. raise HTTPException(status_code=400, detail="Invalid path")
  79. return candidate
  80. candidate = (LOCAL_MUSIC_DIR / relative_path).resolve()
  81. local_root = LOCAL_MUSIC_DIR.resolve()
  82. if candidate != local_root and local_root not in candidate.parents:
  83. raise HTTPException(status_code=400, detail="Invalid path")
  84. return candidate
  85. def load_playlists() -> list[dict[str, Any]]:
  86. ensure_storage()
  87. return json.loads(PLAYLISTS_FILE.read_text(encoding="utf-8"))
  88. def save_playlists(playlists: list[dict[str, Any]]) -> None:
  89. PLAYLISTS_FILE.write_text(
  90. json.dumps(playlists, ensure_ascii=False, indent=2), encoding="utf-8"
  91. )
  92. def is_under_root(path: Path, root: Path) -> bool:
  93. resolved_path = path.resolve()
  94. resolved_root = root.resolve()
  95. return resolved_path == resolved_root or resolved_root in resolved_path.parents
  96. def track_path_for(root: Path, path: Path, prefix: str = "") -> str:
  97. relative = path.relative_to(root).as_posix()
  98. return f"{prefix}/{relative}" if prefix else relative
  99. def track_path_from_abs(path: Path) -> str:
  100. if is_under_root(path, LOCAL_MUSIC_DIR):
  101. return path.relative_to(LOCAL_MUSIC_DIR).as_posix()
  102. if is_under_root(path, CLOUD_MUSIC_DIR):
  103. return f"cloud/{path.relative_to(CLOUD_MUSIC_DIR).as_posix()}"
  104. return path.name
  105. def path_is_under(path: Path, root: Path) -> bool:
  106. try:
  107. resolved_path = path.resolve()
  108. resolved_root = root.resolve()
  109. except OSError:
  110. return False
  111. return resolved_path == resolved_root or resolved_root in resolved_path.parents
  112. def trigger_cloud_cache_refresh_if_needed(*paths: Path) -> None:
  113. if any(path_is_under(path, CLOUD_MUSIC_DIR) for path in paths):
  114. start_cloud_library_refresh()
  115. def cloud_cover_cache_path(relative_path: str) -> Path:
  116. inner_path = relative_path.removeprefix("cloud/").lstrip("/")
  117. target = (CLOUD_COVER_CACHE_DIR / inner_path).resolve()
  118. cache_root = CLOUD_COVER_CACHE_DIR.resolve()
  119. if target != cache_root and cache_root not in target.parents:
  120. raise HTTPException(status_code=400, detail="Invalid cover path")
  121. return target
  122. def cloud_webdav_url(relative_path: str) -> str:
  123. inner_path = relative_path.removeprefix("cloud/").lstrip("/")
  124. encoded = "/".join(urllib.parse.quote(part) for part in inner_path.split("/") if part)
  125. base = CLOUD_WEBDAV_BASE.rstrip("/")
  126. return f"{base}/{encoded}" if encoded else base
  127. def cloud_alist_api_path(relative_path: str) -> str:
  128. inner_path = relative_path.removeprefix("cloud/").lstrip("/")
  129. return f"/百度网盘/mp3file/{inner_path}" if inner_path else "/百度网盘/mp3file"
  130. def alist_login_token() -> str:
  131. payload = json.dumps(
  132. {
  133. "username": CLOUD_WEBDAV_USER,
  134. "password": CLOUD_WEBDAV_PASSWORD,
  135. }
  136. ).encode("utf-8")
  137. request = urllib.request.Request(
  138. f"{CLOUD_ALIST_BASE.rstrip('/')}/api/auth/login",
  139. data=payload,
  140. headers={"Content-Type": "application/json", "User-Agent": "MusicWebPlayer/1.0"},
  141. )
  142. try:
  143. with urllib.request.urlopen(request, timeout=20) as response:
  144. result = json.loads(response.read().decode("utf-8"))
  145. except urllib.error.HTTPError as error:
  146. raise HTTPException(status_code=error.code, detail=error.reason)
  147. except urllib.error.URLError as error:
  148. raise HTTPException(status_code=502, detail=str(error.reason))
  149. token = result.get("data", {}).get("token")
  150. if not token:
  151. raise HTTPException(status_code=502, detail="AList login failed")
  152. return token
  153. def cloud_raw_url(relative_path: str) -> str:
  154. payload = json.dumps({"path": cloud_alist_api_path(relative_path), "password": ""}).encode("utf-8")
  155. request = urllib.request.Request(
  156. f"{CLOUD_ALIST_BASE.rstrip('/')}/api/fs/get",
  157. data=payload,
  158. headers={
  159. "Content-Type": "application/json",
  160. "Authorization": alist_login_token(),
  161. "User-Agent": "MusicWebPlayer/1.0",
  162. },
  163. )
  164. try:
  165. with urllib.request.urlopen(request, timeout=20) as response:
  166. result = json.loads(response.read().decode("utf-8"))
  167. except urllib.error.HTTPError as error:
  168. raise HTTPException(status_code=error.code, detail=error.reason)
  169. except urllib.error.URLError as error:
  170. raise HTTPException(status_code=502, detail=str(error.reason))
  171. data = result.get("data", {})
  172. sign = data.get("sign")
  173. path = data.get("path")
  174. if not sign or not path:
  175. raise HTTPException(status_code=404, detail="Cloud signed url not found")
  176. encoded_path = "/".join(urllib.parse.quote(part) for part in path.lstrip("/").split("/"))
  177. quoted_sign = urllib.parse.quote(sign, safe="")
  178. return f"{CLOUD_ALIST_BASE.rstrip('/')}/d/{urllib.parse.quote('百度网盘')}/{encoded_path}?sign={quoted_sign}"
  179. def is_supported_file(path: Path) -> bool:
  180. return path.is_file() and path.suffix.lower() in SUPPORTED_EXTENSIONS
  181. def track_url(relative_path: str) -> str:
  182. return f"/api/stream/{relative_path}"
  183. def build_track(relative_path: str) -> dict[str, str]:
  184. filename = Path(relative_path).name
  185. return {
  186. "id": relative_path,
  187. "name": filename,
  188. "path": relative_path,
  189. "url": track_url(relative_path),
  190. "folder": str(Path(relative_path).parent).replace(".", "").strip("/"),
  191. }
  192. def find_cover_for_directory(root: Path, prefix: str = "") -> str | None:
  193. path = root / "cover.jpg"
  194. if path.is_file():
  195. return track_path_for(root, path, prefix)
  196. return None
  197. def sanitize_cover_value(cover: Any) -> str | None:
  198. if not isinstance(cover, str) or not cover:
  199. return None
  200. parsed = urllib.parse.urlparse(cover)
  201. path = parsed.path or cover
  202. return cover if path.lower().endswith("/cover.jpg") else None
  203. def sanitize_library_tree(node: dict[str, Any]) -> dict[str, Any]:
  204. node["cover"] = sanitize_cover_value(node.get("cover"))
  205. for child in node.get("folders", []):
  206. if isinstance(child, dict):
  207. sanitize_library_tree(child)
  208. return node
  209. def build_tree(root: Path, prefix: str = "", label: str = "音乐库") -> dict[str, Any]:
  210. cover_path = find_cover_for_directory(root, prefix)
  211. node = {
  212. "name": label,
  213. "path": prefix,
  214. "cover": f"/api/cover/{cover_path}" if cover_path else None,
  215. "folders": [],
  216. "tracks": [],
  217. }
  218. for child in sorted(root.iterdir(), key=lambda item: (item.is_file(), item.name.lower())):
  219. if child.is_dir():
  220. child_rel = child.relative_to(root).as_posix()
  221. child_prefix = f"{prefix}/{child_rel}" if prefix else child_rel
  222. node["folders"].append(build_tree(child, child_prefix, child.name))
  223. elif is_supported_file(child):
  224. node["tracks"].append(build_track(track_path_for(root, child, prefix)))
  225. return node
  226. def collect_tracks(root: Path, prefix: str = "") -> list[dict[str, str]]:
  227. tracks: list[dict[str, str]] = []
  228. for path in sorted(root.rglob("*")):
  229. if is_supported_file(path):
  230. tracks.append(build_track(track_path_for(root, path, prefix)))
  231. return tracks
  232. def root_entry(source: str) -> tuple[str, Path, str]:
  233. for prefix, root, label in MUSIC_ROOTS:
  234. if prefix == source:
  235. return prefix, root, label
  236. raise HTTPException(status_code=404, detail="Library source not found")
  237. def current_timestamp() -> str:
  238. return datetime.now(timezone.utc).isoformat()
  239. def build_library_payload(source: str | None = None) -> dict[str, Any]:
  240. ensure_storage()
  241. entries = MUSIC_ROOTS if source is None else [root_entry(source)]
  242. tree = {
  243. "name": "音乐库",
  244. "path": "",
  245. "cover": None,
  246. "folders": [
  247. build_tree(root, prefix, label)
  248. for prefix, root, label in entries
  249. if root.exists()
  250. ],
  251. "tracks": [],
  252. }
  253. all_tracks: list[dict[str, str]] = []
  254. for prefix, root, _ in entries:
  255. if root.exists():
  256. all_tracks.extend(collect_tracks(root, prefix))
  257. sanitize_library_tree(tree)
  258. return {
  259. "tree": tree,
  260. "all_tracks": all_tracks,
  261. "playlists": load_playlists(),
  262. }
  263. def read_cloud_library_cache() -> dict[str, Any] | None:
  264. if not CLOUD_LIBRARY_CACHE_FILE.exists():
  265. return None
  266. try:
  267. payload = json.loads(CLOUD_LIBRARY_CACHE_FILE.read_text(encoding="utf-8"))
  268. except (OSError, json.JSONDecodeError):
  269. return None
  270. if not isinstance(payload, dict):
  271. return None
  272. tree = payload.get("tree")
  273. if isinstance(tree, dict):
  274. sanitize_library_tree(tree)
  275. return payload
  276. def write_cloud_library_cache(payload: dict[str, Any]) -> dict[str, Any]:
  277. cached_payload = {
  278. **payload,
  279. "cache": {
  280. "updated_at": current_timestamp(),
  281. "is_cached": True,
  282. "refreshing": False,
  283. },
  284. }
  285. CLOUD_LIBRARY_CACHE_FILE.write_text(
  286. json.dumps(cached_payload, ensure_ascii=False, indent=2),
  287. encoding="utf-8",
  288. )
  289. return cached_payload
  290. def build_cloud_library_payload() -> dict[str, Any]:
  291. return build_library_payload("cloud")
  292. def cloud_library_payload_from_cache() -> dict[str, Any] | None:
  293. payload = read_cloud_library_cache()
  294. if not payload:
  295. return None
  296. cache_meta = payload.get("cache") if isinstance(payload.get("cache"), dict) else {}
  297. payload["cache"] = {
  298. "updated_at": cache_meta.get("updated_at"),
  299. "is_cached": True,
  300. "refreshing": CLOUD_REFRESH_STATE["running"],
  301. }
  302. return payload
  303. def refresh_cloud_library_cache_sync() -> dict[str, Any]:
  304. payload = build_cloud_library_payload()
  305. return write_cloud_library_cache(payload)
  306. def start_cloud_library_refresh() -> bool:
  307. with CLOUD_REFRESH_LOCK:
  308. if CLOUD_REFRESH_STATE["running"]:
  309. return False
  310. CLOUD_REFRESH_STATE["running"] = True
  311. def runner() -> None:
  312. try:
  313. refresh_cloud_library_cache_sync()
  314. finally:
  315. with CLOUD_REFRESH_LOCK:
  316. CLOUD_REFRESH_STATE["running"] = False
  317. threading.Thread(target=runner, daemon=True).start()
  318. return True
  319. @app.on_event("startup")
  320. def startup_event() -> None:
  321. ensure_storage()
  322. if not CLOUD_LIBRARY_CACHE_FILE.exists():
  323. start_cloud_library_refresh()
  324. @app.get("/", response_class=HTMLResponse)
  325. def index(request: Request) -> HTMLResponse:
  326. return templates.TemplateResponse("index.html", {"request": request})
  327. @app.get("/api/library")
  328. def library() -> JSONResponse:
  329. return JSONResponse(build_library_payload())
  330. @app.get("/api/library/{source}")
  331. def library_by_source(source: str) -> JSONResponse:
  332. normalized = "" if source == "local" else source
  333. if normalized == "cloud":
  334. cached = cloud_library_payload_from_cache()
  335. if cached:
  336. start_cloud_library_refresh()
  337. return JSONResponse(cached)
  338. payload = refresh_cloud_library_cache_sync()
  339. return JSONResponse(payload)
  340. return JSONResponse(build_library_payload(normalized))
  341. @app.get("/api/library/{source}/refresh")
  342. def refresh_library_by_source(source: str) -> JSONResponse:
  343. normalized = "" if source == "local" else source
  344. if normalized != "cloud":
  345. return JSONResponse(build_library_payload(normalized))
  346. started = start_cloud_library_refresh()
  347. cached = cloud_library_payload_from_cache()
  348. if cached:
  349. cached["cache"]["refreshing"] = True
  350. return JSONResponse({"started": started, "library": cached})
  351. payload = refresh_cloud_library_cache_sync()
  352. return JSONResponse({"started": started, "library": payload})
  353. @app.get("/api/stream/{file_path:path}")
  354. def stream_file(request: Request, file_path: str) -> StreamingResponse:
  355. if file_path.startswith("cloud/"):
  356. return proxy_cloud_stream(cloud_raw_url(file_path), request.headers.get("range"))
  357. file = safe_music_path(file_path)
  358. if not file.exists() or not file.is_file():
  359. raise HTTPException(status_code=404, detail="File not found")
  360. media_type = mimetypes.guess_type(file.name)[0] or "application/octet-stream"
  361. def file_iterator() -> Any:
  362. with file.open("rb") as handle:
  363. while chunk := handle.read(1024 * 1024):
  364. if not chunk:
  365. break
  366. yield chunk
  367. return StreamingResponse(
  368. file_iterator(),
  369. media_type=media_type,
  370. headers={"Cache-Control": "private, no-cache"},
  371. )
  372. @app.get("/api/cloud-url/{file_path:path}")
  373. def get_cloud_url(file_path: str) -> JSONResponse:
  374. if not file_path.startswith("cloud/"):
  375. raise HTTPException(status_code=400, detail="Not a cloud track")
  376. return JSONResponse({"url": cloud_raw_url(file_path)})
  377. def proxy_cloud_stream(remote_url: str, range_header: str | None = None) -> StreamingResponse:
  378. auth = base64.b64encode(f"{CLOUD_WEBDAV_USER}:{CLOUD_WEBDAV_PASSWORD}".encode("utf-8")).decode("ascii")
  379. headers = {
  380. "Authorization": f"Basic {auth}",
  381. "User-Agent": "MusicWebPlayer/1.0",
  382. }
  383. if range_header:
  384. headers["Range"] = range_header
  385. request = urllib.request.Request(
  386. remote_url,
  387. headers=headers,
  388. )
  389. try:
  390. response = urllib.request.urlopen(request, timeout=30)
  391. except urllib.error.HTTPError as error:
  392. raise HTTPException(status_code=error.code, detail=error.reason)
  393. except urllib.error.URLError as error:
  394. raise HTTPException(status_code=502, detail=str(error.reason))
  395. media_type = mimetypes.guess_type(urllib.parse.urlparse(remote_url).path)[0] or response.headers.get_content_type() or "audio/mpeg"
  396. passthrough_headers = {
  397. "Cache-Control": "private, no-cache",
  398. "Accept-Ranges": response.headers.get("Accept-Ranges") or "bytes",
  399. "Content-Type": media_type,
  400. }
  401. for key in ("ETag", "Last-Modified", "Content-Range"):
  402. value = response.headers.get(key)
  403. if value:
  404. passthrough_headers[key] = value
  405. def remote_iterator() -> Any:
  406. with response:
  407. while chunk := response.read(1024 * 1024):
  408. yield chunk
  409. return StreamingResponse(
  410. remote_iterator(),
  411. status_code=getattr(response, "status", 200),
  412. media_type=media_type,
  413. headers=passthrough_headers,
  414. )
  415. def cache_cloud_cover(relative_path: str) -> Path:
  416. cached_file = cloud_cover_cache_path(relative_path)
  417. if cached_file.exists() and cached_file.is_file():
  418. return cached_file
  419. cached_file.parent.mkdir(parents=True, exist_ok=True)
  420. remote_url = cloud_raw_url(relative_path)
  421. request = urllib.request.Request(
  422. remote_url,
  423. headers={"User-Agent": "MusicWebPlayer/1.0"},
  424. )
  425. try:
  426. with urllib.request.urlopen(request, timeout=30) as response:
  427. with cached_file.open("wb") as output:
  428. shutil.copyfileobj(response, output)
  429. except urllib.error.HTTPError as error:
  430. raise HTTPException(status_code=error.code, detail=error.reason)
  431. except urllib.error.URLError as error:
  432. raise HTTPException(status_code=502, detail=str(error.reason))
  433. except OSError as error:
  434. raise HTTPException(status_code=500, detail=str(error))
  435. return cached_file
  436. @app.get("/api/cover/{file_path:path}")
  437. def cover_file(request: Request, file_path: str):
  438. if file_path.startswith("cloud/"):
  439. if Path(file_path).name.lower() != "cover.jpg":
  440. raise HTTPException(status_code=404, detail="Cover not found")
  441. cached_file = cache_cloud_cover(file_path)
  442. media_type = mimetypes.guess_type(cached_file.name)[0] or "image/jpeg"
  443. return FileResponse(cached_file, media_type=media_type, filename=cached_file.name)
  444. file = safe_music_path(file_path)
  445. if file.name.lower() != "cover.jpg" or not file.exists() or not file.is_file():
  446. raise HTTPException(status_code=404, detail="Cover not found")
  447. media_type = mimetypes.guess_type(file.name)[0] or "application/octet-stream"
  448. return FileResponse(file, media_type=media_type, filename=file.name)
  449. @app.post("/api/upload")
  450. async def upload_files(
  451. files: list[UploadFile] = File(...),
  452. target_dir: str = Form(default=""),
  453. ) -> JSONResponse:
  454. destination = safe_music_path(target_dir)
  455. if not str(destination).startswith(str(LOCAL_MUSIC_DIR.resolve())):
  456. raise HTTPException(status_code=400, detail="Upload destination must be local music dir")
  457. destination.mkdir(parents=True, exist_ok=True)
  458. saved: list[str] = []
  459. for upload in files:
  460. suffix = Path(upload.filename or "").suffix.lower()
  461. if suffix not in SUPPORTED_EXTENSIONS:
  462. continue
  463. filename = Path(upload.filename or f"upload-{uuid.uuid4().hex}").name
  464. file_path = destination / filename
  465. with file_path.open("wb") as buffer:
  466. shutil.copyfileobj(upload.file, buffer)
  467. saved.append(track_path_from_abs(file_path))
  468. trigger_cloud_cache_refresh_if_needed(destination)
  469. return JSONResponse({"saved": saved})
  470. @app.post("/api/folder")
  471. def create_folder(payload: FolderCreateRequest) -> JSONResponse:
  472. folder = safe_music_path(payload.path)
  473. if not str(folder).startswith(str(LOCAL_MUSIC_DIR.resolve())):
  474. raise HTTPException(status_code=400, detail="Folder must be created in local music dir")
  475. folder.mkdir(parents=True, exist_ok=True)
  476. trigger_cloud_cache_refresh_if_needed(folder)
  477. return JSONResponse({"created": track_path_from_abs(folder)})
  478. @app.post("/api/move")
  479. def move_file(payload: MoveRequest) -> JSONResponse:
  480. source = safe_music_path(payload.source)
  481. destination_dir = safe_music_path(payload.destination_dir)
  482. if not source.exists():
  483. raise HTTPException(status_code=404, detail="Source not found")
  484. if not destination_dir.exists():
  485. destination_dir.mkdir(parents=True, exist_ok=True)
  486. destination = destination_dir / source.name
  487. shutil.move(str(source), str(destination))
  488. trigger_cloud_cache_refresh_if_needed(source, destination_dir, destination)
  489. return JSONResponse({"moved": track_path_from_abs(destination)})
  490. @app.post("/api/playlists")
  491. def create_playlist(payload: PlaylistCreateRequest) -> JSONResponse:
  492. playlists = load_playlists()
  493. playlist = {
  494. "id": uuid.uuid4().hex,
  495. "name": payload.name.strip() or "未命名播放列表",
  496. "tracks": payload.tracks,
  497. }
  498. playlists.append(playlist)
  499. save_playlists(playlists)
  500. return JSONResponse(playlist)
  501. @app.put("/api/playlists/{playlist_id}")
  502. def update_playlist(playlist_id: str, payload: PlaylistUpdateRequest) -> JSONResponse:
  503. playlists = load_playlists()
  504. for playlist in playlists:
  505. if playlist["id"] == playlist_id:
  506. playlist["tracks"] = payload.tracks
  507. save_playlists(playlists)
  508. return JSONResponse(playlist)
  509. raise HTTPException(status_code=404, detail="Playlist not found")
  510. @app.delete("/api/playlists/{playlist_id}")
  511. def delete_playlist(playlist_id: str) -> JSONResponse:
  512. playlists = load_playlists()
  513. filtered = [playlist for playlist in playlists if playlist["id"] != playlist_id]
  514. if len(filtered) == len(playlists):
  515. raise HTTPException(status_code=404, detail="Playlist not found")
  516. save_playlists(filtered)
  517. return JSONResponse({"deleted": playlist_id})