Store track paths relative to the data dir for multi-machine sync
The library is going live as the canonical store, synced to a second machine via Syncthing. Track locations were stored absolute and only remapped at import, so on a second machine (where Syncthing mounts the folder at a different path) every location would break. - lintunes/paths.py: to_relative/to_absolute. Locations are stored relative to the data dir and resolved back on load, applied only at the json_storage boundary (save_tracks/load_library). Track.location stays absolute in memory, so the player, tagging, and art code are unchanged. The data dir and the music move together inside one synced tree, so paths resolve wherever it's mounted — no per-machine music_root config. Absolute paths in older library.json files still load (back-compat). - tests/test_round15.py: helper round-trips, back-compat, and a machine-2 scenario (save under root A, load the copied tree under root B). - TASKS.md/tasks-done.md: mark the data-dir move + real import done; log the benign exit-time Qt/FFmpeg teardown segfault. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@ -2,6 +2,7 @@ import json
|
||||
from pathlib import Path
|
||||
|
||||
from lintunes.models import Track, Playlist, PlaylistSettings, Library
|
||||
from lintunes.paths import to_relative, to_absolute
|
||||
|
||||
|
||||
def save_library(library: Library, data_dir: Path):
|
||||
@ -23,7 +24,14 @@ def save_library(library: Library, data_dir: Path):
|
||||
|
||||
def save_tracks(library: Library, data_dir: Path):
|
||||
data_dir.mkdir(parents=True, exist_ok=True)
|
||||
tracks_dict = {str(tid): track.to_dict() for tid, track in library.tracks.items()}
|
||||
tracks_dict = {}
|
||||
for tid, track in library.tracks.items():
|
||||
data = track.to_dict()
|
||||
if "location" in data:
|
||||
# Store paths relative to the data dir so the library is portable
|
||||
# across machines that sync the folder to different mount points.
|
||||
data["location"] = to_relative(data["location"], data_dir)
|
||||
tracks_dict[str(tid)] = data
|
||||
_write_json(data_dir / "library.json", tracks_dict)
|
||||
|
||||
|
||||
@ -62,6 +70,8 @@ def load_library(data_dir: Path) -> Library:
|
||||
tracks_dict = _read_json(library_path)
|
||||
for tid_str, track_data in tracks_dict.items():
|
||||
track = Track.from_dict(track_data)
|
||||
# Stored relative to the data dir; resolve back to an absolute path.
|
||||
track.location = to_absolute(track.location, data_dir)
|
||||
tracks[track.track_id] = track
|
||||
|
||||
# Load metadata
|
||||
|
||||
Reference in New Issue
Block a user