diff --git a/app.py b/app.py index c175c65..5f41507 100755 --- a/app.py +++ b/app.py @@ -74,61 +74,53 @@ def list_directory_contents(directory, subpath): files = [] transcription_dir = os.path.join(directory, "Transkription") transcription_exists = os.path.isdir(transcription_dir) - + # Define allowed file extensions. allowed_music_exts = ('.mp3',) allowed_image_exts = ('.jpg', '.jpeg', '.png', '.gif', '.bmp') try: - for item in sorted(os.listdir(directory)): - # Skip hidden folders and files starting with a dot. - if item.startswith('.'): - continue - - 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: + with os.scandir(directory) as it: + # Sorting by name if required. + for entry in sorted(it, key=lambda e: e.name): + # Skip hidden files and directories. + if entry.name.startswith('.'): 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 item.lower().endswith(allowed_music_exts): - file_type = 'music' - else: - file_type = 'image' - - file_entry = {'name': item, 'path': rel_path, 'file_type': file_type} - - # Only check for transcription if it's a music file. - if file_type == 'music' and transcription_exists: - base_name = os.path.splitext(item)[0] - transcript_filename = base_name + '.md' - transcript_path = os.path.join(transcription_dir, transcript_filename) - if os.path.isfile(transcript_path): - 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 = transcript_rel_path.replace(os.sep, '/') - file_entry['transcript_url'] = url_for('get_transcript', subpath=transcript_rel_path) - else: - file_entry['has_transcript'] = False - else: - file_entry['has_transcript'] = False - files.append(file_entry) + + if entry.is_dir(follow_symlinks=False): + if entry.name in ["Transkription", "@eaDir"]: + continue + rel_path = os.path.join(subpath, entry.name) if subpath else entry.name + directories.append({'name': entry.name, 'path': rel_path.replace(os.sep, '/')}) + elif entry.is_file(follow_symlinks=False): + lower_name = entry.name.lower() + 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 + if lower_name.endswith(allowed_music_exts): + file_type = 'music' + else: + 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. + if file_type == 'music' and transcription_exists: + base_name = os.path.splitext(entry.name)[0] + transcript_filename = base_name + '.md' + transcript_path = os.path.join(transcription_dir, transcript_filename) + if os.path.isfile(transcript_path): + file_entry['has_transcript'] = True + transcript_rel_path = os.path.join(subpath, "Transkription", transcript_filename) if subpath else os.path.join("Transkription", transcript_filename) + 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: pass + return directories, files + def generate_breadcrumbs(subpath=None): breadcrumbs = [{'name': 'Home', 'path': ''}] if subpath: