mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-04 20:43:04 +00:00
handle watching ~/ dir
+ fix bug that caused duplicate album colors in db
This commit is contained in:
+52
-5
@@ -1,6 +1,12 @@
|
||||
from flask import Blueprint, request
|
||||
from app import settings
|
||||
|
||||
from app.db.sqlite.settings import SettingsSQLMethods as sdb
|
||||
from app.lib import populate
|
||||
from app.logger import log
|
||||
|
||||
from app.db.store import Store
|
||||
from app.utils import background
|
||||
|
||||
api = Blueprint("settings", __name__, url_prefix="/")
|
||||
|
||||
@@ -8,7 +14,22 @@ api = Blueprint("settings", __name__, url_prefix="/")
|
||||
def get_child_dirs(parent: str, children: list[str]):
|
||||
"""Returns child directories in a list, given a parent directory"""
|
||||
|
||||
return [dir for dir in children if dir.startswith(parent)]
|
||||
return [dir for dir in children if dir.startswith(parent) and dir != parent]
|
||||
|
||||
|
||||
@background
|
||||
def rebuild_store(db_dirs: list[str]):
|
||||
log.info("Rebuilding library...")
|
||||
Store.remove_tracks_by_dir_except(db_dirs)
|
||||
|
||||
Store.load_all_tracks()
|
||||
Store.process_folders()
|
||||
Store.load_albums()
|
||||
Store.load_artists()
|
||||
|
||||
populate.Populate()
|
||||
|
||||
log.info("Rebuilding library... ✅")
|
||||
|
||||
|
||||
@api.route("/settings/add-root-dirs", methods=["POST"])
|
||||
@@ -29,16 +50,42 @@ def add_root_dirs():
|
||||
except KeyError:
|
||||
return msg, 400
|
||||
|
||||
# --- Unregister child directories ---
|
||||
def finalize(new_dirs: list[str], removed_dirs: list[str], db_dirs: list[str]):
|
||||
sdb.remove_root_dirs(removed_dirs)
|
||||
sdb.add_root_dirs(new_dirs)
|
||||
rebuild_store(db_dirs)
|
||||
|
||||
# ---
|
||||
db_dirs = sdb.get_root_dirs()
|
||||
|
||||
if db_dirs[0] == "$home" and new_dirs[0] == "$home".strip():
|
||||
return {"msg": "Not changed!"}
|
||||
|
||||
if db_dirs[0] == "$home":
|
||||
sdb.remove_root_dirs(db_dirs)
|
||||
|
||||
try:
|
||||
if new_dirs[0] == "$home":
|
||||
finalize(["$home"], db_dirs, [settings.USER_HOME_DIR])
|
||||
|
||||
return {"msg": "Updated!"}
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
for _dir in new_dirs:
|
||||
children = get_child_dirs(_dir, db_dirs)
|
||||
removed_dirs.extend(children)
|
||||
# ------------------------------------
|
||||
# ---
|
||||
|
||||
sdb.add_root_dirs(new_dirs)
|
||||
sdb.remove_root_dirs(removed_dirs)
|
||||
for _dir in removed_dirs:
|
||||
try:
|
||||
db_dirs.remove(_dir)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
db_dirs.extend(new_dirs)
|
||||
|
||||
finalize(new_dirs, removed_dirs, db_dirs)
|
||||
|
||||
return {"msg": "Updated!"}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user