fix lag listing large file lists
This commit is contained in:
parent
af975b4bd9
commit
d144274c4c
48
app.py
48
app.py
@ -80,46 +80,36 @@ def list_directory_contents(directory, subpath):
|
||||
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('.'):
|
||||
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
|
||||
|
||||
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:
|
||||
if entry.is_dir(follow_symlinks=False):
|
||||
if entry.name in ["Transkription", "@eaDir"]:
|
||||
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):
|
||||
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': item, 'path': rel_path, 'file_type': file_type}
|
||||
|
||||
# Only check for transcription if it's a music file.
|
||||
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(item)[0]
|
||||
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)
|
||||
transcript_rel_path = transcript_rel_path.replace(os.sep, '/')
|
||||
file_entry['transcript_url'] = url_for('get_transcript', subpath=transcript_rel_path)
|
||||
file_entry['transcript_url'] = url_for('get_transcript', subpath=transcript_rel_path.replace(os.sep, '/'))
|
||||
else:
|
||||
file_entry['has_transcript'] = False
|
||||
else:
|
||||
@ -127,8 +117,10 @@ def list_directory_contents(directory, subpath):
|
||||
files.append(file_entry)
|
||||
except PermissionError:
|
||||
pass
|
||||
|
||||
return directories, files
|
||||
|
||||
|
||||
def generate_breadcrumbs(subpath=None):
|
||||
breadcrumbs = [{'name': 'Home', 'path': ''}]
|
||||
if subpath:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user