-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement dependency injection for file processing and refactor…
… upload endpoint (#35) Introduce dependency injection for the file processing service, refactor the upload endpoint to utilize this service, and clean up obsolete code and import paths.
- Loading branch information
Showing
7 changed files
with
70 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,35 @@ | ||
import io | ||
import os | ||
import time | ||
import shutil | ||
from fastapi import APIRouter, HTTPException, Query, Request, status | ||
from fastapi import APIRouter, HTTPException, Query, Request, status, Depends | ||
from injector import Injector, inject | ||
from src.service.file_service import MAX_FILE_SIZE, FileService | ||
|
||
router = APIRouter() | ||
|
||
MAX_FILE_SIZE = 1024 * 1024 | ||
DIR = "saved_files" | ||
ALLOWED_EXTENSIONS = [".txt", ".json", ".csv"] | ||
|
||
async def process_up(content: str, extension: str): | ||
print("Procesando evento 'up'") | ||
|
||
timestamp = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) | ||
def get_file_service(request: Request) -> FileService: | ||
injector: Injector = request.app.state.injector | ||
return injector.get(FileService) | ||
|
||
filename = f"{timestamp}{extension}" | ||
os.makedirs(DIR, exist_ok=True) | ||
file_location = os.path.join(DIR, filename) | ||
|
||
|
||
with open(file_location, "wb+") as f: | ||
shutil.copyfileobj(content, f) | ||
|
||
print(f"file saved: {file_location}") | ||
|
||
async def process_join(content: str): | ||
pass | ||
|
||
@router.post("/uploadFile") | ||
async def create_upload_file( | ||
request: Request, | ||
event: str = Query(..., description="Tipo de evento. Ejemplo: 'up' o 'join'") | ||
event: str = Query(..., | ||
description="Tipo de evento. Ejemplo: 'up' o 'join'"), | ||
file_service: FileService = Depends(get_file_service) | ||
): | ||
body = await request.body() | ||
|
||
if (len(body) > MAX_FILE_SIZE): | ||
raise HTTPException( | ||
detail="File too large", | ||
status_code=403 | ||
) | ||
|
||
content = io.BytesIO(body) | ||
|
||
timestamp = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) | ||
extension = ".json" | ||
filename = f"{timestamp}{extension}" | ||
os.makedirs(DIR, exist_ok=True) | ||
file_location = os.path.join(DIR, filename) | ||
|
||
with open(file_location, "wb") as f: | ||
f.write(content.getvalue()) | ||
if len(body) > MAX_FILE_SIZE: | ||
raise HTTPException(detail="File too large", status_code=403) | ||
|
||
content.seek(0) | ||
extension = ".json" | ||
|
||
if event == "up": | ||
await process_up(content, extension) | ||
file_service.process_up(body, extension) | ||
elif event == "join": | ||
await process_join(content) | ||
file_service.process_join(body) | ||
else: | ||
raise HTTPException(status_code=400, detail=f"Evento {event} no implementado") | ||
raise HTTPException( | ||
status_code=400, detail=f"Evento {event} no implementado") | ||
|
||
return status.HTTP_200_OK |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# src/core/di.py | ||
from injector import Module, provider, singleton | ||
from sqlmodel import Session | ||
from src.service.file_service import FileService | ||
from src.core.db.connection import engine | ||
|
||
|
||
class AppModule(Module): | ||
@singleton | ||
@provider | ||
def provide_session(self) -> Session: | ||
return Session(engine) | ||
|
||
@singleton | ||
@provider | ||
def provide_file_service(self, session: Session) -> FileService: | ||
return FileService(session) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import os | ||
import time | ||
from sqlmodel import Session | ||
|
||
DIR = "saved_files" | ||
MAX_FILE_SIZE = 1024 * 1024 | ||
ALLOWED_EXTENSIONS = [".txt", ".json", ".csv"] | ||
|
||
|
||
class FileService: | ||
def __init__(self, session: Session): | ||
self.session = session | ||
|
||
def save_file(self, content: bytes, extension: str) -> str: | ||
timestamp = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) | ||
filename = f"{timestamp}{extension}" | ||
os.makedirs(DIR, exist_ok=True) | ||
file_location = os.path.join(DIR, filename) | ||
|
||
with open(file_location, "wb") as f: | ||
f.write(content) | ||
|
||
return file_location | ||
|
||
def process_up(self, content: bytes, extension: str): | ||
file_location = self.save_file(content, extension) | ||
|
||
def process_join(self, content: bytes): | ||
pass |
This file was deleted.
Oops, something went wrong.