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-07-04 18:56:54 +03:00
|
|
|
from auth import AuthBearer, get_current_user
|
2023-06-28 20:39:27 +03:00
|
|
|
from fastapi import APIRouter, Depends, Query, Request, UploadFile
|
2023-09-13 14:47:12 +03:00
|
|
|
from models import Brain, File, UserIdentity, UserUsage
|
2023-09-07 14:22:06 +03:00
|
|
|
from models.databases.supabase.notifications import (
|
|
|
|
CreateNotificationProperties,
|
|
|
|
NotificationUpdatableProperties,
|
|
|
|
)
|
|
|
|
from models.notifications import NotificationsStatusEnum
|
2023-08-21 13:25:16 +03:00
|
|
|
from repository.brain import get_brain_details
|
2023-09-07 14:22:06 +03:00
|
|
|
from repository.notification.add_notification import add_notification
|
2023-09-13 14:47:12 +03:00
|
|
|
from repository.notification.update_notification import update_notification_by_id
|
2023-08-21 13:25:16 +03:00
|
|
|
from repository.user_identity import get_user_identity
|
2023-07-19 14:41:46 +03:00
|
|
|
from routes.authorizations.brain_authorization import (
|
|
|
|
RoleEnum,
|
|
|
|
validate_brain_authorization,
|
|
|
|
)
|
2023-09-13 14:47:12 +03:00
|
|
|
from utils.file import convert_bytes, get_file_size
|
|
|
|
from utils.processors import filter_file
|
2023-07-19 14:41:46 +03:00
|
|
|
|
2023-06-11 00:59:16 +03:00
|
|
|
upload_router = APIRouter()
|
|
|
|
|
|
|
|
|
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-07-04 18:56:54 +03:00
|
|
|
enable_summarization: bool = False,
|
2023-08-21 15:05:13 +03:00
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
2023-07-04 18:56:54 +03:00
|
|
|
):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Upload a file to the user's storage.
|
|
|
|
|
|
|
|
- `file`: The file to be uploaded.
|
|
|
|
- `enable_summarization`: Flag to enable summarization of the file's content.
|
|
|
|
- `current_user`: The current authenticated user.
|
|
|
|
- Returns the response message indicating the success or failure of the upload.
|
|
|
|
|
|
|
|
This endpoint allows users to upload files to their storage (brain). It checks the remaining free space in the user's storage (brain)
|
|
|
|
and ensures that the file size does not exceed the maximum capacity. If the file is within the allowed size limit,
|
|
|
|
it can optionally apply summarization to the file's content. The response message will indicate the status of the upload.
|
|
|
|
"""
|
2023-07-19 14:41:46 +03:00
|
|
|
validate_brain_authorization(
|
|
|
|
brain_id, current_user.id, [RoleEnum.Editor, RoleEnum.Owner]
|
|
|
|
)
|
2023-06-28 20:39:27 +03:00
|
|
|
|
2023-07-04 18:56:54 +03:00
|
|
|
brain = Brain(id=brain_id)
|
2023-09-13 14:47:12 +03:00
|
|
|
userDailyUsage = UserUsage(
|
|
|
|
id=current_user.id,
|
|
|
|
email=current_user.email,
|
|
|
|
openai_api_key=current_user.openai_api_key,
|
|
|
|
)
|
|
|
|
userSettings = userDailyUsage.get_user_settings()
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-07-04 18:56:54 +03:00
|
|
|
if request.headers.get("Openai-Api-Key"):
|
2023-09-13 14:47:12 +03:00
|
|
|
brain.max_brain_size = userSettings.get("max_brain_size", 1000000000)
|
2023-07-24 15:17:33 +03:00
|
|
|
|
2023-09-13 14:47:12 +03:00
|
|
|
remaining_free_space = userSettings.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)
|
|
|
|
|
|
|
|
file = File(file=uploadFile)
|
2023-06-11 00:59:16 +03:00
|
|
|
if remaining_free_space - file_size < 0:
|
2023-07-04 18:56:54 +03:00
|
|
|
message = {
|
2023-08-21 15:05:13 +03:00
|
|
|
"message": f"❌ UserIdentity's brain will exceed maximum capacity with this upload. Maximum file allowed is : {convert_bytes(remaining_free_space)}",
|
2023-07-04 18:56:54 +03:00
|
|
|
"type": "error",
|
|
|
|
}
|
|
|
|
else:
|
2023-09-07 18:23:31 +03:00
|
|
|
upload_notification = None
|
|
|
|
if chat_id:
|
|
|
|
upload_notification = add_notification(
|
|
|
|
CreateNotificationProperties(
|
|
|
|
action="UPLOAD",
|
|
|
|
chat_id=chat_id,
|
|
|
|
status=NotificationsStatusEnum.Pending,
|
|
|
|
)
|
2023-09-07 14:22:06 +03:00
|
|
|
)
|
2023-08-01 10:24:57 +03:00
|
|
|
openai_api_key = request.headers.get("Openai-Api-Key", None)
|
|
|
|
if openai_api_key is None:
|
2023-08-03 11:37:13 +03:00
|
|
|
brain_details = get_brain_details(brain_id)
|
2023-08-01 17:25:02 +03:00
|
|
|
if brain_details:
|
2023-08-03 11:37:13 +03:00
|
|
|
openai_api_key = brain_details.openai_api_key
|
2023-08-01 10:24:57 +03:00
|
|
|
|
|
|
|
if openai_api_key is None:
|
|
|
|
openai_api_key = get_user_identity(current_user.id).openai_api_key
|
|
|
|
|
2023-07-04 18:56:54 +03:00
|
|
|
message = await filter_file(
|
2023-08-03 21:24:42 +03:00
|
|
|
file=file,
|
|
|
|
enable_summarization=enable_summarization,
|
2023-07-04 18:56:54 +03:00
|
|
|
brain_id=brain_id,
|
2023-08-01 10:24:57 +03:00
|
|
|
openai_api_key=openai_api_key,
|
2023-07-04 18:56:54 +03:00
|
|
|
)
|
2023-09-12 18:44:15 +03:00
|
|
|
if not file.file:
|
|
|
|
raise Exception("File not found")
|
2023-09-07 18:23:31 +03:00
|
|
|
|
|
|
|
if upload_notification:
|
2023-09-12 18:44:15 +03:00
|
|
|
notification_message = {
|
|
|
|
"status": message["type"],
|
|
|
|
"message": message["message"],
|
|
|
|
"name": file.file.filename if file.file else "",
|
|
|
|
}
|
2023-09-07 18:23:31 +03:00
|
|
|
update_notification_by_id(
|
|
|
|
upload_notification.id,
|
|
|
|
NotificationUpdatableProperties(
|
2023-09-12 18:44:15 +03:00
|
|
|
status=NotificationsStatusEnum.Done,
|
|
|
|
message=str(notification_message),
|
2023-09-07 18:23:31 +03:00
|
|
|
),
|
|
|
|
)
|
2023-06-28 20:39:27 +03:00
|
|
|
|
2023-06-11 00:59:16 +03:00
|
|
|
return message
|