fix lag listing large file lists

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

82
app.py
View File

@ -74,61 +74,53 @@ def list_directory_contents(directory, subpath):
files = [] files = []
transcription_dir = os.path.join(directory, "Transkription") transcription_dir = os.path.join(directory, "Transkription")
transcription_exists = os.path.isdir(transcription_dir) transcription_exists = os.path.isdir(transcription_dir)
# Define allowed file extensions. # Define allowed file extensions.
allowed_music_exts = ('.mp3',) allowed_music_exts = ('.mp3',)
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, '/') if entry.is_dir(follow_symlinks=False):
directories.append({'name': item, 'path': rel_path}) if entry.name in ["Transkription", "@eaDir"]:
# Process files: either music or image files. continue
elif os.path.isfile(full_path) and ( rel_path = os.path.join(subpath, entry.name) if subpath else entry.name
item.lower().endswith(allowed_music_exts) or item.lower().endswith(allowed_image_exts) directories.append({'name': entry.name, 'path': rel_path.replace(os.sep, '/')})
): elif entry.is_file(follow_symlinks=False):
rel_path = os.path.join(subpath, item) if subpath else item lower_name = entry.name.lower()
rel_path = rel_path.replace(os.sep, '/') if lower_name.endswith(allowed_music_exts) or lower_name.endswith(allowed_image_exts):
rel_path = os.path.join(subpath, entry.name) if subpath else entry.name
# Determine the file type. if lower_name.endswith(allowed_music_exts):
if item.lower().endswith(allowed_music_exts): file_type = 'music'
file_type = 'music' else:
else: file_type = 'image'
file_type = 'image' file_entry = {'name': entry.name, 'path': rel_path.replace(os.sep, '/'), 'file_type': file_type}
# Only check for transcription if it's a audio file.
file_entry = {'name': item, 'path': rel_path, 'file_type': file_type} if file_type == 'music' and transcription_exists:
base_name = os.path.splitext(entry.name)[0]
# Only check for transcription if it's a music file. transcript_filename = base_name + '.md'
if file_type == 'music' and transcription_exists: transcript_path = os.path.join(transcription_dir, transcript_filename)
base_name = os.path.splitext(item)[0] if os.path.isfile(transcript_path):
transcript_filename = base_name + '.md' file_entry['has_transcript'] = True
transcript_path = os.path.join(transcription_dir, transcript_filename) transcript_rel_path = os.path.join(subpath, "Transkription", transcript_filename) if subpath else os.path.join("Transkription", transcript_filename)
if os.path.isfile(transcript_path): file_entry['transcript_url'] = url_for('get_transcript', subpath=transcript_rel_path.replace(os.sep, '/'))
file_entry['has_transcript'] = True else:
transcript_rel_path = os.path.join(subpath, "Transkription", transcript_filename) if subpath else os.path.join("Transkription", transcript_filename) file_entry['has_transcript'] = False
transcript_rel_path = transcript_rel_path.replace(os.sep, '/') else:
file_entry['transcript_url'] = url_for('get_transcript', subpath=transcript_rel_path) file_entry['has_transcript'] = False
else: files.append(file_entry)
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: