From 8ac3b8e29462b25537abc3b6bdf0a6fa34d27329 Mon Sep 17 00:00:00 2001 From: cwilvx Date: Thu, 13 Mar 2025 00:23:05 +0300 Subject: [PATCH] fix folders endpoint on windows + try/catch ffmpeg 404s in pydub --- app/api/folder.py | 7 +++---- app/lib/folderslib.py | 27 ++++++++------------------- app/lib/trackslib.py | 14 ++++++++++---- app/store/folder.py | 1 - 4 files changed, 21 insertions(+), 28 deletions(-) diff --git a/app/api/folder.py b/app/api/folder.py index 6aecff1e..254a4086 100644 --- a/app/api/folder.py +++ b/app/api/folder.py @@ -78,7 +78,6 @@ def get_folder_tree(body: FolderTree): config = UserConfig() root_dirs = config.rootDirs - print("root_dirs", root_dirs) try: if req_dir == "$home" and root_dirs[0] == "$home": @@ -122,11 +121,11 @@ def get_all_drives(is_win: bool = False): """ Returns a list of all the drives on a Windows machine. """ - drives = psutil.disk_partitions(all=True) - drives = [d.mountpoint for d in drives] + drives_ = psutil.disk_partitions(all=True) + drives = [Path(d.mountpoint).as_posix() for d in drives_] if is_win: - drives = [win_replace_slash(d) for d in drives] + return drives else: remove = ( "/boot", diff --git a/app/lib/folderslib.py b/app/lib/folderslib.py index b04a2933..a2d59411 100644 --- a/app/lib/folderslib.py +++ b/app/lib/folderslib.py @@ -26,19 +26,6 @@ def create_folder(path: str, trackcount=0) -> Folder: ) -def get_first_child_from_path(root: str, maybe_child: str): - """ - Given a root path and a path, returns the first child from the root path. - """ - if not maybe_child.startswith(root) or maybe_child == root: - return None - - children = maybe_child.replace(root, "") - first = Path(children).parts[0] - - return os.path.join(root, first) - - def get_folders(paths: list[str]): """ Filters out folders that don't have any tracks and @@ -68,7 +55,7 @@ def get_files_and_dirs( Can recursively call itself to skip through empty folders. """ - + # TODO: Replace os.path with pathlib try: entries = os.scandir(path) except FileNotFoundError: @@ -80,17 +67,19 @@ def get_files_and_dirs( dirs, files = [], [] - for entry in entries: - ext = os.path.splitext(entry.name)[1].lower() + for entry_ in entries: + entry = Path(entry_.path) + ext = entry.suffix.lower() if entry.is_dir() and not entry.name.startswith("."): - dir = win_replace_slash(entry.path) + dir = (entry / "").as_posix() + # add a trailing slash to the folder path # to avoid matching a folder starting with the same name as the root path # eg. .../Music and .../Music VideosI - dirs.append(os.path.join(dir, "")) + dirs.append(dir) elif entry.is_file() and ext in SUPPORTED_FILES: - files.append(win_replace_slash(entry.path)) + files.append(entry.as_posix()) files_ = [] diff --git a/app/lib/trackslib.py b/app/lib/trackslib.py index 438116a6..dc3d621b 100644 --- a/app/lib/trackslib.py +++ b/app/lib/trackslib.py @@ -14,8 +14,11 @@ def get_leading_silence_end(filepath: str): Returns the leading silence of a track. """ format = filepath.split(".")[-1] - audio = AudioSegment.from_file(filepath, format=format) - silence = detect_leading_silence(audio, silence_threshold=-40.0, chunk_size=10) + try: + audio = AudioSegment.from_file(filepath, format=format) + silence = detect_leading_silence(audio, silence_threshold=-40.0, chunk_size=10) + except Exception as e: + return 0 return silence if silence > 1000 else 0 @@ -25,8 +28,11 @@ def get_trailing_silence_start(filepath: str): Returns the trailing silence of a track. """ format = filepath.split(".")[-1] - audio = AudioSegment.from_file(filepath, format=format) - duration = len(audio) + try: + audio = AudioSegment.from_file(filepath, format=format) + duration = len(audio) + except Exception as e: + return None audio = audio[-30000:] if len(audio) > 30000 else audio silence_groups = detect_silence(audio, silence_thresh=-40.0, seek_step=10) diff --git a/app/store/folder.py b/app/store/folder.py index 52d7745f..c67b7afb 100644 --- a/app/store/folder.py +++ b/app/store/folder.py @@ -63,7 +63,6 @@ class FolderStore: results = [ {"path": path, "trackcount": count} for path, count in zip(paths, res) ] - print("results", results) return results