fix lag listing large file lists

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

48
app.py
View File

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