Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files
src/firebase/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
src/firebase/__pycache__/firebase_config.cpython-311.pyc
ADDED
|
Binary file (1.8 kB). View file
|
|
|
src/firebase/__pycache__/firebase_provider.cpython-311.pyc
ADDED
|
Binary file (4.36 kB). View file
|
|
|
src/firebase/firebase_config.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import firebase_admin
|
| 2 |
+
from firebase_admin import credentials
|
| 3 |
+
from firebase_admin import storage
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
# Load environment variables
|
| 9 |
+
load_dotenv()
|
| 10 |
+
firebase_url_storageBucket = os.getenv("URL_STORAGEBUCKET")
|
| 11 |
+
|
| 12 |
+
# Get credentials from environment variables
|
| 13 |
+
credential_firebase = {
|
| 14 |
+
"type": os.getenv("TYPE"),
|
| 15 |
+
"project_id": os.getenv("PROJECT_ID"),
|
| 16 |
+
"private_key_id": os.getenv("PRIVATE_KEY_ID"),
|
| 17 |
+
"private_key": os.getenv("PRIVATE_KEY"),
|
| 18 |
+
"client_email": os.getenv("CLIENT_EMAIL"),
|
| 19 |
+
"client_id": os.getenv("CLIENT_ID"),
|
| 20 |
+
"auth_uri": os.getenv("AUTH_URI"),
|
| 21 |
+
"token_uri": os.getenv("TOKEN_URI"),
|
| 22 |
+
"auth_provider_x509_cert_url": os.getenv("AUTH_PROVIDER_X509_CERT_URL"),
|
| 23 |
+
"client_x509_cert_url": os.getenv("CLIENT_X509_CERT_URL"),
|
| 24 |
+
"universe_domain": os.getenv("UNIVERSE_DOMAIN"),
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# Check if the app is not initialized yet
|
| 29 |
+
if not firebase_admin._apps:
|
| 30 |
+
# Initialize the app with the credentials
|
| 31 |
+
cred = credentials.Certificate(credential_firebase)
|
| 32 |
+
firebase_admin.initialize_app(cred, {"storageBucket": firebase_url_storageBucket})
|
| 33 |
+
|
| 34 |
+
# Initialize Firestore
|
| 35 |
+
firebase_bucket = storage.bucket(app=firebase_admin.get_app())
|
| 36 |
+
print("Storage connected")
|
src/firebase/firebase_provider.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .firebase_config import firebase_bucket
|
| 2 |
+
import base64
|
| 3 |
+
import os
|
| 4 |
+
import tempfile
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
|
| 8 |
+
def upload_file_to_storage(file_path, file_name):
|
| 9 |
+
"""
|
| 10 |
+
Upload a file to Firebase Storage
|
| 11 |
+
param:
|
| 12 |
+
file_path: str - The path of the file on local machine to be uploaded
|
| 13 |
+
return:
|
| 14 |
+
str - The public URL of the uploaded file
|
| 15 |
+
"""
|
| 16 |
+
blob = firebase_bucket.blob(file_name)
|
| 17 |
+
blob.upload_from_filename(file_path)
|
| 18 |
+
blob.make_public()
|
| 19 |
+
|
| 20 |
+
return blob.public_url
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def delete_file_from_storage(file_name):
|
| 25 |
+
"""
|
| 26 |
+
Delete a file from Firebase Storage
|
| 27 |
+
param:
|
| 28 |
+
file_name: str - The name of the file to be deleted
|
| 29 |
+
return:
|
| 30 |
+
bool - True if the file is deleted successfully, False if the file is not found
|
| 31 |
+
"""
|
| 32 |
+
try:
|
| 33 |
+
blob = firebase_bucket.blob(file_name)
|
| 34 |
+
blob.delete()
|
| 35 |
+
return True
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print("Error:", e)
|
| 38 |
+
return False
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def list_all_files_in_storage():
|
| 42 |
+
"""
|
| 43 |
+
View all files in Firebase Storage
|
| 44 |
+
return:
|
| 45 |
+
dict - Dictionary with keys are names and values are url of all files in Firebase Storage
|
| 46 |
+
"""
|
| 47 |
+
blobs = firebase_bucket.list_blobs()
|
| 48 |
+
blob_dict = {blob.name: blob.public_url for blob in blobs}
|
| 49 |
+
return blob_dict
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def download_file_from_storage(file_name, destination_path):
|
| 53 |
+
"""
|
| 54 |
+
Download a file from Firebase Storage
|
| 55 |
+
param:
|
| 56 |
+
file_name: str - The name of the file to be downloaded
|
| 57 |
+
destination_path: str - The path to save the downloaded file
|
| 58 |
+
return:
|
| 59 |
+
bool - True if the file is downloaded successfully, False if the file is not found
|
| 60 |
+
"""
|
| 61 |
+
try:
|
| 62 |
+
blob = firebase_bucket.blob(file_name)
|
| 63 |
+
blob.download_to_filename(destination_path)
|
| 64 |
+
print("da tai xun thanh cong")
|
| 65 |
+
return True
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print("Error:", e)
|
| 68 |
+
return False
|
| 69 |
+
def upload_base64_image_to_storage(base64_image, file_name):
|
| 70 |
+
"""
|
| 71 |
+
Upload a base64 image to Firebase Storage
|
| 72 |
+
param:
|
| 73 |
+
base64_image: str - The base64 encoded image
|
| 74 |
+
file_name: str - The name of the file to be uploaded
|
| 75 |
+
return:
|
| 76 |
+
str - The public URL of the uploaded file
|
| 77 |
+
"""
|
| 78 |
+
try:
|
| 79 |
+
# Decode the base64 image
|
| 80 |
+
image_data = base64.b64decode(base64_image)
|
| 81 |
+
|
| 82 |
+
# Convert the decoded image to a JPG file
|
| 83 |
+
image = Image.open(io.BytesIO(image_data))
|
| 84 |
+
temp_file_path = f"{tempfile.gettempdir()}/{file_name}.jpg"
|
| 85 |
+
image.save(temp_file_path, format="JPEG")
|
| 86 |
+
|
| 87 |
+
# Upload the JPG file to Firebase Storage
|
| 88 |
+
public_url = upload_file_to_storage(temp_file_path, f"{file_name}.jpg")
|
| 89 |
+
|
| 90 |
+
# Remove the temporary file
|
| 91 |
+
os.remove(temp_file_path)
|
| 92 |
+
|
| 93 |
+
return public_url
|
| 94 |
+
except Exception as e:
|
| 95 |
+
print("Error:", e)
|
| 96 |
+
return None
|