feat: Refactor backup and sync paths, enhance configuration flexibility

This commit is contained in:
2025-12-15 10:45:28 +09:00
parent 834e21b331
commit 9ddc0d9eb2
6 changed files with 148 additions and 79 deletions
+23 -13
View File
@@ -3,6 +3,7 @@ import os
from app.utils.logger import logger
DEFAULT_SYNC_MODE = "merge_local_primary"
LOCAL_PLAYLISTS_FOLDER = "playlists"
DEFAULT_PATH_MAPPING = {
"mode": "SIMPLE",
"simple": [],
@@ -14,10 +15,22 @@ DEFAULT_PATH_MAPPING = {
}
}
CONFIG_PATH = os.path.abspath(
DEFAULT_CONFIG_PATH = os.path.abspath(
os.path.join(os.path.dirname(__file__), "..", "config.json")
)
# Allow Docker / users to relocate config for backup convenience.
# Example: /app/data/config/config.json
CONFIG_PATH = os.path.abspath(
os.environ.get("PLEXPLAYLISTSYNC_CONFIG_PATH", DEFAULT_CONFIG_PATH)
)
def _ensure_parent_dir(file_path: str) -> None:
parent = os.path.dirname(os.path.abspath(file_path))
if parent and not os.path.isdir(parent):
os.makedirs(parent, exist_ok=True)
class ServerConfig:
@@ -30,16 +43,18 @@ class ServerConfig:
self.timeout = 9
self.library_name = ""
self.sync_mode = DEFAULT_SYNC_MODE
self.local_path = "playlist"
# Local playlists folder is intentionally fixed and not part of config.
# Docker volume should mount host ./playlists -> container /app/playlists.
self.local_path = LOCAL_PLAYLISTS_FOLDER
self.path_rules: list[dict[str, str]] = [] # Legacy field for backward compatibility
self.path_mapping: dict = DEFAULT_PATH_MAPPING.copy()
self.schedule_mode = "DISABLED"
self.schedule_cron = ""
self.schedule_daily_time = "02:00"
self.schedule_daily_time = "00:00"
self.schedule_weekly_days = [0]
self.schedule_weekly_time = "03:00"
self.schedule_weekly_time = "00:00"
self.schedule_auto_watch = False
self.backup_enabled = False
self.backup_enabled = True
self.backup_retention_count = 5
self.load()
@@ -66,7 +81,8 @@ class ServerConfig:
self.timeout = config.get("timeout", 9)
self.library_name = config.get("library_name", "")
self.sync_mode = config.get("sync_mode", DEFAULT_SYNC_MODE)
self.local_path = config.get("local_path", "playlist")
# local_path is fixed by design and not configurable.
self.local_path = LOCAL_PLAYLISTS_FOLDER
self.path_rules = config.get("path_rules", []) or []
# Load path_mapping with default fallback
@@ -97,6 +113,7 @@ class ServerConfig:
logger.debug(f"Current server config: {self.__dict__}")
def save(self):
_ensure_parent_dir(CONFIG_PATH)
config = {
"theme": self.theme,
"token": self.token,
@@ -106,7 +123,6 @@ class ServerConfig:
"timeout": self.timeout,
"library_name": self.library_name,
"sync_mode": self.sync_mode,
"local_path": self.local_path,
"path_rules": self.path_rules,
"path_mapping": self.path_mapping,
"schedule_mode": self.schedule_mode,
@@ -144,9 +160,6 @@ class ServerConfig:
def set_sync_mode(self, sync_mode: str) -> None:
self.sync_mode = sync_mode
def set_local_path(self, local_path: str) -> None:
self.local_path = local_path or "playlist"
def set_theme(self, theme: str) -> None:
# check theme is valid
if theme not in ["auto", "dark", "light"]:
@@ -208,7 +221,6 @@ class ServerConfig:
timeout: int | None = None,
library_name: str | None = None,
sync_mode: str | None = None,
local_path: str | None = None,
path_rules: list[dict[str, str]] | None = None,
path_mapping: dict | None = None,
) -> None:
@@ -228,8 +240,6 @@ class ServerConfig:
self.set_library(library_name)
if sync_mode is not None:
self.set_sync_mode(sync_mode)
if local_path is not None:
self.set_local_path(local_path)
if path_rules is not None:
self.set_path_rules(path_rules)
if path_mapping is not None: