Compare commits
10 Commits
testbed
...
86d0adebda
| Author | SHA1 | Date | |
|---|---|---|---|
| 86d0adebda | |||
| 304e973db1 | |||
| 6c84112d29 | |||
| 1131b81454 | |||
| 6a1780bcee | |||
| fbafe75fae | |||
| fbb5bb55c7 | |||
| f9dbe733c3 | |||
| 350f1d97e6 | |||
| c18ff5b2ef |
+18
-2
@@ -2,11 +2,27 @@
|
|||||||
"theme": "auto",
|
"theme": "auto",
|
||||||
"token": "",
|
"token": "",
|
||||||
"server_url": "",
|
"server_url": "",
|
||||||
"server_port": "32400",
|
|
||||||
"server_scheme": "https",
|
"server_scheme": "https",
|
||||||
|
"server_port": "32400",
|
||||||
"timeout": 9,
|
"timeout": 9,
|
||||||
"library_name": "",
|
"library_name": "",
|
||||||
"sync_mode": "merge_local_primary",
|
"sync_mode": "merge_local_primary",
|
||||||
"local_path": "playlist",
|
"local_path": "playlist",
|
||||||
"path_rules": []
|
"path_rules": [],
|
||||||
|
"path_mapping": {
|
||||||
|
"mode": "SIMPLE",
|
||||||
|
"simple": [],
|
||||||
|
"regex": {
|
||||||
|
"local_pre": [],
|
||||||
|
"local_post": [],
|
||||||
|
"remote_pre": [],
|
||||||
|
"remote_post": []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"schedule_mode": "DISABLED",
|
||||||
|
"schedule_cron": "",
|
||||||
|
"schedule_daily_time": "02:00",
|
||||||
|
"schedule_weekly_days": [0],
|
||||||
|
"schedule_weekly_time": "03:00",
|
||||||
|
"schedule_auto_watch": false
|
||||||
}
|
}
|
||||||
+37
@@ -92,9 +92,29 @@ class RegexRule(BaseModel):
|
|||||||
replacement: str = ""
|
replacement: str = ""
|
||||||
|
|
||||||
|
|
||||||
|
class ReplacementRule(BaseModel):
|
||||||
|
id: str = ""
|
||||||
|
search: str
|
||||||
|
replace: str = ""
|
||||||
|
|
||||||
|
|
||||||
|
class RegexRulesGroup(BaseModel):
|
||||||
|
local_pre: list[ReplacementRule] = []
|
||||||
|
local_post: list[ReplacementRule] = []
|
||||||
|
remote_pre: list[ReplacementRule] = []
|
||||||
|
remote_post: list[ReplacementRule] = []
|
||||||
|
|
||||||
|
|
||||||
|
class PathMappingPayload(BaseModel):
|
||||||
|
mode: str = "SIMPLE"
|
||||||
|
simple: list[ReplacementRule] = []
|
||||||
|
regex: RegexRulesGroup = RegexRulesGroup()
|
||||||
|
|
||||||
|
|
||||||
class SyncSettingsResponse(BaseModel):
|
class SyncSettingsResponse(BaseModel):
|
||||||
sync_mode: str
|
sync_mode: str
|
||||||
path_rules: list[RegexRule]
|
path_rules: list[RegexRule]
|
||||||
|
path_mapping: dict | None = None
|
||||||
local_path: str
|
local_path: str
|
||||||
library_name: str | None = None
|
library_name: str | None = None
|
||||||
server_url: str | None = None
|
server_url: str | None = None
|
||||||
@@ -352,6 +372,7 @@ async def get_settings():
|
|||||||
return SyncSettingsResponse(
|
return SyncSettingsResponse(
|
||||||
sync_mode=server_config.sync_mode,
|
sync_mode=server_config.sync_mode,
|
||||||
path_rules=rules,
|
path_rules=rules,
|
||||||
|
path_mapping=server_config.path_mapping,
|
||||||
local_path=server_config.local_path,
|
local_path=server_config.local_path,
|
||||||
library_name=server_config.library_name,
|
library_name=server_config.library_name,
|
||||||
server_url=server_config.url,
|
server_url=server_config.url,
|
||||||
@@ -380,6 +401,22 @@ async def update_regex_rules(payload: RegexRulePayload):
|
|||||||
return {"rules": payload.rules}
|
return {"rules": payload.rules}
|
||||||
|
|
||||||
|
|
||||||
|
@app.put("/api/settings/path-mapping")
|
||||||
|
async def update_path_mapping(payload: PathMappingPayload):
|
||||||
|
path_mapping_dict = {
|
||||||
|
"mode": payload.mode,
|
||||||
|
"simple": [{"id": rule.id, "search": rule.search, "replace": rule.replace} for rule in payload.simple],
|
||||||
|
"regex": {
|
||||||
|
"local_pre": [{"id": rule.id, "search": rule.search, "replace": rule.replace} for rule in payload.regex.local_pre],
|
||||||
|
"local_post": [{"id": rule.id, "search": rule.search, "replace": rule.replace} for rule in payload.regex.local_post],
|
||||||
|
"remote_pre": [{"id": rule.id, "search": rule.search, "replace": rule.replace} for rule in payload.regex.remote_pre],
|
||||||
|
"remote_post": [{"id": rule.id, "search": rule.search, "replace": rule.replace} for rule in payload.regex.remote_post],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
server_config.set_and_save_config(path_mapping=path_mapping_dict)
|
||||||
|
return {"path_mapping": server_config.path_mapping}
|
||||||
|
|
||||||
|
|
||||||
@app.put("/api/settings/library")
|
@app.put("/api/settings/library")
|
||||||
async def update_library(payload: LibrarySelection):
|
async def update_library(payload: LibrarySelection):
|
||||||
server_config.set_and_save_config(library_name=payload.library_name)
|
server_config.set_and_save_config(library_name=payload.library_name)
|
||||||
|
|||||||
+48
-1
@@ -3,6 +3,16 @@ import os
|
|||||||
from app.utils.logger import logger
|
from app.utils.logger import logger
|
||||||
|
|
||||||
DEFAULT_SYNC_MODE = "merge_local_primary"
|
DEFAULT_SYNC_MODE = "merge_local_primary"
|
||||||
|
DEFAULT_PATH_MAPPING = {
|
||||||
|
"mode": "SIMPLE",
|
||||||
|
"simple": [],
|
||||||
|
"regex": {
|
||||||
|
"local_pre": [],
|
||||||
|
"local_post": [],
|
||||||
|
"remote_pre": [],
|
||||||
|
"remote_post": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
CONFIG_PATH = os.path.abspath(
|
CONFIG_PATH = os.path.abspath(
|
||||||
os.path.join(os.path.dirname(__file__), "..", "config.json")
|
os.path.join(os.path.dirname(__file__), "..", "config.json")
|
||||||
@@ -21,7 +31,8 @@ class ServerConfig:
|
|||||||
self.library_name = ""
|
self.library_name = ""
|
||||||
self.sync_mode = DEFAULT_SYNC_MODE
|
self.sync_mode = DEFAULT_SYNC_MODE
|
||||||
self.local_path = "playlist"
|
self.local_path = "playlist"
|
||||||
self.path_rules: list[dict[str, str]] = []
|
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_mode = "DISABLED"
|
||||||
self.schedule_cron = ""
|
self.schedule_cron = ""
|
||||||
self.schedule_daily_time = "02:00"
|
self.schedule_daily_time = "02:00"
|
||||||
@@ -55,6 +66,23 @@ class ServerConfig:
|
|||||||
self.sync_mode = config.get("sync_mode", DEFAULT_SYNC_MODE)
|
self.sync_mode = config.get("sync_mode", DEFAULT_SYNC_MODE)
|
||||||
self.local_path = config.get("local_path", "playlist")
|
self.local_path = config.get("local_path", "playlist")
|
||||||
self.path_rules = config.get("path_rules", []) or []
|
self.path_rules = config.get("path_rules", []) or []
|
||||||
|
|
||||||
|
# Load path_mapping with default fallback
|
||||||
|
path_mapping_config = config.get("path_mapping")
|
||||||
|
if path_mapping_config:
|
||||||
|
self.path_mapping = {
|
||||||
|
"mode": path_mapping_config.get("mode", "SIMPLE"),
|
||||||
|
"simple": path_mapping_config.get("simple", []),
|
||||||
|
"regex": {
|
||||||
|
"local_pre": path_mapping_config.get("regex", {}).get("local_pre", []),
|
||||||
|
"local_post": path_mapping_config.get("regex", {}).get("local_post", []),
|
||||||
|
"remote_pre": path_mapping_config.get("regex", {}).get("remote_pre", []),
|
||||||
|
"remote_post": path_mapping_config.get("regex", {}).get("remote_post", [])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
self.path_mapping = DEFAULT_PATH_MAPPING.copy()
|
||||||
|
|
||||||
self.schedule_mode = config.get("schedule_mode", "DISABLED")
|
self.schedule_mode = config.get("schedule_mode", "DISABLED")
|
||||||
self.schedule_cron = config.get("schedule_cron", "")
|
self.schedule_cron = config.get("schedule_cron", "")
|
||||||
self.schedule_daily_time = config.get("schedule_daily_time", "02:00")
|
self.schedule_daily_time = config.get("schedule_daily_time", "02:00")
|
||||||
@@ -76,6 +104,7 @@ class ServerConfig:
|
|||||||
"sync_mode": self.sync_mode,
|
"sync_mode": self.sync_mode,
|
||||||
"local_path": self.local_path,
|
"local_path": self.local_path,
|
||||||
"path_rules": self.path_rules,
|
"path_rules": self.path_rules,
|
||||||
|
"path_mapping": self.path_mapping,
|
||||||
"schedule_mode": self.schedule_mode,
|
"schedule_mode": self.schedule_mode,
|
||||||
"schedule_cron": self.schedule_cron,
|
"schedule_cron": self.schedule_cron,
|
||||||
"schedule_daily_time": self.schedule_daily_time,
|
"schedule_daily_time": self.schedule_daily_time,
|
||||||
@@ -121,6 +150,21 @@ class ServerConfig:
|
|||||||
def set_path_rules(self, path_rules: list[dict[str, str]]) -> None:
|
def set_path_rules(self, path_rules: list[dict[str, str]]) -> None:
|
||||||
self.path_rules = path_rules or []
|
self.path_rules = path_rules or []
|
||||||
|
|
||||||
|
def set_path_mapping(self, path_mapping: dict) -> None:
|
||||||
|
if path_mapping:
|
||||||
|
self.path_mapping = {
|
||||||
|
"mode": path_mapping.get("mode", "SIMPLE"),
|
||||||
|
"simple": path_mapping.get("simple", []),
|
||||||
|
"regex": {
|
||||||
|
"local_pre": path_mapping.get("regex", {}).get("local_pre", []),
|
||||||
|
"local_post": path_mapping.get("regex", {}).get("local_post", []),
|
||||||
|
"remote_pre": path_mapping.get("regex", {}).get("remote_pre", []),
|
||||||
|
"remote_post": path_mapping.get("regex", {}).get("remote_post", [])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
self.path_mapping = DEFAULT_PATH_MAPPING.copy()
|
||||||
|
|
||||||
def set_schedule(
|
def set_schedule(
|
||||||
self,
|
self,
|
||||||
mode: str,
|
mode: str,
|
||||||
@@ -150,6 +194,7 @@ class ServerConfig:
|
|||||||
sync_mode: str | None = None,
|
sync_mode: str | None = None,
|
||||||
local_path: str | None = None,
|
local_path: str | None = None,
|
||||||
path_rules: list[dict[str, str]] | None = None,
|
path_rules: list[dict[str, str]] | None = None,
|
||||||
|
path_mapping: dict | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
if theme is not None:
|
if theme is not None:
|
||||||
self.set_theme(theme)
|
self.set_theme(theme)
|
||||||
@@ -171,6 +216,8 @@ class ServerConfig:
|
|||||||
self.set_local_path(local_path)
|
self.set_local_path(local_path)
|
||||||
if path_rules is not None:
|
if path_rules is not None:
|
||||||
self.set_path_rules(path_rules)
|
self.set_path_rules(path_rules)
|
||||||
|
if path_mapping is not None:
|
||||||
|
self.set_path_mapping(path_mapping)
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+243
-13
@@ -40,6 +40,15 @@ class PlaylistSyncResult:
|
|||||||
output_dir: str
|
output_dir: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class CompiledRegexRules:
|
||||||
|
"""Holds compiled regex rules for all four processing stages."""
|
||||||
|
local_pre: list[tuple[re.Pattern[str], str]]
|
||||||
|
local_post: list[tuple[re.Pattern[str], str]]
|
||||||
|
remote_pre: list[tuple[re.Pattern[str], str]]
|
||||||
|
remote_post: list[tuple[re.Pattern[str], str]]
|
||||||
|
|
||||||
|
|
||||||
def load_paths(text: str) -> list[str]:
|
def load_paths(text: str) -> list[str]:
|
||||||
"""Normalize playlist text into a list of absolute paths.
|
"""Normalize playlist text into a list of absolute paths.
|
||||||
|
|
||||||
@@ -72,12 +81,21 @@ def save_paths(paths: Sequence[str]) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def _compile_regex_rules(rules: Sequence[dict[str, str]]) -> list[tuple[re.Pattern[str], str]]:
|
def _compile_regex_rules(rules: Sequence[dict[str, str]]) -> list[tuple[re.Pattern[str], str]]:
|
||||||
|
"""Compile regex rules into pattern/replacement pairs.
|
||||||
|
|
||||||
|
Supports both legacy format (pattern/replacement) and new format (search/replace).
|
||||||
|
"""
|
||||||
compiled: list[tuple[re.Pattern[str], str]] = []
|
compiled: list[tuple[re.Pattern[str], str]] = []
|
||||||
for rule in rules:
|
for rule in rules:
|
||||||
pattern = rule.get("pattern")
|
# Support both legacy (pattern/replacement) and new (search/replace) field names
|
||||||
|
# Use explicit None checks to allow empty strings as valid values
|
||||||
|
pattern = rule.get("pattern") if rule.get("pattern") is not None else rule.get("search")
|
||||||
if not pattern:
|
if not pattern:
|
||||||
continue
|
continue
|
||||||
replacement = rule.get("replacement", "")
|
# For replacement, empty string is a valid value (for deletion)
|
||||||
|
replacement = rule.get("replacement") if rule.get("replacement") is not None else rule.get("replace")
|
||||||
|
if replacement is None:
|
||||||
|
replacement = ""
|
||||||
try:
|
try:
|
||||||
compiled.append((re.compile(pattern), replacement))
|
compiled.append((re.compile(pattern), replacement))
|
||||||
except re.error as exc:
|
except re.error as exc:
|
||||||
@@ -234,9 +252,31 @@ def _merge_chunks(
|
|||||||
return chunks
|
return chunks
|
||||||
|
|
||||||
|
|
||||||
def _write_results(merged_lines: Sequence[str], folder: str) -> None:
|
def _write_results(
|
||||||
_save_playlist_to_folder("local_result.m3u8", merged_lines, folder)
|
merged_lines: Sequence[str],
|
||||||
_save_playlist_to_folder("remote_result.m3u8", merged_lines, folder)
|
folder: str,
|
||||||
|
compiled_rules: CompiledRegexRules | None = None
|
||||||
|
) -> None:
|
||||||
|
"""Write sync results to the test folder.
|
||||||
|
|
||||||
|
If compiled_rules is provided with post-processing rules:
|
||||||
|
- local_result.m3u8: merged_lines processed with local_post rules
|
||||||
|
- remote_result.m3u8: merged_lines processed with remote_post rules
|
||||||
|
- base_next.m3u8: unprocessed merged_lines (normalized sync result)
|
||||||
|
"""
|
||||||
|
# Apply post-processing regex rules if provided
|
||||||
|
if compiled_rules and compiled_rules.local_post:
|
||||||
|
local_lines = _apply_compiled_rules_to_paths(merged_lines, compiled_rules.local_post)
|
||||||
|
else:
|
||||||
|
local_lines = list(merged_lines)
|
||||||
|
|
||||||
|
if compiled_rules and compiled_rules.remote_post:
|
||||||
|
remote_lines = _apply_compiled_rules_to_paths(merged_lines, compiled_rules.remote_post)
|
||||||
|
else:
|
||||||
|
remote_lines = list(merged_lines)
|
||||||
|
|
||||||
|
_save_playlist_to_folder("local_result.m3u8", local_lines, folder)
|
||||||
|
_save_playlist_to_folder("remote_result.m3u8", remote_lines, folder)
|
||||||
_save_playlist_to_folder("base_next.m3u8", merged_lines, folder)
|
_save_playlist_to_folder("base_next.m3u8", merged_lines, folder)
|
||||||
|
|
||||||
|
|
||||||
@@ -379,12 +419,16 @@ def merge_playlists(
|
|||||||
remote_text: str,
|
remote_text: str,
|
||||||
strategy: ConflictResolutionStrategy = ConflictResolutionStrategy.LOCAL_PRIORITY,
|
strategy: ConflictResolutionStrategy = ConflictResolutionStrategy.LOCAL_PRIORITY,
|
||||||
test_folder: str = TEST_PLAYLIST_DIR,
|
test_folder: str = TEST_PLAYLIST_DIR,
|
||||||
|
compiled_rules: CompiledRegexRules | None = None,
|
||||||
) -> MergeResult:
|
) -> MergeResult:
|
||||||
"""Merge playlists using diff3 and resolve conflicts per strategy.
|
"""Merge playlists using diff3 and resolve conflicts per strategy.
|
||||||
|
|
||||||
The base, local, and remote normalized playlists are saved into ``test_folder``
|
The base, local, and remote normalized playlists are saved into ``test_folder``
|
||||||
for inspection. The merged playlist is also stored twice to simulate the
|
for inspection. The merged playlist is also stored twice to simulate the
|
||||||
versions intended for local save and cloud upload.
|
versions intended for local save and cloud upload.
|
||||||
|
|
||||||
|
If compiled_rules is provided, post-processing regex rules will be applied
|
||||||
|
to the results before writing.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
base_paths, local_paths, remote_paths = _normalize_inputs(
|
base_paths, local_paths, remote_paths = _normalize_inputs(
|
||||||
@@ -420,7 +464,7 @@ def merge_playlists(
|
|||||||
merged_lines, base_paths, local_paths, remote_paths
|
merged_lines, base_paths, local_paths, remote_paths
|
||||||
)
|
)
|
||||||
|
|
||||||
_write_results(merged_lines, test_folder)
|
_write_results(merged_lines, test_folder, compiled_rules)
|
||||||
|
|
||||||
return MergeResult(merged_paths=merged_lines, conflicts=conflicts)
|
return MergeResult(merged_paths=merged_lines, conflicts=conflicts)
|
||||||
|
|
||||||
@@ -517,6 +561,7 @@ def _sync_single_playlist(
|
|||||||
remote_text: str,
|
remote_text: str,
|
||||||
playlist_folder: str,
|
playlist_folder: str,
|
||||||
remote_present: bool,
|
remote_present: bool,
|
||||||
|
compiled_rules: CompiledRegexRules | None = None,
|
||||||
) -> PlaylistSyncResult:
|
) -> PlaylistSyncResult:
|
||||||
local_present = local_text is not None
|
local_present = local_text is not None
|
||||||
local_text = local_text or ""
|
local_text = local_text or ""
|
||||||
@@ -535,7 +580,7 @@ def _sync_single_playlist(
|
|||||||
base_text, local_text, remote_text, playlist_folder
|
base_text, local_text, remote_text, playlist_folder
|
||||||
)
|
)
|
||||||
merged_lines = list(local_paths)
|
merged_lines = list(local_paths)
|
||||||
_write_results(merged_lines, playlist_folder)
|
_write_results(merged_lines, playlist_folder, compiled_rules)
|
||||||
return PlaylistSyncResult(playlist, merged_lines, [], "synced", playlist_folder)
|
return PlaylistSyncResult(playlist, merged_lines, [], "synced", playlist_folder)
|
||||||
|
|
||||||
if mode == SyncMode.REMOTE_FORCE:
|
if mode == SyncMode.REMOTE_FORCE:
|
||||||
@@ -547,7 +592,7 @@ def _sync_single_playlist(
|
|||||||
base_text, local_text, remote_text, playlist_folder
|
base_text, local_text, remote_text, playlist_folder
|
||||||
)
|
)
|
||||||
merged_lines = list(remote_paths)
|
merged_lines = list(remote_paths)
|
||||||
_write_results(merged_lines, playlist_folder)
|
_write_results(merged_lines, playlist_folder, compiled_rules)
|
||||||
return PlaylistSyncResult(playlist, merged_lines, [], "synced", playlist_folder)
|
return PlaylistSyncResult(playlist, merged_lines, [], "synced", playlist_folder)
|
||||||
|
|
||||||
if mode not in (SyncMode.MERGE_LOCAL_PRIMARY, SyncMode.MERGE_REMOTE_PRIMARY):
|
if mode not in (SyncMode.MERGE_LOCAL_PRIMARY, SyncMode.MERGE_REMOTE_PRIMARY):
|
||||||
@@ -565,6 +610,7 @@ def _sync_single_playlist(
|
|||||||
remote_text=remote_text,
|
remote_text=remote_text,
|
||||||
strategy=merge_strategy,
|
strategy=merge_strategy,
|
||||||
test_folder=playlist_folder,
|
test_folder=playlist_folder,
|
||||||
|
compiled_rules=compiled_rules,
|
||||||
)
|
)
|
||||||
|
|
||||||
if not merge_result.merged_paths and (not local_present or not remote_present):
|
if not merge_result.merged_paths and (not local_present or not remote_present):
|
||||||
@@ -578,13 +624,177 @@ def _sync_single_playlist(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _compile_path_mapping_rules(path_mapping: dict) -> CompiledRegexRules:
|
||||||
|
"""Compile regex rules from path_mapping config for all four processing stages."""
|
||||||
|
regex_config = path_mapping.get("regex", {})
|
||||||
|
return CompiledRegexRules(
|
||||||
|
local_pre=_compile_regex_rules(regex_config.get("local_pre", [])),
|
||||||
|
local_post=_compile_regex_rules(regex_config.get("local_post", [])),
|
||||||
|
remote_pre=_compile_regex_rules(regex_config.get("remote_pre", [])),
|
||||||
|
remote_post=_compile_regex_rules(regex_config.get("remote_post", [])),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _compile_simple_mapping_rules(simple_mappings: list[dict]) -> CompiledRegexRules:
|
||||||
|
"""Compile simple mapping pairs into four rule groups using UUID-based mapping_ids.
|
||||||
|
|
||||||
|
Each simple mapping has:
|
||||||
|
- id: UUID used as the mapping_id (unique identifier to prevent conflicts)
|
||||||
|
- search: Local path prefix
|
||||||
|
- replace: Cloud path prefix
|
||||||
|
|
||||||
|
This generates four rule sets:
|
||||||
|
- local_pre: Replace local path (search) with mapping_id
|
||||||
|
- remote_pre: Replace cloud path (replace) with mapping_id
|
||||||
|
- local_post: Replace mapping_id with local path (search)
|
||||||
|
- remote_post: Replace mapping_id with cloud path (replace)
|
||||||
|
|
||||||
|
The mapping_id is wrapped with special markers to prevent conflicts with actual paths.
|
||||||
|
"""
|
||||||
|
local_pre_rules: list[dict[str, str]] = []
|
||||||
|
local_post_rules: list[dict[str, str]] = []
|
||||||
|
remote_pre_rules: list[dict[str, str]] = []
|
||||||
|
remote_post_rules: list[dict[str, str]] = []
|
||||||
|
|
||||||
|
# UUID pattern for validation (accepts standard UUID format with or without hyphens)
|
||||||
|
uuid_pattern = re.compile(r'^[a-fA-F0-9]{8}-?[a-fA-F0-9]{4}-?[a-fA-F0-9]{4}-?[a-fA-F0-9]{4}-?[a-fA-F0-9]{12}$')
|
||||||
|
|
||||||
|
for mapping in simple_mappings:
|
||||||
|
# Get the mapping values
|
||||||
|
mapping_id = mapping.get("id")
|
||||||
|
local_path = mapping.get("search", "") # Local path is stored in 'search' field
|
||||||
|
cloud_path = mapping.get("replace", "") # Cloud path is stored in 'replace' field
|
||||||
|
|
||||||
|
# Validate mapping_id is a proper UUID to prevent injection attacks
|
||||||
|
if not mapping_id or not isinstance(mapping_id, str):
|
||||||
|
logger.warning(f"Skipping mapping with missing or invalid id: {mapping}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not uuid_pattern.match(mapping_id):
|
||||||
|
logger.warning(f"Skipping mapping with non-UUID id format: {mapping_id}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Paths must be non-empty strings
|
||||||
|
if not local_path or not isinstance(local_path, str):
|
||||||
|
logger.warning(f"Skipping mapping with missing local path: {mapping}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not cloud_path or not isinstance(cloud_path, str):
|
||||||
|
logger.warning(f"Skipping mapping with missing cloud path: {mapping}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Normalize Windows paths: Replace double backslashes with single backslashes
|
||||||
|
# This handles cases where users enter escaped paths like \\Koha9-Main\\Music
|
||||||
|
# when the actual playlist content uses \Koha9-Main\Music
|
||||||
|
original_local = local_path
|
||||||
|
original_cloud = cloud_path
|
||||||
|
local_path = local_path.replace("\\\\", "\\")
|
||||||
|
cloud_path = cloud_path.replace("\\\\", "\\")
|
||||||
|
|
||||||
|
if local_path != original_local or cloud_path != original_cloud:
|
||||||
|
logger.info(f"Normalized Windows paths:")
|
||||||
|
logger.info(f" Local: {repr(original_local)} -> {repr(local_path)}")
|
||||||
|
logger.info(f" Cloud: {repr(original_cloud)} -> {repr(cloud_path)}")
|
||||||
|
|
||||||
|
# Create a unique placeholder using the validated UUID
|
||||||
|
# Using special markers to prevent conflicts with actual paths
|
||||||
|
placeholder = f"__MAPPING__{mapping_id}__"
|
||||||
|
|
||||||
|
# Debug logging for path mapping
|
||||||
|
logger.debug(f"Simple mapping pair:")
|
||||||
|
logger.debug(f" Local path (search): {repr(local_path)}")
|
||||||
|
logger.debug(f" Cloud path (replace): {repr(cloud_path)}")
|
||||||
|
logger.debug(f" Placeholder: {placeholder}")
|
||||||
|
|
||||||
|
# Pre-processing rules (use re.escape to treat paths as literal strings)
|
||||||
|
# local_pre: Replace local path with placeholder
|
||||||
|
local_pattern = re.escape(local_path)
|
||||||
|
logger.debug(f" Local pre pattern: {repr(local_pattern)}")
|
||||||
|
local_pre_rules.append({
|
||||||
|
"pattern": local_pattern,
|
||||||
|
"replacement": placeholder
|
||||||
|
})
|
||||||
|
|
||||||
|
# remote_pre: Replace cloud path with placeholder
|
||||||
|
remote_pattern = re.escape(cloud_path)
|
||||||
|
logger.debug(f" Remote pre pattern: {repr(remote_pattern)}")
|
||||||
|
remote_pre_rules.append({
|
||||||
|
"pattern": remote_pattern,
|
||||||
|
"replacement": placeholder
|
||||||
|
})
|
||||||
|
|
||||||
|
# Post-processing rules
|
||||||
|
# local_post: Replace placeholder with local path
|
||||||
|
# Note: In regex replacement, backslashes need to be escaped
|
||||||
|
local_post_rules.append({
|
||||||
|
"pattern": re.escape(placeholder),
|
||||||
|
"replacement": local_path.replace("\\", "\\\\")
|
||||||
|
})
|
||||||
|
|
||||||
|
# remote_post: Replace placeholder with cloud path
|
||||||
|
remote_post_rules.append({
|
||||||
|
"pattern": re.escape(placeholder),
|
||||||
|
"replacement": cloud_path.replace("\\", "\\\\")
|
||||||
|
})
|
||||||
|
|
||||||
|
logger.info(f"Compiled {len(local_pre_rules)} simple mapping pairs into rules")
|
||||||
|
|
||||||
|
return CompiledRegexRules(
|
||||||
|
local_pre=_compile_regex_rules(local_pre_rules),
|
||||||
|
local_post=_compile_regex_rules(local_post_rules),
|
||||||
|
remote_pre=_compile_regex_rules(remote_pre_rules),
|
||||||
|
remote_post=_compile_regex_rules(remote_post_rules),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def sync_all_playlists(
|
def sync_all_playlists(
|
||||||
local_dir: str, mode: SyncMode, test_folder: str = TEST_PLAYLIST_DIR
|
local_dir: str, mode: SyncMode, test_folder: str = TEST_PLAYLIST_DIR
|
||||||
) -> list[PlaylistSyncResult]:
|
) -> list[PlaylistSyncResult]:
|
||||||
"""Synchronize all playlists that can be matched by name."""
|
"""Synchronize all playlists that can be matched by name.
|
||||||
|
|
||||||
|
Path mapping modes:
|
||||||
|
- SIMPLE: Uses UUID-based mapping_ids to convert between local and cloud paths
|
||||||
|
- local_pre: local_path -> mapping_id
|
||||||
|
- remote_pre: cloud_path -> mapping_id
|
||||||
|
- local_post: mapping_id -> local_path
|
||||||
|
- remote_post: mapping_id -> cloud_path
|
||||||
|
|
||||||
|
- REGEX: Uses custom regex rules for each processing stage
|
||||||
|
- local_pre, local_post, remote_pre, remote_post rules are applied directly
|
||||||
|
|
||||||
|
Processing flow:
|
||||||
|
1. local_pre rules are applied to local playlists before sync
|
||||||
|
2. remote_pre rules are applied to remote playlists before sync
|
||||||
|
3. Sync/merge is performed
|
||||||
|
4. local_post rules are applied to results before writing to local_result.m3u8
|
||||||
|
5. remote_post rules are applied to results before writing to remote_result.m3u8
|
||||||
|
"""
|
||||||
|
|
||||||
server_config.load()
|
server_config.load()
|
||||||
compiled_rules = _compile_regex_rules(server_config.path_rules)
|
|
||||||
|
# Get path_mapping configuration
|
||||||
|
path_mapping = server_config.path_mapping
|
||||||
|
mapping_mode = path_mapping.get("mode", "SIMPLE")
|
||||||
|
|
||||||
|
# Compile rules based on the mode
|
||||||
|
compiled_rules: CompiledRegexRules | None = None
|
||||||
|
legacy_compiled_rules: list[tuple[re.Pattern[str], str]] = []
|
||||||
|
|
||||||
|
if mapping_mode == "REGEX":
|
||||||
|
compiled_rules = _compile_path_mapping_rules(path_mapping)
|
||||||
|
logger.info("Using REGEX mode for path mapping with 4 rule groups")
|
||||||
|
elif mapping_mode == "SIMPLE":
|
||||||
|
simple_mappings = path_mapping.get("simple", [])
|
||||||
|
if simple_mappings:
|
||||||
|
compiled_rules = _compile_simple_mapping_rules(simple_mappings)
|
||||||
|
logger.info(f"Using SIMPLE mode for path mapping with {len(simple_mappings)} mapping pairs")
|
||||||
|
else:
|
||||||
|
logger.info("SIMPLE mode with no mappings - no path transformations will be applied")
|
||||||
|
else:
|
||||||
|
# Use legacy path_rules for backward compatibility
|
||||||
|
legacy_compiled_rules = _compile_regex_rules(server_config.path_rules)
|
||||||
|
logger.info("Using legacy path_rules for preprocessing")
|
||||||
|
|
||||||
_ensure_test_dir(test_folder)
|
_ensure_test_dir(test_folder)
|
||||||
logger.info(f"Syncing playlists to test folder: {test_folder}")
|
logger.info(f"Syncing playlists to test folder: {test_folder}")
|
||||||
local_playlists = _load_local_playlists(local_dir)
|
local_playlists = _load_local_playlists(local_dir)
|
||||||
@@ -613,15 +823,34 @@ def sync_all_playlists(
|
|||||||
remote_text = snapshot_remote_text
|
remote_text = snapshot_remote_text
|
||||||
remote_present = bool(remote_text.strip()) or remote_exists
|
remote_present = bool(remote_text.strip()) or remote_exists
|
||||||
|
|
||||||
|
if compiled_rules:
|
||||||
|
# Apply pre-processing rules for REGEX or SIMPLE mode
|
||||||
|
# base_text doesn't need pre-processing as it's the normalized state
|
||||||
|
if local_text is not None and compiled_rules.local_pre:
|
||||||
|
logger.debug(f"Applying local_pre rules to playlist: {playlist}")
|
||||||
|
logger.debug(f" Before preprocessing (first 200 chars): {repr(local_text[:200])}")
|
||||||
|
local_text = preprocess_playlist_text(
|
||||||
|
local_text, [], compiled_rules.local_pre
|
||||||
|
)
|
||||||
|
logger.debug(f" After preprocessing (first 200 chars): {repr(local_text[:200])}")
|
||||||
|
if remote_text and compiled_rules.remote_pre:
|
||||||
|
logger.debug(f"Applying remote_pre rules to playlist: {playlist}")
|
||||||
|
logger.debug(f" Before preprocessing (first 200 chars): {repr(remote_text[:200])}")
|
||||||
|
remote_text = preprocess_playlist_text(
|
||||||
|
remote_text, [], compiled_rules.remote_pre
|
||||||
|
)
|
||||||
|
logger.debug(f" After preprocessing (first 200 chars): {repr(remote_text[:200])}")
|
||||||
|
elif legacy_compiled_rules:
|
||||||
|
# Use legacy preprocessing for all texts
|
||||||
base_text = preprocess_playlist_text(
|
base_text = preprocess_playlist_text(
|
||||||
base_text, server_config.path_rules, compiled_rules
|
base_text, server_config.path_rules, legacy_compiled_rules
|
||||||
)
|
)
|
||||||
remote_text = preprocess_playlist_text(
|
remote_text = preprocess_playlist_text(
|
||||||
remote_text, server_config.path_rules, compiled_rules
|
remote_text, server_config.path_rules, legacy_compiled_rules
|
||||||
)
|
)
|
||||||
if local_text is not None:
|
if local_text is not None:
|
||||||
local_text = preprocess_playlist_text(
|
local_text = preprocess_playlist_text(
|
||||||
local_text, server_config.path_rules, compiled_rules
|
local_text, server_config.path_rules, legacy_compiled_rules
|
||||||
)
|
)
|
||||||
|
|
||||||
# Treat missing remote text as absent playlist.
|
# Treat missing remote text as absent playlist.
|
||||||
@@ -633,6 +862,7 @@ def sync_all_playlists(
|
|||||||
remote_text=remote_text,
|
remote_text=remote_text,
|
||||||
playlist_folder=playlist_folder,
|
playlist_folder=playlist_folder,
|
||||||
remote_present=remote_present,
|
remote_present=remote_present,
|
||||||
|
compiled_rules=compiled_rules,
|
||||||
)
|
)
|
||||||
results.append(result)
|
results.append(result)
|
||||||
|
|
||||||
|
|||||||
+76
-20
@@ -1,6 +1,5 @@
|
|||||||
|
|
||||||
import React, { useEffect, useState, useCallback, useRef } from 'react';
|
import React, { useEffect, useState, useCallback, useRef } from 'react';
|
||||||
import { Playlist, ServerType, SyncStrategy, PlexServerConnection, RegexReplacement, PlexConnectionSettings, SyncState, ScheduleSettings, ScheduleMode } from './types';
|
import { Playlist, ServerType, SyncStrategy, PlexServerConnection, PathMappingConfig, PathMappingMode, PlexConnectionSettings, SyncState, ScheduleSettings, ScheduleMode } from './types';
|
||||||
import { apiService } from './services/api';
|
import { apiService } from './services/api';
|
||||||
import {
|
import {
|
||||||
STRIPE_BASE_SPEED,
|
STRIPE_BASE_SPEED,
|
||||||
@@ -10,15 +9,13 @@ import {
|
|||||||
SYNC_SUCCESS_TOTAL_MS,
|
SYNC_SUCCESS_TOTAL_MS,
|
||||||
SYNC_ERROR_RESET_MS,
|
SYNC_ERROR_RESET_MS,
|
||||||
TOAST_AUTO_DISMISS_MS,
|
TOAST_AUTO_DISMISS_MS,
|
||||||
TOAST_EXIT_DURATION_MS,
|
TOAST_EXIT_DURATION_MS
|
||||||
SYNC_BANNER_PADDING_X,
|
|
||||||
SYNC_BANNER_PADDING_Y,
|
|
||||||
SYNC_BANNER_MIN_WIDTH,
|
|
||||||
} from './Config';
|
} from './Config';
|
||||||
|
import { SYNC_BANNER_PADDING_X, SYNC_BANNER_PADDING_Y, SYNC_BANNER_MIN_WIDTH } from './Config';
|
||||||
import ServerPanel from './components/ServerPanel';
|
import ServerPanel from './components/ServerPanel';
|
||||||
import StrategySelector from './components/StrategySelector';
|
import StrategySelector from './components/StrategySelector';
|
||||||
import ConnectionModal from './components/ConnectionModal';
|
import ConnectionModal from './components/ConnectionModal';
|
||||||
import { ArrowLeftRight, ShieldCheck, X, Server, ServerOff, Clock, Eye, EyeOff } from 'lucide-react';
|
import { ArrowLeftRight, ShieldCheck, X, Server, ServerOff, Clock, Eye, EyeOff, Type, Code2 } from 'lucide-react';
|
||||||
|
|
||||||
interface Toast {
|
interface Toast {
|
||||||
id: number;
|
id: number;
|
||||||
@@ -141,8 +138,17 @@ const App: React.FC = () => {
|
|||||||
// Strategy State
|
// Strategy State
|
||||||
const [currentStrategy, setCurrentStrategy] = useState<SyncStrategy>(SyncStrategy.LOCAL_OVERWRITE);
|
const [currentStrategy, setCurrentStrategy] = useState<SyncStrategy>(SyncStrategy.LOCAL_OVERWRITE);
|
||||||
|
|
||||||
// Regex State
|
// Path Mapping State (Includes Simple and Regex Rules)
|
||||||
const [regexReplacements, setRegexReplacements] = useState<RegexReplacement[]>([]);
|
const [pathMappingConfig, setPathMappingConfig] = useState<PathMappingConfig>({
|
||||||
|
mode: PathMappingMode.SIMPLE,
|
||||||
|
simple: [],
|
||||||
|
regex: {
|
||||||
|
localPre: [],
|
||||||
|
localPost: [],
|
||||||
|
remotePre: [],
|
||||||
|
remotePost: []
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Schedule State
|
// Schedule State
|
||||||
const [scheduleSettings, setScheduleSettings] = useState<ScheduleSettings>({
|
const [scheduleSettings, setScheduleSettings] = useState<ScheduleSettings>({
|
||||||
@@ -226,7 +232,7 @@ const App: React.FC = () => {
|
|||||||
const result = await apiService.getSettings();
|
const result = await apiService.getSettings();
|
||||||
if (result.status === 'success') {
|
if (result.status === 'success') {
|
||||||
setCurrentStrategy(result.data.strategy);
|
setCurrentStrategy(result.data.strategy);
|
||||||
setRegexReplacements(result.data.regex);
|
setPathMappingConfig(result.data.pathMapping);
|
||||||
setLocalPath(result.data.localPath || 'playlist');
|
setLocalPath(result.data.localPath || 'playlist');
|
||||||
setConnectionSettings(result.data.connection);
|
setConnectionSettings(result.data.connection);
|
||||||
}
|
}
|
||||||
@@ -347,14 +353,14 @@ const App: React.FC = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle Regex Save
|
// Handle Path Mapping Save
|
||||||
const handleSaveRegex = async (replacements: RegexReplacement[]) => {
|
const handleSavePathMapping = async (config: PathMappingConfig) => {
|
||||||
setRegexReplacements(replacements);
|
setPathMappingConfig(config);
|
||||||
const result = await apiService.saveRegexRules(replacements);
|
const result = await apiService.savePathMapping(config);
|
||||||
if (result.status === 'success') {
|
if (result.status === 'success') {
|
||||||
addToast('Regex preprocessing rules have been saved.');
|
addToast('Path mapping rules have been saved.');
|
||||||
} else {
|
} else {
|
||||||
addToast(result.message || 'Failed to save regex rules.');
|
addToast(result.message || 'Failed to save path mapping rules.');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -365,7 +371,7 @@ const App: React.FC = () => {
|
|||||||
setSyncState(SyncState.SYNCING);
|
setSyncState(SyncState.SYNCING);
|
||||||
manualSyncInProgress.current = true;
|
manualSyncInProgress.current = true;
|
||||||
|
|
||||||
const result = await apiService.syncPlaylists(currentStrategy, regexReplacements, localPath || undefined);
|
const result = await apiService.syncPlaylists(currentStrategy, pathMappingConfig, localPath || undefined);
|
||||||
|
|
||||||
manualSyncInProgress.current = false;
|
manualSyncInProgress.current = false;
|
||||||
|
|
||||||
@@ -513,6 +519,44 @@ const App: React.FC = () => {
|
|||||||
|
|
||||||
const scheduleInfo = getScheduleDisplayInfo();
|
const scheduleInfo = getScheduleDisplayInfo();
|
||||||
|
|
||||||
|
// Helper: Calculate Path Mapping Info
|
||||||
|
const getPathMappingDisplayInfo = (config: PathMappingConfig) => {
|
||||||
|
let count = 0;
|
||||||
|
let modeLabel = '';
|
||||||
|
let Icon = Type;
|
||||||
|
|
||||||
|
if (config.mode === PathMappingMode.SIMPLE) {
|
||||||
|
modeLabel = 'Simple';
|
||||||
|
count = config.simple.length;
|
||||||
|
Icon = Type;
|
||||||
|
} else {
|
||||||
|
modeLabel = 'Regex';
|
||||||
|
count = config.regex.localPre.length +
|
||||||
|
config.regex.localPost.length +
|
||||||
|
config.regex.remotePre.length +
|
||||||
|
config.regex.remotePost.length;
|
||||||
|
Icon = Code2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count === 0) {
|
||||||
|
return {
|
||||||
|
label: 'Path Mapping',
|
||||||
|
value: 'Not Set',
|
||||||
|
active: false,
|
||||||
|
Icon: Icon
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
label: 'Path Mapping',
|
||||||
|
value: `${modeLabel} (${count})`,
|
||||||
|
active: true,
|
||||||
|
Icon: Icon
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const pathMappingInfo = getPathMappingDisplayInfo(pathMappingConfig);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen flex flex-col bg-gray-900 text-gray-100 font-sans overflow-hidden bg-[radial-gradient(ellipse_at_top,_var(--tw-gradient-stops))] from-gray-800 via-gray-900 to-black">
|
<div className="min-h-screen flex flex-col bg-gray-900 text-gray-100 font-sans overflow-hidden bg-[radial-gradient(ellipse_at_top,_var(--tw-gradient-stops))] from-gray-800 via-gray-900 to-black">
|
||||||
|
|
||||||
@@ -574,7 +618,7 @@ const App: React.FC = () => {
|
|||||||
|
|
||||||
{syncState === SyncState.IDLE ? (
|
{syncState === SyncState.IDLE ? (
|
||||||
<>
|
<>
|
||||||
{/* Normal Toolbar */}
|
{/* Normal Toolbar Left */}
|
||||||
<div className="flex items-center space-x-3">
|
<div className="flex items-center space-x-3">
|
||||||
<div className="p-1.5 rounded-lg shadow-lg bg-gradient-to-br from-plex-orange to-yellow-600 text-gray-900 shadow-plex-orange/20">
|
<div className="p-1.5 rounded-lg shadow-lg bg-gradient-to-br from-plex-orange to-yellow-600 text-gray-900 shadow-plex-orange/20">
|
||||||
<ArrowLeftRight size={24} strokeWidth={2.5} />
|
<ArrowLeftRight size={24} strokeWidth={2.5} />
|
||||||
@@ -586,6 +630,17 @@ const App: React.FC = () => {
|
|||||||
|
|
||||||
{/* Normal Toolbar Right */}
|
{/* Normal Toolbar Right */}
|
||||||
<div className="flex items-center gap-4">
|
<div className="flex items-center gap-4">
|
||||||
|
{/* Path Mapping Info */}
|
||||||
|
<div className="flex flex-col items-end hidden md:flex border-r border-gray-700/50 pr-4 mr-1">
|
||||||
|
<span className="text-[10px] uppercase font-bold text-gray-500 tracking-wider">
|
||||||
|
{pathMappingInfo.label}
|
||||||
|
</span>
|
||||||
|
<div className={`text-xs font-mono flex items-center gap-1.5 ${pathMappingInfo.active ? 'text-plex-orange' : 'text-gray-600'}`}>
|
||||||
|
{pathMappingInfo.active && <pathMappingInfo.Icon size={12} />}
|
||||||
|
<span>{pathMappingInfo.value}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
{/* Schedule Info */}
|
{/* Schedule Info */}
|
||||||
<div className="flex flex-col items-end mr-2 md:mr-0 hidden md:flex">
|
<div className="flex flex-col items-end mr-2 md:mr-0 hidden md:flex">
|
||||||
<span className="text-[10px] uppercase font-bold text-gray-500 tracking-wider">
|
<span className="text-[10px] uppercase font-bold text-gray-500 tracking-wider">
|
||||||
@@ -625,6 +680,7 @@ const App: React.FC = () => {
|
|||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
|
/* Syncing / Success Text Banner */
|
||||||
<div className="absolute inset-0 flex items-center justify-center pointer-events-none z-10">
|
<div className="absolute inset-0 flex items-center justify-center pointer-events-none z-10">
|
||||||
<div
|
<div
|
||||||
className="bg-black shadow-none rounded-none border-none"
|
className="bg-black shadow-none rounded-none border-none"
|
||||||
@@ -691,8 +747,8 @@ const App: React.FC = () => {
|
|||||||
<StrategySelector
|
<StrategySelector
|
||||||
currentStrategy={currentStrategy}
|
currentStrategy={currentStrategy}
|
||||||
onSelect={handleStrategyChange}
|
onSelect={handleStrategyChange}
|
||||||
savedRegexReplacements={regexReplacements}
|
savedPathMapping={pathMappingConfig}
|
||||||
onSaveRegex={handleSaveRegex}
|
onSavePathMapping={handleSavePathMapping}
|
||||||
savedSchedule={scheduleSettings}
|
savedSchedule={scheduleSettings}
|
||||||
onSaveSchedule={handleSaveSchedule}
|
onSaveSchedule={handleSaveSchedule}
|
||||||
syncState={syncState}
|
syncState={syncState}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import React, { useState, useRef, useEffect } from 'react';
|
import React, { useState, useRef, useEffect } from 'react';
|
||||||
import { SyncStrategy, RegexReplacement, SyncState, ScheduleSettings, ScheduleMode } from '../types';
|
import { SyncStrategy, ReplacementRule, PathMappingConfig, PathMappingRules, PathMappingMode, SyncState, ScheduleSettings, ScheduleMode } from '../types';
|
||||||
import {
|
import {
|
||||||
ArrowRightCircle,
|
ArrowRightCircle,
|
||||||
ArrowLeftCircle,
|
ArrowLeftCircle,
|
||||||
@@ -17,9 +17,25 @@ import {
|
|||||||
Clock,
|
Clock,
|
||||||
Repeat,
|
Repeat,
|
||||||
CheckSquare,
|
CheckSquare,
|
||||||
Square
|
Square,
|
||||||
|
Type,
|
||||||
|
Code2
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
|
|
||||||
|
// Generate a UUID for mapping rules
|
||||||
|
const generateUUID = (): string => {
|
||||||
|
// Use crypto.randomUUID() if available (modern browsers)
|
||||||
|
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
|
||||||
|
return crypto.randomUUID();
|
||||||
|
}
|
||||||
|
// Fallback to manual UUID v4 generation
|
||||||
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
||||||
|
const r = Math.random() * 16 | 0;
|
||||||
|
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
||||||
|
return v.toString(16);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
interface StrategyOption {
|
interface StrategyOption {
|
||||||
value: SyncStrategy;
|
value: SyncStrategy;
|
||||||
label: string;
|
label: string;
|
||||||
@@ -61,6 +77,29 @@ const STRATEGIES: StrategyOption[] = [
|
|||||||
|
|
||||||
const WEEK_DAYS = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
|
const WEEK_DAYS = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
|
||||||
|
|
||||||
|
// Color Theme Variables for Mapping Editors
|
||||||
|
const MAPPING_THEME = {
|
||||||
|
// Container Themes
|
||||||
|
local: {
|
||||||
|
borderColor: "border-blue-500/20",
|
||||||
|
bgColor: "bg-blue-900/10"
|
||||||
|
},
|
||||||
|
remote: {
|
||||||
|
borderColor: "border-green-500/20",
|
||||||
|
bgColor: "bg-green-900/10"
|
||||||
|
},
|
||||||
|
simple: {
|
||||||
|
borderColor: "border-gray-700/50",
|
||||||
|
bgColor: "bg-gray-900/40"
|
||||||
|
},
|
||||||
|
// Input Field Themes
|
||||||
|
inputs: {
|
||||||
|
default: "bg-gray-800 border-gray-700 text-gray-200 focus:border-plex-orange placeholder-gray-600",
|
||||||
|
local: "bg-blue-500/10 border-blue-500/30 text-blue-100 focus:border-blue-400 placeholder-blue-300/30",
|
||||||
|
cloud: "bg-green-500/10 border-green-500/30 text-green-100 focus:border-green-400 placeholder-green-300/30"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Helper to determine the actual mode and settings that would be saved based on the current UI state
|
// Helper to determine the actual mode and settings that would be saved based on the current UI state
|
||||||
const deriveEffectiveSchedule = (schedule: ScheduleSettings, tab: ScheduleMode): ScheduleSettings => {
|
const deriveEffectiveSchedule = (schedule: ScheduleSettings, tab: ScheduleMode): ScheduleSettings => {
|
||||||
const derived = { ...schedule };
|
const derived = { ...schedule };
|
||||||
@@ -80,11 +119,114 @@ const deriveEffectiveSchedule = (schedule: ScheduleSettings, tab: ScheduleMode):
|
|||||||
return derived;
|
return derived;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Sub-component for a single Mapping Group Editor
|
||||||
|
interface MappingGroupEditorProps {
|
||||||
|
title: string;
|
||||||
|
subtitle?: string;
|
||||||
|
rules: ReplacementRule[];
|
||||||
|
onChange: (newRules: ReplacementRule[]) => void;
|
||||||
|
isLocked: boolean;
|
||||||
|
borderColor?: string;
|
||||||
|
bgColor?: string;
|
||||||
|
// Input specific props
|
||||||
|
leftPlaceholder?: string;
|
||||||
|
rightPlaceholder?: string;
|
||||||
|
leftInputClass?: string;
|
||||||
|
rightInputClass?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const MappingGroupEditor: React.FC<MappingGroupEditorProps> = ({
|
||||||
|
title,
|
||||||
|
subtitle,
|
||||||
|
rules,
|
||||||
|
onChange,
|
||||||
|
isLocked,
|
||||||
|
borderColor = "border-gray-700",
|
||||||
|
bgColor = "bg-gray-900/50",
|
||||||
|
leftPlaceholder = "Pattern",
|
||||||
|
rightPlaceholder = "Replace",
|
||||||
|
leftInputClass,
|
||||||
|
rightInputClass
|
||||||
|
}) => {
|
||||||
|
|
||||||
|
const handleAdd = () => {
|
||||||
|
if (isLocked) return;
|
||||||
|
const newId = generateUUID();
|
||||||
|
onChange([...rules, { id: newId, search: '', replace: '' }]);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUpdate = (id: string, field: 'search' | 'replace', value: string) => {
|
||||||
|
if (isLocked) return;
|
||||||
|
onChange(rules.map(r => r.id === id ? { ...r, [field]: value } : r));
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDelete = (id: string) => {
|
||||||
|
if (isLocked) return;
|
||||||
|
onChange(rules.filter(r => r.id !== id));
|
||||||
|
};
|
||||||
|
|
||||||
|
// Default input style if not provided
|
||||||
|
const defaultInputStyle = MAPPING_THEME.inputs.default;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={`p-3 rounded-lg border ${borderColor} ${bgColor} flex flex-col h-full transition-colors`}>
|
||||||
|
<div className="flex items-center justify-between mb-2">
|
||||||
|
<div>
|
||||||
|
<h4 className="text-[10px] font-bold uppercase tracking-wider text-gray-400">{title}</h4>
|
||||||
|
{subtitle && <p className="text-[9px] text-gray-500">{subtitle}</p>}
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
onClick={handleAdd}
|
||||||
|
disabled={isLocked}
|
||||||
|
className="p-1 rounded bg-gray-700/50 hover:bg-gray-600 text-gray-400 hover:text-white transition-colors"
|
||||||
|
title="Add Rule"
|
||||||
|
>
|
||||||
|
<Plus size={12} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex-1 space-y-2 overflow-y-auto max-h-32 custom-scrollbar pr-1">
|
||||||
|
{rules.length === 0 ? (
|
||||||
|
<div className="text-xs text-gray-600 italic text-center py-2 border border-dashed border-gray-700/50 rounded-lg">
|
||||||
|
No rules defined.
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
rules.map((rule) => (
|
||||||
|
<div key={rule.id} className="flex items-center space-x-1 animate-in slide-in-from-left-1 duration-200">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder={leftPlaceholder}
|
||||||
|
value={rule.search}
|
||||||
|
onChange={(e) => handleUpdate(rule.id, 'search', e.target.value)}
|
||||||
|
className={`flex-1 min-w-0 border rounded px-1.5 py-1 text-xs focus:outline-none transition-colors ${leftInputClass || defaultInputStyle}`}
|
||||||
|
/>
|
||||||
|
<ArrowRightCircle size={10} className="text-gray-600 flex-none opacity-50" />
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder={rightPlaceholder}
|
||||||
|
value={rule.replace}
|
||||||
|
onChange={(e) => handleUpdate(rule.id, 'replace', e.target.value)}
|
||||||
|
className={`flex-1 min-w-0 border rounded px-1.5 py-1 text-xs focus:outline-none transition-colors ${rightInputClass || defaultInputStyle}`}
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
onClick={() => handleDelete(rule.id)}
|
||||||
|
className="text-gray-600 hover:text-red-400 p-1 rounded hover:bg-red-500/10 transition-colors flex-none"
|
||||||
|
>
|
||||||
|
<Trash2 size={12} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
interface StrategySelectorProps {
|
interface StrategySelectorProps {
|
||||||
currentStrategy: SyncStrategy;
|
currentStrategy: SyncStrategy;
|
||||||
onSelect: (strategy: SyncStrategy, label: string) => void;
|
onSelect: (strategy: SyncStrategy, label: string) => void;
|
||||||
savedRegexReplacements: RegexReplacement[];
|
savedPathMapping: PathMappingConfig;
|
||||||
onSaveRegex: (replacements: RegexReplacement[]) => void;
|
onSavePathMapping: (config: PathMappingConfig) => void;
|
||||||
savedSchedule: ScheduleSettings;
|
savedSchedule: ScheduleSettings;
|
||||||
onSaveSchedule: (settings: ScheduleSettings) => Promise<boolean>;
|
onSaveSchedule: (settings: ScheduleSettings) => Promise<boolean>;
|
||||||
syncState: SyncState;
|
syncState: SyncState;
|
||||||
@@ -94,8 +236,8 @@ interface StrategySelectorProps {
|
|||||||
const StrategySelector: React.FC<StrategySelectorProps> = ({
|
const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||||
currentStrategy,
|
currentStrategy,
|
||||||
onSelect,
|
onSelect,
|
||||||
savedRegexReplacements,
|
savedPathMapping,
|
||||||
onSaveRegex,
|
onSavePathMapping,
|
||||||
savedSchedule,
|
savedSchedule,
|
||||||
onSaveSchedule,
|
onSaveSchedule,
|
||||||
syncState,
|
syncState,
|
||||||
@@ -104,17 +246,16 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const dropdownRef = useRef<HTMLDivElement>(null);
|
const dropdownRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
// Local state for regex editing
|
// Local state for path mapping editing (stores all lists for both modes)
|
||||||
const [localReplacements, setLocalReplacements] = useState<RegexReplacement[]>([]);
|
const [localPathMapping, setLocalPathMapping] = useState<PathMappingConfig>(savedPathMapping);
|
||||||
const [isRegexDirty, setIsRegexDirty] = useState(false);
|
const [isMappingDirty, setIsMappingDirty] = useState(false);
|
||||||
|
|
||||||
// Local state for Schedule editing
|
// Local state for Schedule editing
|
||||||
const [localSchedule, setLocalSchedule] = useState<ScheduleSettings>(savedSchedule);
|
const [localSchedule, setLocalSchedule] = useState<ScheduleSettings>(savedSchedule);
|
||||||
const [isScheduleDirty, setIsScheduleDirty] = useState(false);
|
const [isScheduleDirty, setIsScheduleDirty] = useState(false);
|
||||||
|
|
||||||
// UI State for Schedule Tabs
|
// UI State for Schedule Tabs
|
||||||
// We initialize active tab based on the saved mode. If DISABLED, default to CRON.
|
const [activeScheduleTab, setActiveScheduleTab] = useState<ScheduleMode>(
|
||||||
const [activeTab, setActiveTab] = useState<ScheduleMode>(
|
|
||||||
savedSchedule.mode === ScheduleMode.DISABLED ? ScheduleMode.CRON : savedSchedule.mode
|
savedSchedule.mode === ScheduleMode.DISABLED ? ScheduleMode.CRON : savedSchedule.mode
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -123,32 +264,30 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
|
|
||||||
// Initialize local state when prop updates
|
// Initialize local state when prop updates
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setLocalReplacements(JSON.parse(JSON.stringify(savedRegexReplacements)));
|
setLocalPathMapping(JSON.parse(JSON.stringify(savedPathMapping)));
|
||||||
setIsRegexDirty(false);
|
setIsMappingDirty(false);
|
||||||
}, [savedRegexReplacements]);
|
}, [savedPathMapping]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setLocalSchedule(JSON.parse(JSON.stringify(savedSchedule)));
|
setLocalSchedule(JSON.parse(JSON.stringify(savedSchedule)));
|
||||||
// If the saved mode is not disabled, ensure we show that tab.
|
|
||||||
if (savedSchedule.mode !== ScheduleMode.DISABLED) {
|
if (savedSchedule.mode !== ScheduleMode.DISABLED) {
|
||||||
setActiveTab(savedSchedule.mode);
|
setActiveScheduleTab(savedSchedule.mode);
|
||||||
}
|
}
|
||||||
setIsScheduleDirty(false);
|
setIsScheduleDirty(false);
|
||||||
}, [savedSchedule]);
|
}, [savedSchedule]);
|
||||||
|
|
||||||
// Check dirty state whenever local changes
|
// Check dirty state whenever local mapping changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const isDifferent = JSON.stringify(localReplacements) !== JSON.stringify(savedRegexReplacements);
|
const isDifferent = JSON.stringify(localPathMapping) !== JSON.stringify(savedPathMapping);
|
||||||
setIsRegexDirty(isDifferent);
|
setIsMappingDirty(isDifferent);
|
||||||
}, [localReplacements, savedRegexReplacements]);
|
}, [localPathMapping, savedPathMapping]);
|
||||||
|
|
||||||
// Check dirty state for Schedule (including Active Tab changes)
|
// Check dirty state for Schedule (including Active Tab changes)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// We calculate what the "effective" schedule would be if we saved right now.
|
const effectiveLocal = deriveEffectiveSchedule(localSchedule, activeScheduleTab);
|
||||||
const effectiveLocal = deriveEffectiveSchedule(localSchedule, activeTab);
|
|
||||||
const isDifferent = JSON.stringify(effectiveLocal) !== JSON.stringify(savedSchedule);
|
const isDifferent = JSON.stringify(effectiveLocal) !== JSON.stringify(savedSchedule);
|
||||||
setIsScheduleDirty(isDifferent);
|
setIsScheduleDirty(isDifferent);
|
||||||
}, [localSchedule, savedSchedule, activeTab]);
|
}, [localSchedule, savedSchedule, activeScheduleTab]);
|
||||||
|
|
||||||
const selectedOption = STRATEGIES.find(s => s.value === currentStrategy) || STRATEGIES[0];
|
const selectedOption = STRATEGIES.find(s => s.value === currentStrategy) || STRATEGIES[0];
|
||||||
|
|
||||||
@@ -162,47 +301,70 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
return () => document.removeEventListener('mousedown', handleClickOutside);
|
return () => document.removeEventListener('mousedown', handleClickOutside);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Determine if tabs have changed from the saved state
|
const isScheduleActionable = isScheduleDirty || (activeScheduleTab !== (savedSchedule.mode === ScheduleMode.DISABLED ? ScheduleMode.CRON : savedSchedule.mode));
|
||||||
const initialTab = savedSchedule.mode === ScheduleMode.DISABLED ? ScheduleMode.CRON : savedSchedule.mode;
|
|
||||||
const hasTabChanged = activeTab !== initialTab;
|
|
||||||
const isScheduleActionable = isScheduleDirty || hasTabChanged;
|
|
||||||
|
|
||||||
const handleSelect = (strategy: StrategyOption) => {
|
const handleSelect = (strategy: StrategyOption) => {
|
||||||
if (isLocked) return;
|
if (isLocked) return;
|
||||||
onSelect(strategy.value, strategy.label);
|
onSelect(strategy.value, strategy.label);
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- Regex Handlers ---
|
// --- Path Mapping Handlers ---
|
||||||
const handleAddRegex = () => {
|
const currentMappingMode = localPathMapping.mode;
|
||||||
|
|
||||||
|
const updateRegexGroup = (section: keyof PathMappingRules, newRules: ReplacementRule[]) => {
|
||||||
if (isLocked) return;
|
if (isLocked) return;
|
||||||
const newId = Date.now().toString();
|
setLocalPathMapping(prev => ({
|
||||||
setLocalReplacements(prev => [...prev, { id: newId, pattern: '', replacement: '' }]);
|
...prev,
|
||||||
|
regex: {
|
||||||
|
...prev.regex,
|
||||||
|
[section]: newRules
|
||||||
|
}
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDeleteRegex = (id: string) => {
|
const updateSimpleGroup = (newRules: ReplacementRule[]) => {
|
||||||
if (isLocked) return;
|
if (isLocked) return;
|
||||||
setLocalReplacements(prev => prev.filter(r => r.id !== id));
|
setLocalPathMapping(prev => ({
|
||||||
|
...prev,
|
||||||
|
simple: newRules
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleUpdateRegex = (id: string, field: 'pattern' | 'replacement', value: string) => {
|
const setMappingMode = (mode: PathMappingMode) => {
|
||||||
if (isLocked) return;
|
if (isLocked) return;
|
||||||
setLocalReplacements(prev => prev.map(r =>
|
setLocalPathMapping(prev => ({ ...prev, mode }));
|
||||||
r.id === id ? { ...r, [field]: value } : r
|
|
||||||
));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleResetRegex = () => {
|
const handleResetMapping = () => {
|
||||||
if (isLocked) return;
|
if (isLocked) return;
|
||||||
setLocalReplacements(JSON.parse(JSON.stringify(savedRegexReplacements)));
|
setLocalPathMapping(JSON.parse(JSON.stringify(savedPathMapping)));
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSaveRegex = () => {
|
const handleSaveMappingClick = () => {
|
||||||
if (isLocked) return;
|
if (isLocked) return;
|
||||||
const validReplacements = localReplacements.filter(r => r.pattern.trim() !== '');
|
const clean = (rules: ReplacementRule[]) => rules.filter(r => r.search.trim() !== '');
|
||||||
setLocalReplacements(validReplacements);
|
|
||||||
onSaveRegex(validReplacements);
|
// Clean regex rules
|
||||||
|
const cleanRegex = (rules: PathMappingRules): PathMappingRules => ({
|
||||||
|
localPre: clean(rules.localPre),
|
||||||
|
localPost: clean(rules.localPost),
|
||||||
|
remotePre: clean(rules.remotePre),
|
||||||
|
remotePost: clean(rules.remotePost),
|
||||||
|
});
|
||||||
|
|
||||||
|
const cleanedConfig: PathMappingConfig = {
|
||||||
|
mode: localPathMapping.mode,
|
||||||
|
simple: clean(localPathMapping.simple),
|
||||||
|
regex: cleanRegex(localPathMapping.regex),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
setLocalPathMapping(cleanedConfig);
|
||||||
|
onSavePathMapping(cleanedConfig);
|
||||||
|
};
|
||||||
|
|
||||||
|
const regexRules = localPathMapping.regex;
|
||||||
|
const simpleRules = localPathMapping.simple;
|
||||||
|
|
||||||
// --- Schedule Handlers ---
|
// --- Schedule Handlers ---
|
||||||
const handleUpdateSchedule = (field: keyof ScheduleSettings, value: any) => {
|
const handleUpdateSchedule = (field: keyof ScheduleSettings, value: any) => {
|
||||||
if (isLocked) return;
|
if (isLocked) return;
|
||||||
@@ -222,24 +384,18 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
if (isLocked) return;
|
if (isLocked) return;
|
||||||
setLocalSchedule(JSON.parse(JSON.stringify(savedSchedule)));
|
setLocalSchedule(JSON.parse(JSON.stringify(savedSchedule)));
|
||||||
if (savedSchedule.mode !== ScheduleMode.DISABLED) {
|
if (savedSchedule.mode !== ScheduleMode.DISABLED) {
|
||||||
setActiveTab(savedSchedule.mode);
|
setActiveScheduleTab(savedSchedule.mode);
|
||||||
} else {
|
} else {
|
||||||
setActiveTab(ScheduleMode.CRON);
|
setActiveScheduleTab(ScheduleMode.CRON);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSaveScheduleClick = async () => {
|
const handleSaveScheduleClick = async () => {
|
||||||
if (isLocked) return;
|
if (isLocked) return;
|
||||||
|
const settingsToSave = deriveEffectiveSchedule(localSchedule, activeScheduleTab);
|
||||||
// Determine the effective settings based on the current view (tab) and inputs
|
|
||||||
const settingsToSave = deriveEffectiveSchedule(localSchedule, activeTab);
|
|
||||||
|
|
||||||
// Call API
|
|
||||||
const success = await onSaveSchedule(settingsToSave);
|
const success = await onSaveSchedule(settingsToSave);
|
||||||
if (success) {
|
if (success) {
|
||||||
setLocalSchedule(settingsToSave);
|
setLocalSchedule(settingsToSave);
|
||||||
// Dirty state is cleared by the useEffect prop update, or we can clear it optimistically here if needed,
|
|
||||||
// but useEffect [savedSchedule] handles it correctly.
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -248,7 +404,6 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
onSync();
|
onSync();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helper to toggle enable/disable for current active tab (Daily/Weekly)
|
|
||||||
const toggleScheduleEnable = (targetMode: ScheduleMode) => {
|
const toggleScheduleEnable = (targetMode: ScheduleMode) => {
|
||||||
if (isLocked) return;
|
if (isLocked) return;
|
||||||
if (localSchedule.mode === targetMode) {
|
if (localSchedule.mode === targetMode) {
|
||||||
@@ -258,7 +413,6 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// If syncing or locked, apply grayscale filter to content sections
|
|
||||||
const contentClass = isLocked ? "opacity-50 pointer-events-none grayscale transition-all" : "transition-all";
|
const contentClass = isLocked ? "opacity-50 pointer-events-none grayscale transition-all" : "transition-all";
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -279,12 +433,13 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
<div
|
<div
|
||||||
className={`absolute
|
className={`absolute
|
||||||
top-14
|
top-14
|
||||||
/* Mobile: Open to left */
|
/* Mobile: Open to left (max width of screen) */
|
||||||
right-0 origin-top-right
|
right-0 w-[90vw] max-w-[90vw] origin-top-right
|
||||||
/* Desktop: Center alignment */
|
|
||||||
md:left-1/2 md:right-auto md:origin-top md:-translate-x-1/2
|
|
||||||
|
|
||||||
w-80 md:w-[32rem] bg-gray-800/95 border border-white/10 rounded-xl shadow-2xl z-50 overflow-hidden backdrop-blur-xl
|
/* Desktop: Center alignment, wider */
|
||||||
|
md:left-1/2 md:right-auto md:origin-top md:-translate-x-1/2 md:w-[60rem] md:max-w-[60rem]
|
||||||
|
|
||||||
|
bg-gray-800/95 border border-white/10 rounded-xl shadow-2xl z-50 overflow-hidden backdrop-blur-xl
|
||||||
transition-all duration-200 ease-out
|
transition-all duration-200 ease-out
|
||||||
${isOpen ? 'opacity-100 scale-100 visible translate-y-0' : 'opacity-0 scale-95 invisible pointer-events-none -translate-y-2'}`}
|
${isOpen ? 'opacity-100 scale-100 visible translate-y-0' : 'opacity-0 scale-95 invisible pointer-events-none -translate-y-2'}`}
|
||||||
>
|
>
|
||||||
@@ -328,78 +483,106 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Section 2: Regex Preprocessing */}
|
{/* Section 2: Path Mapping (Tabs + Grid) */}
|
||||||
<div className="p-4 bg-gray-900/40 border-b border-white/5 flex-none">
|
<div className="p-4 bg-gray-900/40 border-b border-white/5 flex-none">
|
||||||
<div className="flex items-center justify-between mb-3">
|
<div className="flex items-center justify-between mb-3">
|
||||||
<h3 className="text-[10px] font-bold text-gray-500 uppercase tracking-widest">Regex Rules</h3>
|
<h3 className="text-[10px] font-bold text-gray-500 uppercase tracking-widest">Path Mapping</h3>
|
||||||
{localReplacements.length === 0 && (
|
|
||||||
<button
|
|
||||||
onClick={handleAddRegex}
|
|
||||||
className="p-1 rounded bg-gray-700/50 hover:bg-gray-600 text-gray-400 hover:text-white transition-colors"
|
|
||||||
title="Add Rule"
|
|
||||||
>
|
|
||||||
<Plus size={14} />
|
|
||||||
</button>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="space-y-2 mb-4 max-h-40 overflow-y-auto pr-1 custom-scrollbar">
|
{/* Tabs for Path Mapping Mode */}
|
||||||
{localReplacements.length === 0 ? (
|
<div className="flex space-x-1 bg-black/30 p-1 rounded-lg mb-4">
|
||||||
<div className="text-xs text-gray-600 italic text-center py-2 border border-dashed border-gray-700/50 rounded-lg">
|
{[
|
||||||
No regex replacements configured.
|
{ id: PathMappingMode.SIMPLE, label: 'Simple Mapping', icon: Type },
|
||||||
|
{ id: PathMappingMode.REGEX, label: 'Regex Rules', icon: Code2 },
|
||||||
|
].map((tab) => (
|
||||||
|
<button
|
||||||
|
key={tab.id}
|
||||||
|
onClick={() => setMappingMode(tab.id)}
|
||||||
|
className={`flex-1 flex items-center justify-center space-x-2 py-1.5 rounded-md text-xs font-medium transition-all
|
||||||
|
${currentMappingMode === tab.id
|
||||||
|
? 'bg-gray-700 text-plex-orange shadow-sm'
|
||||||
|
: 'text-gray-400 hover:text-gray-200 hover:bg-white/5'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<tab.icon size={12} />
|
||||||
|
<span>{tab.label}</span>
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Content Area */}
|
||||||
|
<div className="mb-4">
|
||||||
|
{currentMappingMode === PathMappingMode.SIMPLE ? (
|
||||||
|
// Simple Mode: Single Editor
|
||||||
|
<div className="animate-in fade-in duration-200">
|
||||||
|
<MappingGroupEditor
|
||||||
|
title="Path Mapping"
|
||||||
|
subtitle="Map Local paths to Cloud paths using simple string matching"
|
||||||
|
rules={simpleRules}
|
||||||
|
onChange={updateSimpleGroup}
|
||||||
|
isLocked={isLocked}
|
||||||
|
borderColor={MAPPING_THEME.simple.borderColor}
|
||||||
|
bgColor={MAPPING_THEME.simple.bgColor}
|
||||||
|
leftPlaceholder="Local Path"
|
||||||
|
rightPlaceholder="Cloud Path"
|
||||||
|
leftInputClass={MAPPING_THEME.inputs.local}
|
||||||
|
rightInputClass={MAPPING_THEME.inputs.cloud}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
localReplacements.map((regex) => (
|
// Regex Mode: 2x2 Grid
|
||||||
<div key={regex.id} className="flex items-center space-x-2 animate-in slide-in-from-left-2 duration-200">
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 animate-in fade-in duration-200">
|
||||||
<div className="flex-1 min-w-0">
|
{/* Row 1: Pre-Processing */}
|
||||||
<input
|
<MappingGroupEditor
|
||||||
type="text"
|
title="Local Playlist"
|
||||||
placeholder="Pattern"
|
subtitle="Pre-Processing (Before Sync)"
|
||||||
value={regex.pattern}
|
rules={regexRules.localPre}
|
||||||
onChange={(e) => handleUpdateRegex(regex.id, 'pattern', e.target.value)}
|
onChange={(rules) => updateRegexGroup('localPre', rules)}
|
||||||
className={`w-full bg-gray-900/80 border rounded-md px-2 py-1.5 text-xs text-gray-200 focus:outline-none focus:ring-1 transition-all placeholder-gray-600
|
isLocked={isLocked}
|
||||||
${!regex.pattern && isRegexDirty ? 'border-red-500/30 focus:border-red-500' : 'border-gray-700 focus:border-plex-orange'}`}
|
borderColor={MAPPING_THEME.local.borderColor}
|
||||||
|
bgColor={MAPPING_THEME.local.bgColor}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<MappingGroupEditor
|
||||||
|
title="Remote Playlist"
|
||||||
|
subtitle="Pre-Processing (Before Sync)"
|
||||||
|
rules={regexRules.remotePre}
|
||||||
|
onChange={(rules) => updateRegexGroup('remotePre', rules)}
|
||||||
|
isLocked={isLocked}
|
||||||
|
borderColor={MAPPING_THEME.remote.borderColor}
|
||||||
|
bgColor={MAPPING_THEME.remote.bgColor}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{/* Row 2: Post-Processing */}
|
||||||
|
<MappingGroupEditor
|
||||||
|
title="Local Playlist"
|
||||||
|
subtitle="Post-Processing (After Sync / Result)"
|
||||||
|
rules={regexRules.localPost}
|
||||||
|
onChange={(rules) => updateRegexGroup('localPost', rules)}
|
||||||
|
isLocked={isLocked}
|
||||||
|
borderColor={MAPPING_THEME.local.borderColor}
|
||||||
|
bgColor={MAPPING_THEME.local.bgColor}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<MappingGroupEditor
|
||||||
|
title="Remote Playlist"
|
||||||
|
subtitle="Post-Processing (After Sync / Result)"
|
||||||
|
rules={regexRules.remotePost}
|
||||||
|
onChange={(rules) => updateRegexGroup('remotePost', rules)}
|
||||||
|
isLocked={isLocked}
|
||||||
|
borderColor={MAPPING_THEME.remote.borderColor}
|
||||||
|
bgColor={MAPPING_THEME.remote.bgColor}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex-none text-gray-600">
|
|
||||||
<ArrowRightCircle size={12} />
|
|
||||||
</div>
|
|
||||||
<div className="flex-1 min-w-0">
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
placeholder="Replacement"
|
|
||||||
value={regex.replacement}
|
|
||||||
onChange={(e) => handleUpdateRegex(regex.id, 'replacement', e.target.value)}
|
|
||||||
className="w-full bg-gray-900/80 border border-gray-700 rounded-md px-2 py-1.5 text-xs text-gray-200 focus:outline-none focus:border-plex-orange focus:ring-1 focus:ring-plex-orange transition-all placeholder-gray-600"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<button
|
|
||||||
onClick={() => handleDeleteRegex(regex.id)}
|
|
||||||
className="text-gray-600 hover:text-red-400 p-1.5 hover:bg-red-500/10 rounded transition-colors"
|
|
||||||
title="Delete Rule"
|
|
||||||
>
|
|
||||||
<Trash2 size={14} />
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
))
|
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex justify-between items-center gap-2">
|
<div className="flex justify-end items-center gap-2">
|
||||||
<button
|
<button
|
||||||
onClick={handleAddRegex}
|
onClick={handleResetMapping}
|
||||||
className={`flex items-center space-x-1 px-2 py-1 rounded text-[10px] font-bold uppercase tracking-wide transition-colors ${localReplacements.length > 0 ? 'text-plex-orange hover:bg-plex-orange/10' : 'hidden'}`}
|
disabled={!isMappingDirty}
|
||||||
>
|
|
||||||
<Plus size={10} />
|
|
||||||
<span>Add</span>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
<div className="flex items-center gap-2 ml-auto">
|
|
||||||
<button
|
|
||||||
onClick={handleResetRegex}
|
|
||||||
disabled={!isRegexDirty}
|
|
||||||
className={`flex items-center justify-center space-x-1.5 px-3 py-1.5 rounded-md text-xs font-medium border transition-all
|
className={`flex items-center justify-center space-x-1.5 px-3 py-1.5 rounded-md text-xs font-medium border transition-all
|
||||||
${isRegexDirty
|
${isMappingDirty
|
||||||
? 'bg-gray-800 border-gray-600 text-gray-300 hover:bg-gray-700 hover:text-white'
|
? 'bg-gray-800 border-gray-600 text-gray-300 hover:bg-gray-700 hover:text-white'
|
||||||
: 'bg-transparent border-transparent text-gray-700 cursor-not-allowed'}`}
|
: 'bg-transparent border-transparent text-gray-700 cursor-not-allowed'}`}
|
||||||
>
|
>
|
||||||
@@ -407,19 +590,18 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
<span>Revert</span>
|
<span>Revert</span>
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={handleSaveRegex}
|
onClick={handleSaveMappingClick}
|
||||||
disabled={!isRegexDirty}
|
disabled={!isMappingDirty}
|
||||||
className={`flex items-center justify-center space-x-1.5 px-3 py-1.5 rounded-md text-xs font-bold border transition-all
|
className={`flex items-center justify-center space-x-1.5 px-3 py-1.5 rounded-md text-xs font-bold border transition-all
|
||||||
${isRegexDirty
|
${isMappingDirty
|
||||||
? 'bg-plex-orange border-plex-orange text-gray-900 hover:bg-yellow-500 shadow-lg shadow-plex-orange/10'
|
? 'bg-plex-orange border-plex-orange text-gray-900 hover:bg-yellow-500 shadow-lg shadow-plex-orange/10'
|
||||||
: 'bg-gray-800/30 border-gray-800/50 text-gray-600 cursor-not-allowed'}`}
|
: 'bg-gray-800/30 border-gray-800/50 text-gray-600 cursor-not-allowed'}`}
|
||||||
>
|
>
|
||||||
<Save size={12} />
|
<Save size={12} />
|
||||||
<span>Save</span>
|
<span>Save Rules</span>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Section 3: Scheduled Tasks */}
|
{/* Section 3: Scheduled Tasks */}
|
||||||
<div className="p-4 bg-gray-900/40 border-b border-white/5 flex-none">
|
<div className="p-4 bg-gray-900/40 border-b border-white/5 flex-none">
|
||||||
@@ -436,9 +618,9 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
].map((tab) => (
|
].map((tab) => (
|
||||||
<button
|
<button
|
||||||
key={tab.id}
|
key={tab.id}
|
||||||
onClick={() => setActiveTab(tab.id)}
|
onClick={() => setActiveScheduleTab(tab.id)}
|
||||||
className={`flex-1 flex items-center justify-center space-x-2 py-1.5 rounded-md text-xs font-medium transition-all
|
className={`flex-1 flex items-center justify-center space-x-2 py-1.5 rounded-md text-xs font-medium transition-all
|
||||||
${activeTab === tab.id
|
${activeScheduleTab === tab.id
|
||||||
? 'bg-gray-700 text-plex-orange shadow-sm'
|
? 'bg-gray-700 text-plex-orange shadow-sm'
|
||||||
: 'text-gray-400 hover:text-gray-200 hover:bg-white/5'
|
: 'text-gray-400 hover:text-gray-200 hover:bg-white/5'
|
||||||
}`}
|
}`}
|
||||||
@@ -451,7 +633,7 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
|
|
||||||
{/* Tab Content */}
|
{/* Tab Content */}
|
||||||
<div className="mb-4 min-h-[50px]">
|
<div className="mb-4 min-h-[50px]">
|
||||||
{activeTab === ScheduleMode.CRON && (
|
{activeScheduleTab === ScheduleMode.CRON && (
|
||||||
<div className="space-y-2 animate-in fade-in duration-200">
|
<div className="space-y-2 animate-in fade-in duration-200">
|
||||||
<div className="flex items-center space-x-2">
|
<div className="flex items-center space-x-2">
|
||||||
<span className="text-gray-500 font-mono text-xs">Cron:</span>
|
<span className="text-gray-500 font-mono text-xs">Cron:</span>
|
||||||
@@ -469,7 +651,7 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{activeTab === ScheduleMode.DAILY && (
|
{activeScheduleTab === ScheduleMode.DAILY && (
|
||||||
<div className="flex flex-col animate-in fade-in duration-200">
|
<div className="flex flex-col animate-in fade-in duration-200">
|
||||||
{/* Top Row: Checkbox + Label */}
|
{/* Top Row: Checkbox + Label */}
|
||||||
<div className="flex items-center justify-start space-x-2 mb-2">
|
<div className="flex items-center justify-start space-x-2 mb-2">
|
||||||
@@ -496,7 +678,7 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{activeTab === ScheduleMode.WEEKLY && (
|
{activeScheduleTab === ScheduleMode.WEEKLY && (
|
||||||
<div className="flex flex-col animate-in fade-in duration-200">
|
<div className="flex flex-col animate-in fade-in duration-200">
|
||||||
{/* Top Row: Checkbox + Label */}
|
{/* Top Row: Checkbox + Label */}
|
||||||
<div className="flex items-center justify-start space-x-2 mb-2">
|
<div className="flex items-center justify-start space-x-2 mb-2">
|
||||||
@@ -563,7 +745,7 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Action Buttons (Mirrored from Regex) */}
|
{/* Action Buttons */}
|
||||||
<div className="flex items-center gap-2 justify-end pt-3 border-t border-white/5">
|
<div className="flex items-center gap-2 justify-end pt-3 border-t border-white/5">
|
||||||
<button
|
<button
|
||||||
onClick={handleResetSchedule}
|
onClick={handleResetSchedule}
|
||||||
@@ -599,7 +781,7 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
className={`w-full py-3 rounded-lg text-sm font-bold flex items-center justify-center gap-2 transition-all shadow-lg
|
className={`w-full py-3 rounded-lg text-sm font-bold flex items-center justify-center gap-2 transition-all shadow-lg
|
||||||
${isLocked
|
${isLocked
|
||||||
? 'bg-gray-700/30 text-gray-500 cursor-not-allowed border border-gray-700/50'
|
? 'bg-gray-700/30 text-gray-500 cursor-not-allowed border border-gray-700/50'
|
||||||
: isRegexDirty
|
: isMappingDirty
|
||||||
? 'bg-gray-800 text-gray-500 cursor-not-allowed border border-gray-700'
|
? 'bg-gray-800 text-gray-500 cursor-not-allowed border border-gray-700'
|
||||||
: 'bg-green-600 hover:bg-green-500 text-white border border-green-500 shadow-green-900/20 active:scale-[0.98]'
|
: 'bg-green-600 hover:bg-green-500 text-white border border-green-500 shadow-green-900/20 active:scale-[0.98]'
|
||||||
}`}
|
}`}
|
||||||
@@ -616,9 +798,9 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
|||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</button>
|
</button>
|
||||||
{(isRegexDirty) && (
|
{(isMappingDirty) && (
|
||||||
<p className="text-[10px] text-plex-orange text-center mt-2">
|
<p className="text-[10px] text-plex-orange text-center mt-2">
|
||||||
Please save regex changes before syncing.
|
Please save path mapping changes before syncing.
|
||||||
</p>
|
</p>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+61
-13
@@ -1,4 +1,4 @@
|
|||||||
import { Playlist, ServerType, ApiResponse, PlexServerConnection, PlexConnectionSettings, PlexLibrary, RegexReplacement, SyncStrategy, ScheduleSettings } from '../types';
|
import { Playlist, ServerType, ApiResponse, PlexServerConnection, PlexConnectionSettings, PlexLibrary, ReplacementRule, PathMappingConfig, PathMappingMode, PathMappingRules, SyncStrategy, ScheduleSettings } from '../types';
|
||||||
|
|
||||||
const API_BASE = import.meta.env.VITE_API_BASE_URL || '';
|
const API_BASE = import.meta.env.VITE_API_BASE_URL || '';
|
||||||
|
|
||||||
@@ -41,21 +41,69 @@ const mapLibrary = (item: any): PlexLibrary => ({
|
|||||||
type: item.type ?? 'artist',
|
type: item.type ?? 'artist',
|
||||||
});
|
});
|
||||||
|
|
||||||
const mapRegexRules = (rules: any[]): RegexReplacement[] =>
|
// Helper function to map raw rules array to ReplacementRule[]
|
||||||
|
const mapReplacementRules = (rules: any[]): ReplacementRule[] =>
|
||||||
(rules || []).map((rule, index) => ({
|
(rules || []).map((rule, index) => ({
|
||||||
id: rule.id || `${rule.pattern || 'rule'}-${index}`,
|
id: rule.id || `${rule.search || 'rule'}-${index}-${Date.now()}`,
|
||||||
pattern: rule.pattern || '',
|
search: rule.search || rule.pattern || '',
|
||||||
replacement: rule.replacement || '',
|
replace: rule.replace || rule.replacement || '',
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// Helper function to map API path_mapping response to PathMappingConfig
|
||||||
|
const mapPathMappingConfig = (data: any): PathMappingConfig => {
|
||||||
|
const defaultConfig: PathMappingConfig = {
|
||||||
|
mode: PathMappingMode.SIMPLE,
|
||||||
|
simple: [],
|
||||||
|
regex: {
|
||||||
|
localPre: [],
|
||||||
|
localPost: [],
|
||||||
|
remotePre: [],
|
||||||
|
remotePost: []
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!data || !data.path_mapping) {
|
||||||
|
return defaultConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
const pm = data.path_mapping;
|
||||||
|
return {
|
||||||
|
mode: pm.mode === 'REGEX' ? PathMappingMode.REGEX : PathMappingMode.SIMPLE,
|
||||||
|
simple: mapReplacementRules(pm.simple || []),
|
||||||
|
regex: {
|
||||||
|
localPre: mapReplacementRules(pm.regex?.localPre || pm.regex?.local_pre || []),
|
||||||
|
localPost: mapReplacementRules(pm.regex?.localPost || pm.regex?.local_post || []),
|
||||||
|
remotePre: mapReplacementRules(pm.regex?.remotePre || pm.regex?.remote_pre || []),
|
||||||
|
remotePost: mapReplacementRules(pm.regex?.remotePost || pm.regex?.remote_post || [])
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// Helper function to convert PathMappingConfig to API format
|
||||||
|
const pathMappingToApi = (config: PathMappingConfig) => {
|
||||||
|
const rulesToApi = (rules: ReplacementRule[]) =>
|
||||||
|
rules.map(({ id, search, replace }) => ({ id, search, replace }));
|
||||||
|
|
||||||
|
return {
|
||||||
|
mode: config.mode,
|
||||||
|
simple: rulesToApi(config.simple),
|
||||||
|
regex: {
|
||||||
|
local_pre: rulesToApi(config.regex.localPre),
|
||||||
|
local_post: rulesToApi(config.regex.localPost),
|
||||||
|
remote_pre: rulesToApi(config.regex.remotePre),
|
||||||
|
remote_post: rulesToApi(config.regex.remotePost)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const apiService = {
|
export const apiService = {
|
||||||
async getSettings(): Promise<ApiResponse<{ strategy: SyncStrategy; regex: RegexReplacement[]; connection: PlexConnectionSettings; localPath: string }>> {
|
async getSettings(): Promise<ApiResponse<{ strategy: SyncStrategy; pathMapping: PathMappingConfig; connection: PlexConnectionSettings; localPath: string }>> {
|
||||||
const response = await fetch(`${API_BASE}/api/settings`);
|
const response = await fetch(`${API_BASE}/api/settings`);
|
||||||
const result = await handleResponse<any>(response);
|
const result = await handleResponse<any>(response);
|
||||||
if (result.status === 'success') {
|
if (result.status === 'success') {
|
||||||
const mode = result.data.sync_mode as string;
|
const mode = result.data.sync_mode as string;
|
||||||
const strategy = MODE_TO_STRATEGY[mode] || SyncStrategy.LOCAL_OVERWRITE;
|
const strategy = MODE_TO_STRATEGY[mode] || SyncStrategy.LOCAL_OVERWRITE;
|
||||||
const regex = mapRegexRules(result.data.path_rules || []);
|
const pathMapping = mapPathMappingConfig(result.data);
|
||||||
const connection: PlexConnectionSettings = {
|
const connection: PlexConnectionSettings = {
|
||||||
protocol: (result.data.scheme as 'http' | 'https') || 'https',
|
protocol: (result.data.scheme as 'http' | 'https') || 'https',
|
||||||
address: result.data.server_url || '',
|
address: result.data.server_url || '',
|
||||||
@@ -63,9 +111,9 @@ export const apiService = {
|
|||||||
token: result.data.token || '',
|
token: result.data.token || '',
|
||||||
libraryName: result.data.library_name || '',
|
libraryName: result.data.library_name || '',
|
||||||
};
|
};
|
||||||
return { status: 'success', data: { strategy, regex, connection, localPath: result.data.local_path || '' } };
|
return { status: 'success', data: { strategy, pathMapping, connection, localPath: result.data.local_path || '' } };
|
||||||
}
|
}
|
||||||
return result as ApiResponse<any> as ApiResponse<{ strategy: SyncStrategy; regex: RegexReplacement[]; connection: PlexConnectionSettings; localPath: string }>;
|
return result as ApiResponse<any> as ApiResponse<{ strategy: SyncStrategy; pathMapping: PathMappingConfig; connection: PlexConnectionSettings; localPath: string }>;
|
||||||
},
|
},
|
||||||
|
|
||||||
async updateSyncStrategy(strategy: SyncStrategy): Promise<ApiResponse<{ sync_mode: string }>> {
|
async updateSyncStrategy(strategy: SyncStrategy): Promise<ApiResponse<{ sync_mode: string }>> {
|
||||||
@@ -78,9 +126,9 @@ export const apiService = {
|
|||||||
return handleResponse(response);
|
return handleResponse(response);
|
||||||
},
|
},
|
||||||
|
|
||||||
async saveRegexRules(replacements: RegexReplacement[]): Promise<ApiResponse<{ rules: RegexReplacement[] }>> {
|
async savePathMapping(config: PathMappingConfig): Promise<ApiResponse<null>> {
|
||||||
const payload = { rules: replacements.map(({ pattern, replacement }) => ({ pattern, replacement })) };
|
const payload = pathMappingToApi(config);
|
||||||
const response = await fetch(`${API_BASE}/api/settings/regex-rules`, {
|
const response = await fetch(`${API_BASE}/api/settings/path-mapping`, {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(payload),
|
body: JSON.stringify(payload),
|
||||||
@@ -170,7 +218,7 @@ export const apiService = {
|
|||||||
return result as ApiResponse<{ token: string; serverInfo: PlexServerConnection }>;
|
return result as ApiResponse<{ token: string; serverInfo: PlexServerConnection }>;
|
||||||
},
|
},
|
||||||
|
|
||||||
async syncPlaylists(strategy: SyncStrategy, _regexRules: RegexReplacement[], localPath?: string): Promise<ApiResponse<null>> {
|
async syncPlaylists(strategy: SyncStrategy, _pathMapping: PathMappingConfig, localPath?: string): Promise<ApiResponse<null>> {
|
||||||
const response = await fetch(`${API_BASE}/api/sync`, {
|
const response = await fetch(`${API_BASE}/api/sync`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
|||||||
@@ -34,6 +34,30 @@ export enum SyncState {
|
|||||||
ERROR = 'ERROR'
|
ERROR = 'ERROR'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ReplacementRule {
|
||||||
|
id: string;
|
||||||
|
search: string;
|
||||||
|
replace: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PathMappingRules {
|
||||||
|
localPre: ReplacementRule[];
|
||||||
|
localPost: ReplacementRule[];
|
||||||
|
remotePre: ReplacementRule[];
|
||||||
|
remotePost: ReplacementRule[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum PathMappingMode {
|
||||||
|
SIMPLE = 'SIMPLE',
|
||||||
|
REGEX = 'REGEX'
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PathMappingConfig {
|
||||||
|
mode: PathMappingMode;
|
||||||
|
simple: ReplacementRule[];
|
||||||
|
regex: PathMappingRules;
|
||||||
|
}
|
||||||
|
|
||||||
export interface RegexReplacement {
|
export interface RegexReplacement {
|
||||||
id: string;
|
id: string;
|
||||||
pattern: string;
|
pattern: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user