larxius commited on
Commit
3be0c8b
·
verified ·
1 Parent(s): 6177f18

Update backend/utils/firebase_storage.py

Browse files
Files changed (1) hide show
  1. backend/utils/firebase_storage.py +26 -8
backend/utils/firebase_storage.py CHANGED
@@ -59,13 +59,15 @@ def init_firebase() -> bool:
59
  from firebase_admin import credentials, storage
60
 
61
  if not firebase_admin._apps:
62
- # Support both file path and base64-encoded JSON (for HF Spaces)
63
  if os.path.isfile(creds_path):
64
  cred = credentials.Certificate(creds_path)
65
  else:
66
- # Treat as base64-encoded JSON string
67
- creds_json = base64.b64decode(creds_path).decode("utf-8")
68
- creds_dict = json.loads(creds_json)
 
 
69
  cred = credentials.Certificate(creds_dict)
70
 
71
  _app = firebase_admin.initialize_app(cred, {"storageBucket": bucket_name})
@@ -111,8 +113,16 @@ def upload_bytes(
111
  try:
112
  blob = _bucket.blob(destination_blob)
113
  blob.upload_from_string(data, content_type=content_type)
114
- blob.make_public()
115
- url = blob.public_url
 
 
 
 
 
 
 
 
116
  logger.info(f"[Firebase] Uploaded {destination_blob} ({len(data)} bytes)")
117
  return url
118
 
@@ -144,8 +154,16 @@ def upload_fileobj(
144
  blob = _bucket.blob(destination_blob)
145
  fileobj.seek(0)
146
  blob.upload_from_file(fileobj, content_type=content_type)
147
- blob.make_public()
148
- url = blob.public_url
 
 
 
 
 
 
 
 
149
  logger.info(f"[Firebase] Uploaded {destination_blob}")
150
  return url
151
 
 
59
  from firebase_admin import credentials, storage
60
 
61
  if not firebase_admin._apps:
62
+ # Support both file path, base64-encoded JSON, and direct JSON string
63
  if os.path.isfile(creds_path):
64
  cred = credentials.Certificate(creds_path)
65
  else:
66
+ try:
67
+ creds_json = base64.b64decode(creds_path).decode("utf-8")
68
+ creds_dict = json.loads(creds_json)
69
+ except Exception:
70
+ creds_dict = json.loads(creds_path)
71
  cred = credentials.Certificate(creds_dict)
72
 
73
  _app = firebase_admin.initialize_app(cred, {"storageBucket": bucket_name})
 
113
  try:
114
  blob = _bucket.blob(destination_blob)
115
  blob.upload_from_string(data, content_type=content_type)
116
+ try:
117
+ blob.make_public()
118
+ url = blob.public_url
119
+ except Exception as pub_err:
120
+ logger.warning(f"[Firebase] make_public failed (Uniform Bucket-Level Access active), generating public media URL: {pub_err}")
121
+ import urllib.parse
122
+ encoded_blob = urllib.parse.quote(destination_blob, safe='')
123
+ bucket_name = _bucket.name if _bucket else "storage"
124
+ url = f"https://firebasestorage.googleapis.com/v0/b/{bucket_name}/o/{encoded_blob}?alt=media"
125
+
126
  logger.info(f"[Firebase] Uploaded {destination_blob} ({len(data)} bytes)")
127
  return url
128
 
 
154
  blob = _bucket.blob(destination_blob)
155
  fileobj.seek(0)
156
  blob.upload_from_file(fileobj, content_type=content_type)
157
+ try:
158
+ blob.make_public()
159
+ url = blob.public_url
160
+ except Exception as pub_err:
161
+ logger.warning(f"[Firebase] make_public failed (Uniform Bucket-Level Access active), generating public media URL: {pub_err}")
162
+ import urllib.parse
163
+ encoded_blob = urllib.parse.quote(destination_blob, safe='')
164
+ bucket_name = _bucket.name if _bucket else "storage"
165
+ url = f"https://firebasestorage.googleapis.com/v0/b/{bucket_name}/o/{encoded_blob}?alt=media"
166
+
167
  logger.info(f"[Firebase] Uploaded {destination_blob}")
168
  return url
169