update supported audio files in settings.py

+ add win_replace_slash function to format win path strings
+ misc
This commit is contained in:
geoffrey45
2023-01-30 15:59:28 +03:00
parent 93a04ba041
commit 7e15680f26
15 changed files with 268 additions and 96 deletions
+38 -7
View File
@@ -2,6 +2,7 @@
Contains all the folder routes.
"""
import os
import psutil
from pathlib import Path
from flask import Blueprint, request
@@ -10,7 +11,7 @@ from app import settings
from app.lib.folderslib import GetFilesAndDirs
from app.db.sqlite.settings import SettingsSQLMethods as db
from app.models import Folder
from app.utils import create_folder_hash
from app.utils import create_folder_hash, is_windows, win_replace_slash
api = Blueprint("folder", __name__, url_prefix="/")
@@ -42,8 +43,8 @@ def get_folder_tree():
return {
"folders": [
Folder(
name=f.name,
path=str(f),
name=f.name if f.name != "" else str(f).replace("\\", "/"),
path=win_replace_slash(str(f)),
has_tracks=True,
is_sym=f.is_symlink(),
path_hash=create_folder_hash(*f.parts[1:]),
@@ -61,26 +62,56 @@ def get_folder_tree():
}
def get_all_drives():
"""
Returns a list of all the drives on a windows machine.
"""
drives = psutil.disk_partitions()
return [d.mountpoint for d in drives]
@api.route("/folder/dir-browser", methods=["POST"])
def list_folders():
"""
Returns a list of all the folders in the given folder.
"""
data = request.get_json()
is_win = is_windows()
try:
req_dir: str = data["folder"]
except KeyError:
req_dir = settings.USER_HOME_DIR
req_dir = "$home"
if req_dir == "$home":
req_dir = settings.USER_HOME_DIR
# req_dir = settings.USER_HOME_DIR
if is_win:
return {
"folders": [
{"name": win_replace_slash(d), "path": win_replace_slash(d)}
for d in get_all_drives()
]
}
entries = os.scandir(req_dir)
req_dir = req_dir + "/"
try:
entries = os.scandir(req_dir)
except PermissionError:
return {"folders": []}
dirs = [e.name for e in entries if e.is_dir() and not e.name.startswith(".")]
dirs = [{"name": d, "path": os.path.join(req_dir, d)} for d in dirs]
dirs = [
{"name": d, "path": win_replace_slash(os.path.join(req_dir, d))} for d in dirs
]
return {
"folders": sorted(dirs, key=lambda i: i["name"]),
}
# todo:
# - handle showing windows disks in root_dir configuration
# - handle the above, but for all partitions mounted in linux.
# - handle the "\" in client's folder page breadcrumb
+25 -17
View File
@@ -48,6 +48,19 @@ def rebuild_store(db_dirs: list[str]):
log.info("Rebuilding library... ✅")
def finalize(new_: list[str], removed_: list[str], db_dirs_: list[str]):
"""
Params:
new_: will be added to the database
removed_: will be removed from the database
db_dirs_: will be used to remove tracks that
are outside these directories from the database and store.
"""
sdb.remove_root_dirs(removed_)
sdb.add_root_dirs(new_)
rebuild_store(db_dirs_)
@api.route("/settings/add-root-dirs", methods=["POST"])
def add_root_dirs():
"""
@@ -66,33 +79,28 @@ def add_root_dirs():
except KeyError:
return msg, 400
def finalize(new_: list[str], removed_: list[str], db_dirs_: list[str]):
sdb.remove_root_dirs(removed_)
sdb.add_root_dirs(new_)
rebuild_store(db_dirs_)
# ---
db_dirs = sdb.get_root_dirs()
_h = "$home"
try:
if db_dirs[0] == _h and new_dirs[0] == _h.strip():
return {"msg": "Not changed!"}
db_home = any([d == _h for d in db_dirs]) # if $home is in db
incoming_home = any([d == _h for d in new_dirs]) # if $home is in incoming
if db_dirs[0] == _h:
sdb.remove_root_dirs(db_dirs)
# handle $home case
if db_home and incoming_home:
return {"msg": "Not changed!"}
if new_dirs[0] == _h:
finalize([_h], db_dirs, [settings.USER_HOME_DIR])
if db_home or incoming_home:
sdb.remove_root_dirs(db_dirs)
return {"root_dirs": [_h]}
except IndexError:
pass
if incoming_home:
finalize([_h], [], [settings.USER_HOME_DIR])
return {"root_dirs": [_h]}
# ---
for _dir in new_dirs:
children = get_child_dirs(_dir, db_dirs)
removed_dirs.extend(children)
# ---
for _dir in removed_dirs:
try: