Skip to content

Commit 47d7308

Browse files
committed
Add as_bytes option
1 parent 61f061a commit 47d7308

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

app/backend/approaches/approach.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,13 +414,13 @@ async def download_blob_as_base64(self, blob_url: str, user_oid: Optional[str] =
414414
blob_path = blob_url
415415

416416
# Download the blob using the appropriate client
417-
blob_manager = self.global_blob_manager
417+
blob_bytes = None
418418
if ".dfs.core.windows.net" in blob_url and self.user_blob_manager:
419-
blob_manager = self.user_blob_manager
420-
blob_downloader = await blob_manager.download_blob(blob_path, user_oid=user_oid)
421-
if blob_downloader is not None:
422-
blob = await blob_downloader.readall()
423-
img = base64.b64encode(blob).decode("utf-8")
419+
blob_bytes = await self.user_blob_manager.download_blob(blob_path, user_oid=user_oid, as_bytes=True)
420+
elif self.global_blob_manager:
421+
blob_bytes = await self.global_blob_manager.download_blob(blob_path, as_bytes=True)
422+
if blob_bytes and isinstance(blob_bytes, (bytes)):
423+
img = base64.b64encode(blob_bytes).decode("utf-8")
424424
return f"data:image/png;base64,{img}"
425425
return None
426426

app/backend/prepdocslib/blobmanager.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ async def upload_document_image(
103103
raise NotImplementedError("Subclasses must implement this method")
104104

105105
async def download_blob(
106-
self, blob_path: str, user_oid: Optional[str] = None
107-
) -> Optional[Union[BlobStorageStreamDownloader, AdlsBlobStorageStreamDownloader]]:
106+
self, blob_path: str, user_oid: Optional[str] = None, as_bytes: bool = False
107+
) -> Optional[Union[BlobStorageStreamDownloader, AdlsBlobStorageStreamDownloader, bytes]]:
108108
"""
109109
Downloads a blob from Azure Storage.
110110
If user_oid is provided, it checks if the blob belongs to the user.
@@ -253,7 +253,7 @@ async def upload_document_image(
253253
return unquote(file_client.url)
254254

255255
async def download_blob(
256-
self, blob_path: str, user_oid: Optional[str] = None
256+
self, blob_path: str, user_oid: Optional[str] = None, as_bytes: bool = False
257257
) -> Optional[Union[AdlsBlobStorageStreamDownloader, BlobStorageStreamDownloader]]:
258258
"""
259259
Downloads a blob from Azure Data Lake Storage.
@@ -290,7 +290,10 @@ async def download_blob(
290290
user_directory_client = await self._ensure_directory(directory_path=directory_path, user_oid=user_oid)
291291
file_client = user_directory_client.get_file_client(filename)
292292
blob = await file_client.download_file()
293-
return blob
293+
if as_bytes:
294+
return await blob.readall()
295+
else:
296+
return blob
294297
except ResourceNotFoundError:
295298
logger.warning(f"Directory or file not found: {directory_path}/{filename}")
296299
return None
@@ -444,8 +447,8 @@ async def upload_document_image(
444447
return blob_client.url
445448

446449
async def download_blob(
447-
self, blob_path: str, user_oid: Optional[str] = None
448-
) -> Optional[Union[BlobStorageStreamDownloader, AdlsBlobStorageStreamDownloader]]:
450+
self, blob_path: str, user_oid: Optional[str] = None, as_bytes: bool = False
451+
) -> Optional[Union[BlobStorageStreamDownloader, AdlsBlobStorageStreamDownloader, bytes]]:
449452
if user_oid is not None:
450453
raise ValueError(
451454
"user_oid is not supported for BlobManager. Use AdlsBlobManager for user-specific operations."
@@ -462,7 +465,10 @@ async def download_blob(
462465
if not blob.properties:
463466
logger.warning(f"No blob exists for {blob_path}")
464467
return None
465-
return blob
468+
if as_bytes:
469+
return await blob.readall()
470+
else:
471+
return blob
466472
except ResourceNotFoundError:
467473
logger.warning("Blob not found: %s", blob_path)
468474
return None

0 commit comments

Comments
 (0)