mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2025-04-19 19:03:51 +00:00
Optimize file scanning using scandir
This commit is contained in:
parent
7bba21af47
commit
0e86405198
@ -35,7 +35,7 @@ class OutputManager:
|
|||||||
# TODO get video cover preview
|
# TODO get video cover preview
|
||||||
|
|
||||||
elif os.path.isdir(filepath):
|
elif os.path.isdir(filepath):
|
||||||
files = self.get_items_by_directory(filepath)
|
files = self.get_folder_items(filepath)
|
||||||
return web.json_response(files)
|
return web.json_response(files)
|
||||||
|
|
||||||
return web.Response(status=404)
|
return web.Response(status=404)
|
||||||
@ -59,28 +59,27 @@ class OutputManager:
|
|||||||
def get_output_filepath(self, pathname: str):
|
def get_output_filepath(self, pathname: str):
|
||||||
return f"{self.output_uri}/{pathname}"
|
return f"{self.output_uri}/{pathname}"
|
||||||
|
|
||||||
def get_items_by_directory(self, pathname: str):
|
def get_folder_items(self, folder: str):
|
||||||
result = []
|
result = []
|
||||||
items = os.listdir(pathname)
|
|
||||||
|
|
||||||
for name in items:
|
with os.scandir(folder) as it:
|
||||||
filepath = os.path.join(pathname, name)
|
for entry in it:
|
||||||
|
filepath = entry.path
|
||||||
|
is_dir = entry.is_dir()
|
||||||
|
|
||||||
if os.path.isfile(filepath) and not self.assert_file_type(filepath, ["image", "video", "audio"]):
|
if not is_dir and not self.assert_file_type(filepath, ["image", "video", "audio"]):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
state = os.stat(filepath)
|
state = entry.stat()
|
||||||
is_dir = os.path.isdir(filepath)
|
result.append(
|
||||||
|
{
|
||||||
result.append(
|
"name": entry.name,
|
||||||
{
|
"type": "folder" if entry.is_dir() else self.get_file_content_type(filepath),
|
||||||
"name": name,
|
"size": 0 if is_dir else state.st_size,
|
||||||
"type": "folder" if is_dir else self.get_file_content_type(filepath),
|
"createdAt": round(state.st_ctime_ns / 1000000),
|
||||||
"size": 0 if is_dir else state.st_size,
|
"updatedAt": round(state.st_mtime_ns / 1000000),
|
||||||
"createdAt": round(state.st_ctime_ns / 1000000),
|
}
|
||||||
"updatedAt": round(state.st_mtime_ns / 1000000),
|
)
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user