2023-09-20 10:35:37 +03:00
|
|
|
import os
|
2023-09-08 12:03:14 +03:00
|
|
|
from typing import Optional
|
2023-06-28 20:39:27 +03:00
|
|
|
from uuid import UUID
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-09-14 12:56:59 +03:00
|
|
|
from celery_worker import process_file_and_notify
|
2023-09-20 10:35:37 +03:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, Request, UploadFile
|
|
|
|
from logger import get_logger
|
2023-11-14 16:31:02 +03:00
|
|
|
from middlewares.auth import AuthBearer, get_current_user
|
2023-11-24 16:54:16 +03:00
|
|
|
from models import UserUsage
|
2023-12-01 00:29:28 +03:00
|
|
|
from modules.brain.entity.brain_entity import RoleEnum
|
|
|
|
from modules.brain.service.brain_authorization_service import (
|
|
|
|
validate_brain_authorization,
|
|
|
|
)
|
2023-11-29 11:04:03 +03:00
|
|
|
from modules.knowledge.dto.inputs import CreateKnowledgeProperties
|
|
|
|
from modules.knowledge.service.knowledge_service import KnowledgeService
|
2023-11-28 16:27:39 +03:00
|
|
|
from modules.notification.dto.inputs import (
|
|
|
|
CreateNotificationProperties,
|
|
|
|
NotificationUpdatableProperties,
|
|
|
|
)
|
|
|
|
from modules.notification.entity.notification import NotificationsStatusEnum
|
|
|
|
from modules.notification.service.notification_service import NotificationService
|
2023-11-15 15:17:51 +03:00
|
|
|
from modules.user.entity.user_identity import UserIdentity
|
2023-11-14 11:52:44 +03:00
|
|
|
from packages.files.file import convert_bytes, get_file_size
|
2023-11-25 01:12:49 +03:00
|
|
|
from repository.files.upload_file import upload_file_storage
|
2023-07-19 14:41:46 +03:00
|
|
|
|
2023-09-20 10:35:37 +03:00
|
|
|
logger = get_logger(__name__)
|
2023-06-11 00:59:16 +03:00
|
|
|
upload_router = APIRouter()
|
|
|
|
|
2023-11-28 16:27:39 +03:00
|
|
|
notification_service = NotificationService()
|
2023-11-29 11:04:03 +03:00
|
|
|
knowledge_service = KnowledgeService()
|
2023-11-28 16:27:39 +03:00
|
|
|
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-08-21 00:20:57 +03:00
|
|
|
@upload_router.get("/upload/healthz", tags=["Health"])
|
|
|
|
async def healthz():
|
|
|
|
return {"status": "ok"}
|
|
|
|
|
|
|
|
|
2023-06-15 15:43:40 +03:00
|
|
|
@upload_router.post("/upload", dependencies=[Depends(AuthBearer())], tags=["Upload"])
|
2023-07-04 18:56:54 +03:00
|
|
|
async def upload_file(
|
|
|
|
request: Request,
|
|
|
|
uploadFile: UploadFile,
|
|
|
|
brain_id: UUID = Query(..., description="The ID of the brain"),
|
2023-09-08 12:03:14 +03:00
|
|
|
chat_id: Optional[UUID] = Query(None, description="The ID of the chat"),
|
2023-08-21 15:05:13 +03:00
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
2023-07-04 18:56:54 +03:00
|
|
|
):
|
2023-07-19 14:41:46 +03:00
|
|
|
validate_brain_authorization(
|
|
|
|
brain_id, current_user.id, [RoleEnum.Editor, RoleEnum.Owner]
|
|
|
|
)
|
2023-11-24 16:54:16 +03:00
|
|
|
|
|
|
|
user_daily_usage = UserUsage(
|
2023-09-13 14:47:12 +03:00
|
|
|
id=current_user.id,
|
|
|
|
email=current_user.email,
|
|
|
|
)
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-11-24 16:54:16 +03:00
|
|
|
user_settings = user_daily_usage.get_user_settings()
|
|
|
|
|
|
|
|
remaining_free_space = user_settings.get("max_brain_size", 1000000000)
|
2023-06-12 18:58:05 +03:00
|
|
|
|
2023-06-28 20:39:27 +03:00
|
|
|
file_size = get_file_size(uploadFile)
|
2023-06-11 00:59:16 +03:00
|
|
|
if remaining_free_space - file_size < 0:
|
2023-11-24 16:54:16 +03:00
|
|
|
message = f"Brain will exceed maximum capacity. Maximum file allowed is : {convert_bytes(remaining_free_space)}"
|
|
|
|
raise HTTPException(status_code=403, detail=message)
|
2023-09-14 12:56:59 +03:00
|
|
|
upload_notification = None
|
|
|
|
if chat_id:
|
2023-11-28 16:27:39 +03:00
|
|
|
upload_notification = notification_service.add_notification(
|
2023-09-14 12:56:59 +03:00
|
|
|
CreateNotificationProperties(
|
|
|
|
action="UPLOAD",
|
|
|
|
chat_id=chat_id,
|
|
|
|
status=NotificationsStatusEnum.Pending,
|
2023-09-07 14:22:06 +03:00
|
|
|
)
|
2023-07-04 18:56:54 +03:00
|
|
|
)
|
2023-09-14 12:56:59 +03:00
|
|
|
|
2023-11-25 01:12:49 +03:00
|
|
|
file_content = await uploadFile.read()
|
|
|
|
filename_with_brain_id = str(brain_id) + "/" + str(uploadFile.filename)
|
2023-09-14 12:56:59 +03:00
|
|
|
|
2023-09-20 10:35:37 +03:00
|
|
|
try:
|
2023-11-24 16:54:16 +03:00
|
|
|
file_in_storage = upload_file_storage(file_content, filename_with_brain_id)
|
|
|
|
logger.info(f"File {file_in_storage} uploaded successfully")
|
2023-09-20 10:35:37 +03:00
|
|
|
|
|
|
|
except Exception as e:
|
2023-11-27 19:36:46 +03:00
|
|
|
print(e)
|
|
|
|
notification_message = {
|
|
|
|
"status": "error",
|
|
|
|
"message": "There was an error uploading the file. Please check the file and try again. If the issue persist, please open an issue on Github",
|
|
|
|
"name": uploadFile.filename if uploadFile else "Last Upload File",
|
|
|
|
}
|
2023-11-28 16:27:39 +03:00
|
|
|
notification_service.update_notification_by_id(
|
2024-01-29 08:35:10 +03:00
|
|
|
upload_notification.id if upload_notification else None,
|
2023-11-27 19:36:46 +03:00
|
|
|
NotificationUpdatableProperties(
|
|
|
|
status=NotificationsStatusEnum.Done,
|
|
|
|
message=str(notification_message),
|
|
|
|
),
|
|
|
|
)
|
2023-09-20 10:35:37 +03:00
|
|
|
if "The resource already exists" in str(e):
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=403,
|
2023-11-25 01:12:49 +03:00
|
|
|
detail=f"File {uploadFile.filename} already exists in storage.",
|
2023-09-20 10:35:37 +03:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise HTTPException(
|
2023-11-25 01:12:49 +03:00
|
|
|
status_code=500, detail=f"Failed to upload file to storage. {e}"
|
2023-09-20 10:35:37 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
knowledge_to_add = CreateKnowledgeProperties(
|
|
|
|
brain_id=brain_id,
|
2023-11-25 01:12:49 +03:00
|
|
|
file_name=uploadFile.filename,
|
2023-09-20 10:35:37 +03:00
|
|
|
extension=os.path.splitext(
|
2023-11-25 01:12:49 +03:00
|
|
|
uploadFile.filename # pyright: ignore reportPrivateUsage=none
|
2023-09-20 10:35:37 +03:00
|
|
|
)[-1].lower(),
|
|
|
|
)
|
|
|
|
|
2023-11-29 11:04:03 +03:00
|
|
|
added_knowledge = knowledge_service.add_knowledge(knowledge_to_add)
|
2023-09-20 10:35:37 +03:00
|
|
|
logger.info(f"Knowledge {added_knowledge} added successfully")
|
|
|
|
|
2023-09-14 12:56:59 +03:00
|
|
|
process_file_and_notify.delay(
|
|
|
|
file_name=filename_with_brain_id,
|
2023-11-25 01:12:49 +03:00
|
|
|
file_original_name=uploadFile.filename,
|
2023-09-14 12:56:59 +03:00
|
|
|
brain_id=brain_id,
|
|
|
|
notification_id=upload_notification.id if upload_notification else None,
|
|
|
|
)
|
|
|
|
return {"message": "File processing has started."}
|