diff --git a/backend/utils/other/storage.py b/backend/utils/other/storage.py index fc302c3cee..fe35c4a020 100644 --- a/backend/utils/other/storage.py +++ b/backend/utils/other/storage.py @@ -11,7 +11,7 @@ from google.oauth2 import service_account from google.cloud.storage import transfer_manager from google.cloud.exceptions import NotFound as BlobNotFound -from google.cloud.exceptions import NotFound +from google.cloud.exceptions import NotFound, Forbidden from database.redis_db import cache_signed_url, get_cached_signed_url from utils import encryption @@ -47,20 +47,27 @@ def upload_profile_audio(file_path: str, uid: str): def get_user_has_speech_profile(uid: str, max_age_days: int = None) -> bool: - bucket = storage_client.bucket(speech_profiles_bucket) - blob = bucket.blob(f'{uid}/speech_profile.wav') - if not blob.exists(): + if not speech_profiles_bucket: return False - # Check age if max_age_days is specified - if max_age_days is not None: - blob.reload() - if blob.time_created: - age = datetime.datetime.now(datetime.timezone.utc) - blob.time_created - if age.days > max_age_days: - return False + try: + bucket = storage_client.bucket(speech_profiles_bucket) + blob = bucket.blob(f'{uid}/speech_profile.wav') + + if not blob.exists(): + return False - return True + # Check age if max_age_days is specified + if max_age_days is not None: + blob.reload() + if blob.time_created: + age = datetime.datetime.now(datetime.timezone.utc) - blob.time_created + if age.days > max_age_days: + return False + + return True + except Forbidden: + return False def get_profile_audio_if_exists(uid: str, download: bool = True) -> str: