From 1d9af05e9ecb3442bb2ef892e6db38f4c6f2aba3 Mon Sep 17 00:00:00 2001 From: ManishMadan2882 Date: Tue, 15 Jul 2025 15:34:16 +0530 Subject: [PATCH] (feat:storage) is dir fnc --- application/storage/base.py | 13 +++++++++++++ application/storage/local.py | 13 +++++++++++++ application/storage/s3.py | 25 +++++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/application/storage/base.py b/application/storage/base.py index 07f33c7b..50eaac7e 100644 --- a/application/storage/base.py +++ b/application/storage/base.py @@ -93,3 +93,16 @@ class BaseStorage(ABC): List[str]: List of file paths """ pass + + @abstractmethod + def is_directory(self, path: str) -> bool: + """ + Check if a path is a directory. + + Args: + path: Path to check + + Returns: + bool: True if the path is a directory + """ + pass diff --git a/application/storage/local.py b/application/storage/local.py index fb21f08d..b4530501 100644 --- a/application/storage/local.py +++ b/application/storage/local.py @@ -101,3 +101,16 @@ class LocalStorage(BaseStorage): raise FileNotFoundError(f"File not found: {full_path}") return processor_func(local_path=full_path, **kwargs) + + def is_directory(self, path: str) -> bool: + """ + Check if a path is a directory in local storage. + + Args: + path: Path to check + + Returns: + bool: True if the path is a directory, False otherwise + """ + full_path = self._get_full_path(path) + return os.path.isdir(full_path) diff --git a/application/storage/s3.py b/application/storage/s3.py index 1babb843..36333f1c 100644 --- a/application/storage/s3.py +++ b/application/storage/s3.py @@ -130,3 +130,28 @@ class S3Storage(BaseStorage): except Exception as e: logging.error(f"Error processing S3 file {path}: {e}", exc_info=True) raise + + def is_directory(self, path: str) -> bool: + """ + Check if a path is a directory in S3 storage. + + In S3, directories are virtual concepts. A path is considered a directory + if there are objects with the path as a prefix. + + Args: + path: Path to check + + Returns: + bool: True if the path is a directory, False otherwise + """ + # Ensure path ends with a slash if not empty + if path and not path.endswith('/'): + path += '/' + + response = self.s3.list_objects_v2( + Bucket=self.bucket_name, + Prefix=path, + MaxKeys=1 + ) + + return 'Contents' in response