fix lag listing large file lists

This commit is contained in:
lelo 2025-03-23 17:25:06 +01:00
parent af975b4bd9
commit d144274c4c

78
app.py
View File

@ -80,55 +80,47 @@ def list_directory_contents(directory, subpath):
allowed_image_exts = ('.jpg', '.jpeg', '.png', '.gif', '.bmp') allowed_image_exts = ('.jpg', '.jpeg', '.png', '.gif', '.bmp')
try: try:
for item in sorted(os.listdir(directory)): with os.scandir(directory) as it:
# Skip hidden folders and files starting with a dot. # Sorting by name if required.
if item.startswith('.'): for entry in sorted(it, key=lambda e: e.name):
continue # Skip hidden files and directories.
if entry.name.startswith('.'):
full_path = os.path.join(directory, item)
# Process directories.
if os.path.isdir(full_path):
# skip folder
skip_folder = ["Transkription", "@eaDir"]
if item in skip_folder:
continue continue
rel_path = os.path.join(subpath, item) if subpath else item
rel_path = rel_path.replace(os.sep, '/')
directories.append({'name': item, 'path': rel_path})
# Process files: either music or image files.
elif os.path.isfile(full_path) and (
item.lower().endswith(allowed_music_exts) or item.lower().endswith(allowed_image_exts)
):
rel_path = os.path.join(subpath, item) if subpath else item
rel_path = rel_path.replace(os.sep, '/')
# Determine the file type. if entry.is_dir(follow_symlinks=False):
if item.lower().endswith(allowed_music_exts): if entry.name in ["Transkription", "@eaDir"]:
file_type = 'music' continue
else: rel_path = os.path.join(subpath, entry.name) if subpath else entry.name
file_type = 'image' directories.append({'name': entry.name, 'path': rel_path.replace(os.sep, '/')})
elif entry.is_file(follow_symlinks=False):
file_entry = {'name': item, 'path': rel_path, 'file_type': file_type} lower_name = entry.name.lower()
if lower_name.endswith(allowed_music_exts) or lower_name.endswith(allowed_image_exts):
# Only check for transcription if it's a music file. rel_path = os.path.join(subpath, entry.name) if subpath else entry.name
if file_type == 'music' and transcription_exists: if lower_name.endswith(allowed_music_exts):
base_name = os.path.splitext(item)[0] file_type = 'music'
transcript_filename = base_name + '.md' else:
transcript_path = os.path.join(transcription_dir, transcript_filename) file_type = 'image'
if os.path.isfile(transcript_path): file_entry = {'name': entry.name, 'path': rel_path.replace(os.sep, '/'), 'file_type': file_type}
file_entry['has_transcript'] = True # Only check for transcription if it's a audio file.
transcript_rel_path = os.path.join(subpath, "Transkription", transcript_filename) if subpath else os.path.join("Transkription", transcript_filename) if file_type == 'music' and transcription_exists:
transcript_rel_path = transcript_rel_path.replace(os.sep, '/') base_name = os.path.splitext(entry.name)[0]
file_entry['transcript_url'] = url_for('get_transcript', subpath=transcript_rel_path) transcript_filename = base_name + '.md'
else: transcript_path = os.path.join(transcription_dir, transcript_filename)
file_entry['has_transcript'] = False if os.path.isfile(transcript_path):
else: file_entry['has_transcript'] = True
file_entry['has_transcript'] = False transcript_rel_path = os.path.join(subpath, "Transkription", transcript_filename) if subpath else os.path.join("Transkription", transcript_filename)
files.append(file_entry) file_entry['transcript_url'] = url_for('get_transcript', subpath=transcript_rel_path.replace(os.sep, '/'))
else:
file_entry['has_transcript'] = False
else:
file_entry['has_transcript'] = False
files.append(file_entry)
except PermissionError: except PermissionError:
pass pass
return directories, files return directories, files
def generate_breadcrumbs(subpath=None): def generate_breadcrumbs(subpath=None):
breadcrumbs = [{'name': 'Home', 'path': ''}] breadcrumbs = [{'name': 'Home', 'path': ''}]
if subpath: if subpath: