larxius commited on
Commit
cd1d605
·
verified ·
1 Parent(s): cabfd4a

Support base64-encoded Firebase credentials for HF Spaces

Browse files
Files changed (1) hide show
  1. backend/utils/firebase_storage.py +12 -2
backend/utils/firebase_storage.py CHANGED
@@ -5,12 +5,14 @@ Uploads organization logos and scan report PDFs to Firebase Storage
5
  and returns public/signed download URLs.
6
 
7
  Env vars required:
8
- FIREBASE_CREDENTIALS — path to serviceAccountKey.json
9
  FIREBASE_STORAGE_BUCKET — e.g. your-project.appspot.com
10
  """
11
 
12
  import os
13
  import io
 
 
14
  import logging
15
  from typing import Optional
16
 
@@ -57,7 +59,15 @@ def init_firebase() -> bool:
57
  from firebase_admin import credentials, storage
58
 
59
  if not firebase_admin._apps:
60
- cred = credentials.Certificate(creds_path)
 
 
 
 
 
 
 
 
61
  _app = firebase_admin.initialize_app(cred, {"storageBucket": bucket_name})
62
 
63
  _bucket = storage.bucket()
 
5
  and returns public/signed download URLs.
6
 
7
  Env vars required:
8
+ FIREBASE_CREDENTIALS — path to serviceAccountKey.json OR base64-encoded JSON
9
  FIREBASE_STORAGE_BUCKET — e.g. your-project.appspot.com
10
  """
11
 
12
  import os
13
  import io
14
+ import base64
15
+ import json
16
  import logging
17
  from typing import Optional
18
 
 
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})
72
 
73
  _bucket = storage.bucket()