ABAO77 commited on
Commit
8604b55
·
verified ·
1 Parent(s): 6ba50c9

Delete firebase

Browse files
firebase/.DS_Store DELETED
Binary file (6.15 kB)
 
firebase/__pycache__/firebase_config.cpython-311.pyc DELETED
Binary file (1.8 kB)
 
firebase/__pycache__/firebase_provider.cpython-311.pyc DELETED
Binary file (4.36 kB)
 
firebase/firebase_config.py DELETED
@@ -1,36 +0,0 @@
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")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
firebase/firebase_provider.py DELETED
@@ -1,123 +0,0 @@
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
- import aiofiles
8
- import asyncio
9
- from typing import List
10
- from datetime import datetime
11
- import pytz
12
-
13
-
14
- def upload_file_to_storage(file_path, file_name):
15
- """
16
- Upload a file to Firebase Storage
17
- param:
18
- file_path: str - The path of the file on local machine to be uploaded
19
- return:
20
- str - The public URL of the uploaded file
21
- """
22
- blob = firebase_bucket.blob(file_name)
23
- blob.upload_from_filename(file_path)
24
- blob.make_public()
25
-
26
- return blob.public_url
27
-
28
-
29
- def delete_file_from_storage(file_name):
30
- """
31
- Delete a file from Firebase Storage
32
- param:
33
- file_name: str - The name of the file to be deleted
34
- return:
35
- bool - True if the file is deleted successfully, False if the file is not found
36
- """
37
- try:
38
- blob = firebase_bucket.blob(file_name)
39
- blob.delete()
40
- return True
41
- except Exception as e:
42
- print("Error:", e)
43
- return False
44
-
45
-
46
- def list_all_files_in_storage():
47
- """
48
- View all files in Firebase Storage
49
- return:
50
- dict - Dictionary with keys are names and values are url of all files in Firebase Storage
51
- """
52
- blobs = firebase_bucket.list_blobs()
53
- blob_dict = {blob.name: blob.public_url for blob in blobs}
54
- return blob_dict
55
-
56
-
57
- def download_file_from_storage(file_name, destination_path):
58
- """
59
- Download a file from Firebase Storage
60
- param:
61
- file_name: str - The name of the file to be downloaded
62
- destination_path: str - The path to save the downloaded file
63
- return:
64
- bool - True if the file is downloaded successfully, False if the file is not found
65
- """
66
- try:
67
- blob = firebase_bucket.blob(file_name)
68
- blob.download_to_filename(destination_path)
69
- print("da tai xun thanh cong")
70
- return True
71
- except Exception as e:
72
- print("Error:", e)
73
- return False
74
-
75
-
76
- async def upload_base64_image_to_storage(base64_image: str, file_name: str) -> str:
77
- """
78
- Upload a base64 image to Firebase Storage
79
- param:
80
- base64_image: str - The base64 encoded image
81
- file_name: str - The name of the file to be uploaded
82
- return:
83
- str - The public URL of the uploaded file
84
- """
85
- try:
86
- # Decode the base64 image
87
- image_data = base64.b64decode(base64_image)
88
-
89
- # Convert the decoded image to a JPG file
90
- image = Image.open(io.BytesIO(image_data))
91
- temp_file_path = f"{tempfile.gettempdir()}/{file_name}.jpg"
92
-
93
- async with aiofiles.open(temp_file_path, mode="wb") as f:
94
- image.save(f, format="JPEG")
95
-
96
- # Upload the JPG file to Firebase Storage
97
- public_url = await upload_file_to_storage(temp_file_path, f"{file_name}.jpg")
98
-
99
- # Remove the temporary file
100
- os.remove(temp_file_path)
101
-
102
- return public_url
103
- except Exception as e:
104
- print("Error:", e)
105
- return None
106
-
107
-
108
- async def process_images(base64_images: List[str]) -> List[str]:
109
- tasks = []
110
- for idx, base64_image in enumerate(base64_images):
111
- timestamp = (
112
- datetime.now(pytz.timezone("Asia/Ho_Chi_Minh"))
113
- .replace(tzinfo=None)
114
- .strftime("%Y-%m-%d %H:%M:%S")
115
- )
116
- encoded_timestamp = (
117
- base64.urlsafe_b64encode(timestamp.encode()).decode().rstrip("=")
118
- )
119
- file_name = f"image_{encoded_timestamp}_{idx}"
120
- tasks.append(upload_base64_image_to_storage(base64_image, file_name))
121
-
122
- final_output = await asyncio.gather(*tasks)
123
- return final_output