abadesalex commited on
Commit
57cab59
1 Parent(s): 457389c

infraestructure and feature upload

Browse files
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ #venv
2
+ Api/venv
Api/requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ PyPDF2
4
+ python-multipart
Api/src/__pycache__/main.cpython-310.pyc ADDED
Binary file (851 Bytes). View file
 
Api/src/db_local_storage/__pycache__/files_db.cpython-310.pyc ADDED
Binary file (228 Bytes). View file
 
Api/src/db_local_storage/__pycache__/files_names_db.cpython-310.pyc ADDED
Binary file (183 Bytes). View file
 
Api/src/db_local_storage/files_db.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ FILES_NAMES_DATABASE = {}
2
+ FILES_DIRECTORY = "src/db_local_storage/files"
Api/src/main.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import uvicorn
2
+ from fastapi import APIRouter, FastAPI
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ from src.modules.uploadDocument.routes.uploadDocument_route import \
5
+ router as upload_file_routes
6
+
7
+ app = FastAPI()
8
+
9
+ origins = [
10
+ "http://localhost:8000",
11
+ "http://localhost:3000",
12
+ ]
13
+
14
+ app.add_middleware(
15
+ CORSMiddleware,
16
+ allow_origins=origins,
17
+ allow_credentials=True,
18
+ allow_methods=["*"],
19
+ allow_headers=["*"],
20
+ )
21
+
22
+ app_router = APIRouter(prefix="/api")
23
+ app_router.include_router(upload_file_routes, prefix="/upload", tags=["upload"])
24
+
25
+ @app_router.get("/")
26
+ async def root():
27
+ return {"message": "Hello World"}
28
+
29
+ app.include_router(app_router)
Api/src/modules/uploadDocument/controllers/__pycache__/FileUploadController.cpython-310.pyc ADDED
Binary file (788 Bytes). View file
 
Api/src/modules/uploadDocument/controllers/__pycache__/file_upload_controller.cpython-310.pyc ADDED
Binary file (1.08 kB). View file
 
Api/src/modules/uploadDocument/controllers/file_upload_controller.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import shutil
2
+ from typing import Any
3
+
4
+ from fastapi import HTTPException, UploadFile
5
+ from fastapi.responses import JSONResponse
6
+ from src.modules.uploadDocument.features.uploadDocument_feature import \
7
+ UploadDocumentFeature
8
+
9
+
10
+ class FileUploadController:
11
+
12
+ async def handle_file_upload(self, file: UploadFile) -> Any:
13
+ try:
14
+ print(file)
15
+ result = await UploadDocumentFeature.uploadFile(file)
16
+ message = result.get("message", "No message provided")
17
+
18
+ return JSONResponse(
19
+ status_code=200, content={"message": message}
20
+ )
21
+
22
+ except Exception as e:
23
+ raise HTTPException(status_code=500, detail=str(e))
Api/src/modules/uploadDocument/features/__pycache__/uploadDocument_feature.cpython-310.pyc ADDED
Binary file (1.7 kB). View file
 
Api/src/modules/uploadDocument/features/uploadDocument_feature.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import Dict
3
+
4
+ from fastapi import UploadFile
5
+ from src.db_local_storage.files_db import FILES_DIRECTORY, FILES_NAMES_DATABASE
6
+
7
+
8
+ class UploadDocumentFeature:
9
+
10
+ @staticmethod
11
+ def ensure_directory_exists(folder_path: str) -> None:
12
+ """Ensure that the directory exists."""
13
+ if not os.path.exists(folder_path):
14
+ os.makedirs(folder_path)
15
+
16
+ @staticmethod
17
+ async def saveFile(document: UploadFile, directory: str) -> str:
18
+ """Save the uploaded file to the specified directory."""
19
+ file_path = os.path.join(directory, document.filename)
20
+ with open(file_path, "wb") as file:
21
+ content = await document.read()
22
+ file.write(content)
23
+ return file_path
24
+
25
+ @staticmethod
26
+ async def uploadFile(document: UploadFile) -> Dict[str, str]:
27
+ """
28
+ Upload a file to the server
29
+ :param document: the file to upload
30
+ :return: a message to confirm the upload
31
+ """
32
+
33
+ UploadDocumentFeature.ensure_directory_exists(FILES_DIRECTORY)
34
+ id = len(FILES_NAMES_DATABASE) + 1
35
+ FILES_NAMES_DATABASE[id] = document.filename
36
+
37
+ file_path = await UploadDocumentFeature.saveFile(document, FILES_DIRECTORY)
38
+
39
+ return {"message": "Document Updated"}
Api/src/modules/uploadDocument/routes/__pycache__/uploadDocument_route.cpython-310.pyc ADDED
Binary file (622 Bytes). View file
 
Api/src/modules/uploadDocument/routes/uploadDocument_route.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, File, HTTPException, UploadFile
2
+ from src.modules.uploadDocument.controllers.file_upload_controller import \
3
+ FileUploadController
4
+
5
+ router = APIRouter()
6
+ controller = FileUploadController()
7
+
8
+
9
+ @router.post(
10
+ "/document",
11
+ )
12
+ async def upload_file(file: UploadFile = File(...)):
13
+ return await controller.handle_file_upload(file)
Api/src/modules/uploadDocument/schemas/uploadDocument_schema.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+
3
+
4
+ class DocumentUpload(BaseModel):
5
+ filename: str
6
+
7
+
8
+ class DocumentResponse(BaseModel):
9
+ id: int
10
+ filename: str