DocuRAG / Api /src /modules /uploadDocument /features /uploadDocument_feature.py
abadesalex's picture
infraestructure and feature upload
57cab59
raw
history blame
1.28 kB
import os
from typing import Dict
from fastapi import UploadFile
from src.db_local_storage.files_db import FILES_DIRECTORY, FILES_NAMES_DATABASE
class UploadDocumentFeature:
@staticmethod
def ensure_directory_exists(folder_path: str) -> None:
"""Ensure that the directory exists."""
if not os.path.exists(folder_path):
os.makedirs(folder_path)
@staticmethod
async def saveFile(document: UploadFile, directory: str) -> str:
"""Save the uploaded file to the specified directory."""
file_path = os.path.join(directory, document.filename)
with open(file_path, "wb") as file:
content = await document.read()
file.write(content)
return file_path
@staticmethod
async def uploadFile(document: UploadFile) -> Dict[str, str]:
"""
Upload a file to the server
:param document: the file to upload
:return: a message to confirm the upload
"""
UploadDocumentFeature.ensure_directory_exists(FILES_DIRECTORY)
id = len(FILES_NAMES_DATABASE) + 1
FILES_NAMES_DATABASE[id] = document.filename
file_path = await UploadDocumentFeature.saveFile(document, FILES_DIRECTORY)
return {"message": "Document Updated"}