Spaces:
Sleeping
Sleeping
Upload 155 files
Browse files- src/.DS_Store +0 -0
- src/apis/.DS_Store +0 -0
- src/apis/config/__pycache__/firebase_config.cpython-311.pyc +0 -0
- src/apis/config/firebase_config.py +2 -2
- src/apis/controllers/.DS_Store +0 -0
- src/apis/controllers/__pycache__/post_controller.cpython-311.pyc +0 -0
- src/apis/controllers/post_controller.py +19 -2
- src/apis/models/__pycache__/post_models.cpython-311.pyc +0 -0
- src/apis/models/post_models.py +3 -0
- src/apis/providers/.DS_Store +0 -0
- src/apis/providers/__pycache__/firebase_provider.cpython-311.pyc +0 -0
- src/apis/providers/firebase_provider.py +34 -1
- src/apis/routes/.DS_Store +0 -0
- src/apis/routes/__pycache__/auth_route.cpython-311.pyc +0 -0
- src/apis/routes/__pycache__/post_router.cpython-311.pyc +0 -0
- src/apis/routes/auth_route.py +9 -0
- src/apis/routes/post_router.py +3 -0
- src/utils/__pycache__/helper.cpython-311.pyc +0 -0
- src/utils/helper.py +31 -2
src/.DS_Store
CHANGED
|
Binary files a/src/.DS_Store and b/src/.DS_Store differ
|
|
|
src/apis/.DS_Store
CHANGED
|
Binary files a/src/apis/.DS_Store and b/src/apis/.DS_Store differ
|
|
|
src/apis/config/__pycache__/firebase_config.cpython-311.pyc
ADDED
|
Binary file (1.79 kB). View file
|
|
|
src/apis/config/firebase_config.py
CHANGED
|
@@ -7,13 +7,13 @@ from dotenv import load_dotenv
|
|
| 7 |
|
| 8 |
# Load environment variables
|
| 9 |
load_dotenv()
|
| 10 |
-
firebase_url_storageBucket = os.getenv("
|
| 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("
|
| 17 |
"private_key": os.getenv("PRIVATE_KEY"),
|
| 18 |
"client_email": os.getenv("CLIENT_EMAIL"),
|
| 19 |
"client_id": os.getenv("CLIENT_ID"),
|
|
|
|
| 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"),
|
src/apis/controllers/.DS_Store
CHANGED
|
Binary files a/src/apis/controllers/.DS_Store and b/src/apis/controllers/.DS_Store differ
|
|
|
src/apis/controllers/__pycache__/post_controller.cpython-311.pyc
CHANGED
|
Binary files a/src/apis/controllers/__pycache__/post_controller.cpython-311.pyc and b/src/apis/controllers/__pycache__/post_controller.cpython-311.pyc differ
|
|
|
src/apis/controllers/post_controller.py
CHANGED
|
@@ -8,6 +8,9 @@ from datetime import datetime
|
|
| 8 |
from bson import ObjectId
|
| 9 |
from src.utils.mongo import UserCRUD
|
| 10 |
from asyncio import gather
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
def serialize_datetime(obj):
|
|
@@ -19,14 +22,28 @@ def serialize_datetime(obj):
|
|
| 19 |
|
| 20 |
|
| 21 |
async def create_a_post_controller(
|
| 22 |
-
content: str, user_id: str, destination_id: str
|
| 23 |
) -> Dict:
|
| 24 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
post = {
|
| 26 |
"content": content,
|
| 27 |
"user_id": user_id,
|
| 28 |
"destination_id": destination_id,
|
| 29 |
"comment_ids": [],
|
|
|
|
| 30 |
"like": [],
|
| 31 |
}
|
| 32 |
await PostCRUD.create(post)
|
|
@@ -79,7 +96,7 @@ async def list_all_posts_controller():
|
|
| 79 |
"destination_id": post.get("destination_id"),
|
| 80 |
"comment_ids": post.get("comment_ids", []),
|
| 81 |
"like": post.get("like", []),
|
| 82 |
-
"picture": [],
|
| 83 |
"created_at": serialize_datetime(post.get("created_at")),
|
| 84 |
"updated_at": serialize_datetime(post.get("updated_at")),
|
| 85 |
"user_info": (
|
|
|
|
| 8 |
from bson import ObjectId
|
| 9 |
from src.utils.mongo import UserCRUD
|
| 10 |
from asyncio import gather
|
| 11 |
+
from src.utils.helper import call_external_api
|
| 12 |
+
from src.utils.logger import get_date_time
|
| 13 |
+
import base64
|
| 14 |
|
| 15 |
|
| 16 |
def serialize_datetime(obj):
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
async def create_a_post_controller(
|
| 25 |
+
content: str, user_id: str, destination_id: str, images: list
|
| 26 |
) -> Dict:
|
| 27 |
try:
|
| 28 |
+
timestamp = get_date_time().replace(tzinfo=None).strftime("%Y-%m-%d %H:%M:%S")
|
| 29 |
+
encoded_timestamp = (
|
| 30 |
+
base64.urlsafe_b64encode(timestamp.encode()).decode().rstrip("=")
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
image_url = await call_external_api(
|
| 34 |
+
method="POST",
|
| 35 |
+
# url="http://localhost:8000/upload_image",
|
| 36 |
+
url="https://abao77-image-retrieval.hf.space/upload_image",
|
| 37 |
+
json={"base64_image": images[0], "image_name": f"_{encoded_timestamp}"},
|
| 38 |
+
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
post = {
|
| 42 |
"content": content,
|
| 43 |
"user_id": user_id,
|
| 44 |
"destination_id": destination_id,
|
| 45 |
"comment_ids": [],
|
| 46 |
+
"picture": [image_url["public_url"]],
|
| 47 |
"like": [],
|
| 48 |
}
|
| 49 |
await PostCRUD.create(post)
|
|
|
|
| 96 |
"destination_id": post.get("destination_id"),
|
| 97 |
"comment_ids": post.get("comment_ids", []),
|
| 98 |
"like": post.get("like", []),
|
| 99 |
+
"picture": post.get("picture", []),
|
| 100 |
"created_at": serialize_datetime(post.get("created_at")),
|
| 101 |
"updated_at": serialize_datetime(post.get("updated_at")),
|
| 102 |
"user_info": (
|
src/apis/models/__pycache__/post_models.cpython-311.pyc
CHANGED
|
Binary files a/src/apis/models/__pycache__/post_models.cpython-311.pyc and b/src/apis/models/__pycache__/post_models.cpython-311.pyc differ
|
|
|
src/apis/models/post_models.py
CHANGED
|
@@ -40,6 +40,7 @@ class Post(BaseDocument):
|
|
| 40 |
destination_id: str = Field("", description="Destination's id")
|
| 41 |
comment_ids: list[str] = Field([], description="Comment's id")
|
| 42 |
like: list[str] = Field([], description="User's id who like this post")
|
|
|
|
| 43 |
model_config = {
|
| 44 |
"json_schema_extra": {
|
| 45 |
"example": {
|
|
@@ -47,6 +48,8 @@ class Post(BaseDocument):
|
|
| 47 |
"user_id": "1234567890",
|
| 48 |
"destination_id": "1234567890",
|
| 49 |
"comment_ids": ["1234567890"],
|
|
|
|
|
|
|
| 50 |
}
|
| 51 |
}
|
| 52 |
}
|
|
|
|
| 40 |
destination_id: str = Field("", description="Destination's id")
|
| 41 |
comment_ids: list[str] = Field([], description="Comment's id")
|
| 42 |
like: list[str] = Field([], description="User's id who like this post")
|
| 43 |
+
picture: list[str] = Field([], description="Picture's url")
|
| 44 |
model_config = {
|
| 45 |
"json_schema_extra": {
|
| 46 |
"example": {
|
|
|
|
| 48 |
"user_id": "1234567890",
|
| 49 |
"destination_id": "1234567890",
|
| 50 |
"comment_ids": ["1234567890"],
|
| 51 |
+
"like": ["1234567890"],
|
| 52 |
+
"picture": ["https://example.com/picture.jpg"],
|
| 53 |
}
|
| 54 |
}
|
| 55 |
}
|
src/apis/providers/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
src/apis/providers/__pycache__/firebase_provider.cpython-311.pyc
ADDED
|
Binary file (4.75 kB). View file
|
|
|
src/apis/providers/firebase_provider.py
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
from src.apis.config.firebase_config import firebase_bucket
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
def upload_file_to_storage(file_path, file_name):
|
|
@@ -9,7 +12,6 @@ def upload_file_to_storage(file_path, file_name):
|
|
| 9 |
return:
|
| 10 |
str - The public URL of the uploaded file
|
| 11 |
"""
|
| 12 |
-
file_name = file_name
|
| 13 |
blob = firebase_bucket.blob(file_name)
|
| 14 |
blob.upload_from_filename(file_path)
|
| 15 |
blob.make_public()
|
|
@@ -17,6 +19,7 @@ def upload_file_to_storage(file_path, file_name):
|
|
| 17 |
return blob.public_url
|
| 18 |
|
| 19 |
|
|
|
|
| 20 |
def delete_file_from_storage(file_name):
|
| 21 |
"""
|
| 22 |
Delete a file from Firebase Storage
|
|
@@ -62,3 +65,33 @@ def download_file_from_storage(file_name, destination_path):
|
|
| 62 |
except Exception as e:
|
| 63 |
print("Error:", e)
|
| 64 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from src.apis.config.firebase_config import firebase_bucket
|
| 2 |
+
import base64
|
| 3 |
+
import os
|
| 4 |
+
import tempfile
|
| 5 |
|
| 6 |
|
| 7 |
def upload_file_to_storage(file_path, file_name):
|
|
|
|
| 12 |
return:
|
| 13 |
str - The public URL of the uploaded file
|
| 14 |
"""
|
|
|
|
| 15 |
blob = firebase_bucket.blob(file_name)
|
| 16 |
blob.upload_from_filename(file_path)
|
| 17 |
blob.make_public()
|
|
|
|
| 19 |
return blob.public_url
|
| 20 |
|
| 21 |
|
| 22 |
+
|
| 23 |
def delete_file_from_storage(file_name):
|
| 24 |
"""
|
| 25 |
Delete a file from Firebase Storage
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
print("Error:", e)
|
| 67 |
return False
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def upload_base64_image_to_storage(base64_image, file_name):
|
| 71 |
+
"""
|
| 72 |
+
Upload a base64 image to Firebase Storage
|
| 73 |
+
param:
|
| 74 |
+
base64_image: str - The base64 encoded image
|
| 75 |
+
file_name: str - The name of the file to be uploaded
|
| 76 |
+
return:
|
| 77 |
+
str - The public URL of the uploaded file
|
| 78 |
+
"""
|
| 79 |
+
try:
|
| 80 |
+
# Decode the base64 image
|
| 81 |
+
image_data = base64.b64decode(base64_image)
|
| 82 |
+
|
| 83 |
+
# Create a temporary file to save the decoded image
|
| 84 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
| 85 |
+
temp_file.write(image_data)
|
| 86 |
+
temp_file_path = temp_file.name
|
| 87 |
+
|
| 88 |
+
# Upload the temporary file to Firebase Storage
|
| 89 |
+
public_url = upload_file_to_storage(temp_file_path, file_name)
|
| 90 |
+
|
| 91 |
+
# Remove the temporary file
|
| 92 |
+
os.remove(temp_file_path)
|
| 93 |
+
|
| 94 |
+
return public_url
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print("Error:", e)
|
| 97 |
+
return None
|
src/apis/routes/.DS_Store
CHANGED
|
Binary files a/src/apis/routes/.DS_Store and b/src/apis/routes/.DS_Store differ
|
|
|
src/apis/routes/__pycache__/auth_route.cpython-311.pyc
CHANGED
|
Binary files a/src/apis/routes/__pycache__/auth_route.cpython-311.pyc and b/src/apis/routes/__pycache__/auth_route.cpython-311.pyc differ
|
|
|
src/apis/routes/__pycache__/post_router.cpython-311.pyc
CHANGED
|
Binary files a/src/apis/routes/__pycache__/post_router.cpython-311.pyc and b/src/apis/routes/__pycache__/post_router.cpython-311.pyc differ
|
|
|
src/apis/routes/auth_route.py
CHANGED
|
@@ -35,3 +35,12 @@ async def get_user_info(user: user_dependency):
|
|
| 35 |
if user is None:
|
| 36 |
return JSONResponse(content={"message": "User not found"}, status_code=401)
|
| 37 |
return JSONResponse(content={"user": user}, status_code=200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if user is None:
|
| 36 |
return JSONResponse(content={"message": "User not found"}, status_code=401)
|
| 37 |
return JSONResponse(content={"user": user}, status_code=200)
|
| 38 |
+
|
| 39 |
+
import time
|
| 40 |
+
import asyncio
|
| 41 |
+
@router.get("/test", status_code=status.HTTP_200_OK)
|
| 42 |
+
async def test():
|
| 43 |
+
print("da vao")
|
| 44 |
+
await asyncio.sleep(5) # Use asyncio.sleep for non-blocking sleep
|
| 45 |
+
print("alo")
|
| 46 |
+
return "test"
|
src/apis/routes/post_router.py
CHANGED
|
@@ -23,12 +23,14 @@ user_dependency = Annotated[User, Depends(get_current_user)]
|
|
| 23 |
class BodyPost(BaseDocument):
|
| 24 |
content: str = Field("", description="Post's content")
|
| 25 |
destination_id: str = Field("", description="Destination's id")
|
|
|
|
| 26 |
|
| 27 |
class Config:
|
| 28 |
json_schema_extra = {
|
| 29 |
"example": {
|
| 30 |
"content": "John Doe",
|
| 31 |
"destination_id": "1234567890",
|
|
|
|
| 32 |
}
|
| 33 |
}
|
| 34 |
|
|
@@ -42,6 +44,7 @@ async def create_post(body: BodyPost, user: user_dependency):
|
|
| 42 |
body.content,
|
| 43 |
user_id,
|
| 44 |
body.destination_id,
|
|
|
|
| 45 |
)
|
| 46 |
logger.info(f"RESULT: {result}")
|
| 47 |
if result["status"] == "error":
|
|
|
|
| 23 |
class BodyPost(BaseDocument):
|
| 24 |
content: str = Field("", description="Post's content")
|
| 25 |
destination_id: str = Field("", description="Destination's id")
|
| 26 |
+
images_base64: Optional[list] = Field([], description="Images base64")
|
| 27 |
|
| 28 |
class Config:
|
| 29 |
json_schema_extra = {
|
| 30 |
"example": {
|
| 31 |
"content": "John Doe",
|
| 32 |
"destination_id": "1234567890",
|
| 33 |
+
"images_base64": ["base64_image1", "base64_image2"],
|
| 34 |
}
|
| 35 |
}
|
| 36 |
|
|
|
|
| 44 |
body.content,
|
| 45 |
user_id,
|
| 46 |
body.destination_id,
|
| 47 |
+
body.images_base64,
|
| 48 |
)
|
| 49 |
logger.info(f"RESULT: {result}")
|
| 50 |
if result["status"] == "error":
|
src/utils/__pycache__/helper.cpython-311.pyc
CHANGED
|
Binary files a/src/utils/__pycache__/helper.cpython-311.pyc and b/src/utils/__pycache__/helper.cpython-311.pyc differ
|
|
|
src/utils/helper.py
CHANGED
|
@@ -6,8 +6,9 @@ from datetime import datetime, timezone
|
|
| 6 |
import re
|
| 7 |
from src.utils.logger import logger
|
| 8 |
from pydantic import BaseModel, Field
|
| 9 |
-
from typing import List,
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
def handle_validator_raise(func):
|
| 13 |
"""
|
|
@@ -252,3 +253,31 @@ def format_include_destinations(include_destinations: List[Union[Destination, st
|
|
| 252 |
else:
|
| 253 |
for index, destination in enumerate(include_destinations):
|
| 254 |
formatted_string += f"#Destination {int(index) + 1}: {destination}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import re
|
| 7 |
from src.utils.logger import logger
|
| 8 |
from pydantic import BaseModel, Field
|
| 9 |
+
from typing import List, Union
|
| 10 |
+
import httpx
|
| 11 |
+
from fastapi import HTTPException
|
| 12 |
|
| 13 |
def handle_validator_raise(func):
|
| 14 |
"""
|
|
|
|
| 253 |
else:
|
| 254 |
for index, destination in enumerate(include_destinations):
|
| 255 |
formatted_string += f"#Destination {int(index) + 1}: {destination}\n"
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
async def call_external_api(
|
| 260 |
+
method: str,
|
| 261 |
+
url: str,
|
| 262 |
+
headers: dict = None,
|
| 263 |
+
params: dict = None,
|
| 264 |
+
data: dict = None,
|
| 265 |
+
json: dict = None,
|
| 266 |
+
timeout: int = 10
|
| 267 |
+
):
|
| 268 |
+
async with httpx.AsyncClient(timeout=timeout) as client:
|
| 269 |
+
try:
|
| 270 |
+
response = await client.request(
|
| 271 |
+
method=method,
|
| 272 |
+
url=url,
|
| 273 |
+
headers=headers,
|
| 274 |
+
params=params,
|
| 275 |
+
data=data,
|
| 276 |
+
json=json
|
| 277 |
+
)
|
| 278 |
+
response.raise_for_status() # Raise an error for non-2xx/3xx responses
|
| 279 |
+
return response.json()
|
| 280 |
+
except httpx.HTTPStatusError as e:
|
| 281 |
+
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
| 282 |
+
except httpx.RequestError as e:
|
| 283 |
+
raise HTTPException(status_code=500, detail=f"Request failed: {str(e)}")
|