diff --git a/__init__.py b/__init__.py index 66837c1..cf5eefc 100644 --- a/__init__.py +++ b/__init__.py @@ -129,7 +129,7 @@ async def read_models(request): Scan all models and read their information. """ try: - result = services.scan_models() + result = services.scan_models(request) return web.json_response({"success": True, "data": result}) except Exception as e: error_msg = f"Read models failed: {str(e)}" @@ -231,7 +231,7 @@ async def download_model_info(request): post = await utils.get_request_body(request) try: scan_mode = post.get("scanMode", "diff") - await services.download_model_info(scan_mode) + await services.download_model_info(scan_mode, request) return web.json_response({"success": True}) except Exception as e: error_msg = f"Download model info failed: {str(e)}" @@ -287,7 +287,7 @@ async def migrate_legacy_information(request): Migrate legacy information. """ try: - await services.migrate_legacy_information() + await services.migrate_legacy_information(request) return web.json_response({"success": True}) except Exception as e: error_msg = f"Migrate model info failed: {str(e)}" diff --git a/py/config.py b/py/config.py index 87a7b3b..46471ce 100644 --- a/py/config.py +++ b/py/config.py @@ -11,6 +11,9 @@ "download": { "max_task_count": "ModelManager.Download.MaxTaskCount", }, + "scan": { + "include_hidden_files": "ModelManager.Scan.IncludeHiddenFiles" + }, } user_agent = "Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148" diff --git a/py/services.py b/py/services.py index f8afda7..d112548 100644 --- a/py/services.py +++ b/py/services.py @@ -8,14 +8,14 @@ from . import searcher -def scan_models(): +def scan_models(request): result = [] model_base_paths = utils.resolve_model_base_paths() for model_type in model_base_paths: folders, extensions = folder_paths.folder_names_and_paths[model_type] for path_index, base_path in enumerate(folders): - files = utils.recursive_search_files(base_path) + files = utils.recursive_search_files(base_path, request) models = folder_paths.filter_files_extensions(files, folder_paths.supported_pt_extensions) @@ -136,14 +136,14 @@ def fetch_model_info(model_page: str): return result -async def download_model_info(scan_mode: str): +async def download_model_info(scan_mode: str, request): utils.print_info(f"Download model info for {scan_mode}") model_base_paths = utils.resolve_model_base_paths() for model_type in model_base_paths: folders, extensions = folder_paths.folder_names_and_paths[model_type] for path_index, base_path in enumerate(folders): - files = utils.recursive_search_files(base_path) + files = utils.recursive_search_files(base_path, request) models = folder_paths.filter_files_extensions(files, folder_paths.supported_pt_extensions) @@ -192,7 +192,7 @@ async def download_model_info(scan_mode: str): utils.print_debug("Completed scan model information.") -async def migrate_legacy_information(): +async def migrate_legacy_information(request): import json import yaml from PIL import Image @@ -204,7 +204,7 @@ async def migrate_legacy_information(): folders, extensions = folder_paths.folder_names_and_paths[model_type] for path_index, base_path in enumerate(folders): - files = utils.recursive_search_files(base_path) + files = utils.recursive_search_files(base_path, request) models = folder_paths.filter_files_extensions(files, folder_paths.supported_pt_extensions) diff --git a/py/utils.py b/py/utils.py index 2152153..275e636 100644 --- a/py/utils.py +++ b/py/utils.py @@ -162,9 +162,29 @@ def get_download_path(): return download_path -def recursive_search_files(directory: str): - files, folder_all = folder_paths.recursive_search(directory, excluded_dir_names=[".git"]) - return [normalize_path(f) for f in files] +def recursive_search_files(directory: str, request): + if not os.path.isdir(directory): + return [] + + excluded_dir_names = [".git"] + result = [] + include_hidden_files = get_setting_value(request, "scan.include_hidden_files", False) + + for dirpath, subdirs, filenames in os.walk(directory, followlinks=True, topdown=True): + subdirs[:] = [d for d in subdirs if d not in excluded_dir_names] + if not include_hidden_files: + subdirs[:] = [d for d in subdirs if not d.startswith(".")] + filenames[:] = [f for f in filenames if not f.startswith(".")] + + for file_name in filenames: + try: + relative_path = os.path.relpath(os.path.join(dirpath, file_name), directory) + result.append(relative_path) + except: + logging.warning(f"Warning: Unable to access {file_name}. Skipping this file.") + continue + + return [normalize_path(f) for f in result] def search_files(directory: str): diff --git a/src/hooks/config.ts b/src/hooks/config.ts index 0674d3d..9465afb 100644 --- a/src/hooks/config.ts +++ b/src/hooks/config.ts @@ -239,5 +239,12 @@ function useAddConfigSettings(store: import('hooks/store').StoreProvider) { }) }, }) + + app.ui?.settings.addSetting({ + id: 'ModelManager.Scan.IncludeHiddenFiles', + name: 'Include hidden files(start with .)', + defaultValue: false, + type: 'boolean', + }) }) }