feat: implement folder and folder track sorting

This commit is contained in:
cwilvx
2024-08-04 11:34:33 +03:00
parent c77d0927c7
commit cc2d017816
5 changed files with 118 additions and 25 deletions
+24 -8
View File
@@ -1,6 +1,7 @@
import os
from pathlib import Path
from app.lib.sortlib import sort_folders, sort_tracks
from app.logger import log
from app.models import Folder
from app.serializers.track import serialize_tracks
@@ -11,7 +12,7 @@ from app.utils.wintools import win_replace_slash
# from app.db.libdata import TrackTable as TrackDB
def create_folder(path: str, trackcount=0, foldercount=0) -> Folder:
def create_folder(path: str, trackcount=0) -> Folder:
"""
Creates a folder object from a path.
"""
@@ -22,7 +23,6 @@ def create_folder(path: str, trackcount=0, foldercount=0) -> Folder:
path=win_replace_slash(str(folder)) + "/",
is_sym=folder.is_symlink(),
trackcount=trackcount,
foldercount=foldercount,
)
@@ -46,14 +46,22 @@ def get_folders(paths: list[str]):
"""
folders = FolderStore.count_tracks_containing_paths(paths)
return [
create_folder(f["path"], f["trackcount"], foldercount=0)
create_folder(f["path"], f["trackcount"])
for f in folders
if f["trackcount"] > 0
]
def get_files_and_dirs(
path: str, start: int, end: int, tracks_only: bool = False, skip_empty_folders=True
path: str,
start: int,
limit: int,
tracksortby: str,
foldersortby: str,
tracksort_reverse: bool,
foldersort_reverse: bool,
tracks_only: bool = False,
skip_empty_folders=True,
):
"""
Given a path, returns a list of tracks and folders in that immediate path.
@@ -102,14 +110,17 @@ def get_files_and_dirs(
tracks = []
if files:
if end == -1:
end = len(files)
if limit == -1:
limit = len(files)
tracks = list(FolderStore.get_tracks_by_filepaths(files[start:end]))
tracks = list(FolderStore.get_tracks_by_filepaths(files))
tracks = sort_tracks(tracks, tracksortby, tracksort_reverse)
tracks = tracks[start : start + limit]
folders = []
if not tracks_only:
folders = get_folders(dirs)
folders = sort_folders(folders, foldersortby, foldersort_reverse)
if skip_empty_folders and len(folders) == 1 and len(tracks) == 0:
# INFO: When we only have one folder and no tracks,
@@ -118,7 +129,11 @@ def get_files_and_dirs(
return get_files_and_dirs(
folders[0].path,
start=start,
end=end,
limit=limit,
tracksortby=tracksortby,
foldersortby=foldersortby,
tracksort_reverse=tracksort_reverse,
foldersort_reverse=foldersort_reverse,
tracks_only=tracks_only,
skip_empty_folders=True,
)
@@ -127,4 +142,5 @@ def get_files_and_dirs(
"path": path,
"tracks": serialize_tracks(tracks),
"folders": folders,
"total": len(files),
}