check if files generated from acces log still exist

This commit is contained in:
lelo 2025-06-14 19:23:31 +00:00
parent f1ff833d89
commit 26527b2ef2

View File

@ -4,6 +4,10 @@ import os
import sqlite3 import sqlite3
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Optional 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) 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) cursor = log_db.execute(query, params_for_filter)
rows = cursor.fetchall() rows = cursor.fetchall()
# Filter by allowed basefolders # Filter by allowed base folders
allowed_basefolders = list(session['folders'].keys()) allowed_basefolders = list(session['folders'].keys())
rows = [ rows = [
(rel_path, access_count) for rel_path, access_count in rows (rel_path, access_count) for rel_path, access_count in rows
if any(rel_path.startswith(folder) for folder in allowed_basefolders) if any(rel_path.startswith(folder) for folder in allowed_basefolders)
] ]
# Convert rows to a list of dictionaries and add category # Convert rows to a list of dicts and add category
rows = [ records = [
{ {
'rel_path': rel_path, 'rel_path': rel_path,
'access_count': access_count, 'access_count': access_count,
@ -159,15 +163,18 @@ def generate_top_list(category):
} }
for rel_path, access_count in rows 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 = [ # Build file list and check existence
{ filelist = []
'name': rel_path.split('/')[-1], for record in records:
'path': rel_path, rel_path = record['rel_path']
'file_type': 'music' if os.path.exists(os.path.join(BASE_DIR, rel_path)): # ensure file exists on disk // slow operation. maybe improve later
} filelist.append({
for rel_path in [r['rel_path'] for r in rows] 'name': os.path.basename(rel_path),
] 'path': rel_path,
'file_type': 'music'
})
return filelist return filelist