diff --git a/helperfunctions.py b/helperfunctions.py index f39356f..7ad94b1 100644 --- a/helperfunctions.py +++ b/helperfunctions.py @@ -4,6 +4,10 @@ import os import sqlite3 from datetime import datetime, timedelta from typing import Optional +import auth + +app_config = auth.return_app_config() +BASE_DIR = os.path.realpath(app_config['BASE_DIR']) log_db = sqlite3.connect("access_log.db", check_same_thread=False) @@ -143,15 +147,15 @@ def generate_top_list(category): cursor = log_db.execute(query, params_for_filter) rows = cursor.fetchall() - # Filter by allowed basefolders + # Filter by allowed base folders allowed_basefolders = list(session['folders'].keys()) rows = [ (rel_path, access_count) for rel_path, access_count in rows if any(rel_path.startswith(folder) for folder in allowed_basefolders) ] - # Convert rows to a list of dictionaries and add category - rows = [ + # Convert rows to a list of dicts and add category + records = [ { 'rel_path': rel_path, 'access_count': access_count, @@ -159,15 +163,18 @@ def generate_top_list(category): } for rel_path, access_count in rows ] - rows = [r for r in rows if r['category'] == category][:20] + # Filter by requested category and limit + records = [r for r in records if r['category'] == category][:20] - filelist = [ - { - 'name': rel_path.split('/')[-1], - 'path': rel_path, - 'file_type': 'music' - } - for rel_path in [r['rel_path'] for r in rows] - ] + # Build file list and check existence + filelist = [] + for record in records: + rel_path = record['rel_path'] + if os.path.exists(os.path.join(BASE_DIR, rel_path)): # ensure file exists on disk // slow operation. maybe improve later + filelist.append({ + 'name': os.path.basename(rel_path), + 'path': rel_path, + 'file_type': 'music' + }) return filelist