From d861bc690e8d37f1c282fbec7cea6db9301c2435 Mon Sep 17 00:00:00 2001 From: psychedelicious <4822129+psychedelicious@users.noreply.github.com> Date: Tue, 30 Apr 2024 21:09:18 +1000 Subject: [PATCH] feat(mm): handle PC_PATH_MAX on external drives on macOS `PC_PATH_MAX` doesn't exist for (some?) external drives on macOS. We need error handling when retrieving this value. Also added error handling for `PC_NAME_MAX` just in case. This does work for me for external drives on macOS, though. Closes #6277 --- .../app/services/download/download_default.py | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/invokeai/app/services/download/download_default.py b/invokeai/app/services/download/download_default.py index f393a18dcb..7d8229fba1 100644 --- a/invokeai/app/services/download/download_default.py +++ b/invokeai/app/services/download/download_default.py @@ -318,10 +318,8 @@ class DownloadQueueService(DownloadQueueServiceBase): in_progress_path.rename(job.download_path) def _validate_filename(self, directory: str, filename: str) -> bool: - pc_name_max = os.pathconf(directory, "PC_NAME_MAX") if hasattr(os, "pathconf") else 260 # hardcoded for windows - pc_path_max = ( - os.pathconf(directory, "PC_PATH_MAX") if hasattr(os, "pathconf") else 32767 - ) # hardcoded for windows with long names enabled + pc_name_max = get_pc_name_max(directory) + pc_path_max = get_pc_path_max(directory) if "/" in filename: return False if filename.startswith(".."): @@ -419,6 +417,26 @@ class DownloadQueueService(DownloadQueueServiceBase): self._logger.warning(excp) +def get_pc_name_max(directory: str) -> int: + if hasattr(os, "pathconf"): + try: + return os.pathconf(directory, "PC_NAME_MAX") + except OSError: + # macOS w/ external drives raise OSError + pass + return 260 # hardcoded for windows + + +def get_pc_path_max(directory: str) -> int: + if hasattr(os, "pathconf"): + try: + return os.pathconf(directory, "PC_PATH_MAX") + except OSError: + # some platforms may not have this value + pass + return 32767 # hardcoded for windows with long names enabled + + # Example on_progress event handler to display a TQDM status bar # Activate with: # download_service.download(DownloadJob('http://foo.bar/baz', '/tmp', on_progress=TqdmProgress().update))