Integrate React frontend with backend API
This commit is contained in:
@@ -17,6 +17,9 @@ class ServerConfig:
|
||||
self.port = "32400"
|
||||
self.library_name = ""
|
||||
self.path_rules: list[dict[str, str]] = []
|
||||
# 新增:本地播放列表目录和默认同步策略(用于新的前端界面)
|
||||
self.local_playlist_dir = os.getenv("LOCAL_PLAYLIST_DIR", "playlist")
|
||||
self.sync_strategy = "LOCAL_OVERWRITE"
|
||||
self.load()
|
||||
|
||||
def load(self) -> None:
|
||||
@@ -41,6 +44,10 @@ class ServerConfig:
|
||||
self.port = config.get("server_port", "32400")
|
||||
self.library_name = config.get("library_name", "")
|
||||
self.path_rules = config.get("path_rules", []) or []
|
||||
self.local_playlist_dir = config.get(
|
||||
"local_playlist_dir", self.local_playlist_dir
|
||||
)
|
||||
self.sync_strategy = config.get("sync_strategy", self.sync_strategy)
|
||||
logger.info(f"Server config loaded: {self.__dict__}")
|
||||
|
||||
def save(self):
|
||||
@@ -52,6 +59,8 @@ class ServerConfig:
|
||||
"server_port": self.port,
|
||||
"library_name": self.library_name,
|
||||
"path_rules": self.path_rules,
|
||||
"local_playlist_dir": self.local_playlist_dir,
|
||||
"sync_strategy": self.sync_strategy,
|
||||
}
|
||||
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
|
||||
json.dump(config, f, indent=4, ensure_ascii=False)
|
||||
@@ -82,6 +91,13 @@ class ServerConfig:
|
||||
def set_path_rules(self, path_rules: list[dict[str, str]]) -> None:
|
||||
self.path_rules = path_rules or []
|
||||
|
||||
def set_local_playlist_dir(self, playlist_dir: str) -> None:
|
||||
if playlist_dir:
|
||||
self.local_playlist_dir = playlist_dir
|
||||
|
||||
def set_sync_strategy(self, sync_strategy: str) -> None:
|
||||
self.sync_strategy = sync_strategy
|
||||
|
||||
def set_and_save_config(
|
||||
self,
|
||||
theme: str = None,
|
||||
@@ -91,6 +107,8 @@ class ServerConfig:
|
||||
port: str = None,
|
||||
library_name: str | None = None,
|
||||
path_rules: list[dict[str, str]] | None = None,
|
||||
local_playlist_dir: str | None = None,
|
||||
sync_strategy: str | None = None,
|
||||
) -> None:
|
||||
if theme is not None:
|
||||
self.set_theme(theme)
|
||||
@@ -106,6 +124,10 @@ class ServerConfig:
|
||||
self.set_library(library_name)
|
||||
if path_rules is not None:
|
||||
self.set_path_rules(path_rules)
|
||||
if local_playlist_dir is not None:
|
||||
self.set_local_playlist_dir(local_playlist_dir)
|
||||
if sync_strategy is not None:
|
||||
self.set_sync_strategy(sync_strategy)
|
||||
self.save()
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import os
|
||||
from typing import List
|
||||
from datetime import datetime
|
||||
from app.utils.logger import logger
|
||||
|
||||
def load_local_playlist(playlist_path: str) -> List[str]:
|
||||
@@ -42,7 +43,9 @@ def scan_local_playlists(base_path: str) -> list[dict]:
|
||||
base_path: Directory that contains playlist files.
|
||||
|
||||
Returns:
|
||||
A list of dictionaries with ``name`` and ``track_count`` keys.
|
||||
A list of dictionaries with ``name`` and ``track_count`` keys, and
|
||||
additional metadata (``path`` and ``last_modified``) for richer API
|
||||
responses.
|
||||
"""
|
||||
|
||||
playlists: list[dict] = []
|
||||
@@ -61,7 +64,15 @@ def scan_local_playlists(base_path: str) -> list[dict]:
|
||||
if not entry.name.lower().endswith((".m3u", ".m3u8")):
|
||||
continue
|
||||
tracks = load_local_playlist(entry.path)
|
||||
playlists.append({"name": entry.name, "track_count": len(tracks)})
|
||||
last_modified = datetime.fromtimestamp(entry.stat().st_mtime)
|
||||
playlists.append(
|
||||
{
|
||||
"name": entry.name,
|
||||
"track_count": len(tracks),
|
||||
"path": entry.path,
|
||||
"last_modified": last_modified.isoformat(),
|
||||
}
|
||||
)
|
||||
|
||||
playlists.sort(key=lambda item: item["name"].lower())
|
||||
logger.info(f"Found {len(playlists)} playlists under {absolute_path}.")
|
||||
|
||||
Reference in New Issue
Block a user