commit 86e1f8f763e85b6678de9ccea13d502f0df95496
parent 32f9d42d758368cd5898d3940a97691a931e19df
Author: Carlosokumu <carlosokumu254@gmail.com>
Date: Wed, 5 Nov 2025 21:42:13 +0300
feat: implement functionality to store object files
Diffstat:
1 file changed, 32 insertions(+), 3 deletions(-)
diff --git a/ungana/attachment/attachment_manager.py b/ungana/attachment/attachment_manager.py
@@ -1,17 +1,31 @@
import os
import hashlib
import mimetypes
+from ungana.config import load as load_config
+from ungana.logging.logging_manager import LoggingManager
+from ungana.store.resolve import resolve_backend_store
+
class AttachmentManager:
- def __init__(self):
- pass
- def create_attachment(self, file_path: str, ctx: str):
+ def __init__(self,logger=None):
+ self.cfg = load_config()
+ self.logger = logger or LoggingManager().get_logger("AttachmentManager")
+
+ def create_attachment(self, file_path: str, ctx: str,data = None):
if not os.path.exists(file_path):
raise FileNotFoundError(f"Attachment file not found: {file_path}")
if not os.access(file_path, os.R_OK):
raise PermissionError(f"Cannot read attachment file: {file_path}")
+
+ if isinstance(data, str):
+ data_bytes = data.encode("utf-8")
+ elif isinstance(data, bytes):
+ data_bytes = data
+ else:
+ data_bytes = b""
+ obj_stores = self.cfg.get("OBJ_STORES")
self.validate(file_path, ctx)
digest = self.digest(file_path)
@@ -24,6 +38,21 @@ class AttachmentManager:
"FMTTYPE": mime_type,
"CTX": ctx,
}
+
+ for obj_store in obj_stores:
+ try:
+ store = resolve_backend_store(obj_store)
+ url = store.put(obj_store, ctx, data_bytes)
+ self.logger.debug(f"Data for '{ctx}' successfully written to {url}")
+ except Exception as e:
+ self.logger.error(
+ f"Failed to write data for '{ctx}' to object store '{obj_store}': {e}",
+ exc_info=True
+ )
+ raise RuntimeError(
+ f"Failed to write data for '{ctx}' to object store '{obj_store}'"
+ )
+
return ("ATTACH", value, params)