pref: Optimize application performance

pull/634/head
zanllp 2024-05-26 14:18:34 +08:00
parent 2beba2763b
commit e7c0cb34d2
1 changed files with 9 additions and 9 deletions

View File

@ -32,17 +32,16 @@ def get_media_files_from_folder(folder_path):
list: 包含媒体文件完整路径的列表 list: 包含媒体文件完整路径的列表
""" """
media_files = [] media_files = []
for root, dirs, files in os.walk(folder_path): with os.scandir(folder_path) as entries:
for file in files: for entry in sorted(entries, key=lambda x: x.stat().st_mtime, reverse=True):
file_path = os.path.join(root, file) if entry.is_file() and is_valid_media_path(entry.path):
if is_valid_media_path(file_path): name = os.path.basename(entry.path)
name = os.path.basename(file_path) stat = entry.stat()
stat = os.stat(file_path)
date = get_formatted_date(stat.st_mtime) date = get_formatted_date(stat.st_mtime)
created_time = get_formatted_date(stat.st_birthtime if hasattr(stat, 'st_birthtime') else stat.st_ctime) created_time = get_formatted_date(stat.st_birthtime if hasattr(stat, 'st_birthtime') else stat.st_ctime)
media_files.append({ media_files.append({
"fullpath": file_path, "fullpath": entry.path,
"media_type": "video" if get_video_type(file_path) else "image", "media_type": "video" if get_video_type(entry.path) else "image",
"type": "file", "type": "file",
"date": date, "date": date,
"created_time": created_time, "created_time": created_time,
@ -50,4 +49,5 @@ def get_media_files_from_folder(folder_path):
}) })
if len(media_files) > 3: if len(media_files) > 3:
return media_files return media_files
return media_files return media_files