fix bug: cannot upload & update file

main
akumadoferudi 6 months ago
parent 924e364e04
commit 495cfa3949

@ -16,4 +16,4 @@ COLLECTOR_CREDENTIAL_PASSWORD=postgres
COLLECTOR_NAME=digital_twin COLLECTOR_NAME=digital_twin
WINDOWS_AEROS_BASE_URL=http://127.0.0.1:8800 WINDOWS_AEROS_BASE_URL=http://192.168.1.87:8800

@ -1,7 +1,7 @@
import os import os
from typing import List, Optional from typing import List, Optional
from fastapi import APIRouter, HTTPException, status from fastapi import APIRouter, HTTPException, status, File, UploadFile, Form
from src.auth.service import CurrentUser from src.auth.service import CurrentUser
from src.database.core import DbSession from src.database.core import DbSession
@ -13,12 +13,33 @@ from .service import import_aro_project
router = APIRouter() router = APIRouter()
@router.post("", response_model=StandardResponse[None])
async def import_aro(
db_session: DbSession,
schematic_name: str = Form(...),
aro_file: UploadFile = File(..., description="ARO file"),
project_name: str = "trialapi"
):
# TEST ONLY
# return {'file': 'ok'}
@router.get("", response_model=StandardResponse[None]) # Create the input object manually
async def import_aro(db_session: DbSession, aeros_project_in: AerosProjectInput, project_name: str = "trialapi"): aeros_project_input = AerosProjectInput(schematic_name=schematic_name, aro_file=aro_file)
await import_aro_project(db_session=db_session, aeros_project_in=aeros_project_in)
return {"data": None, "status": "success", "message": "Success"} result = await import_aro_project(db_session=db_session, aeros_project_in=aeros_project_input)
return {"data": None, "status": "success", "message": "Success", "result": result}
# @router.post("", response_model=StandardResponse[None])
# async def import_aro_old(db_session: DbSession, aeros_project_in: AerosProjectInput, project_name: str = "trialapi"):
# # TEST ONLY
# # return {'file': 'ok'}
# await import_aro_project(db_session=db_session, aeros_project_in=aeros_project_in)
# return {"data": None, "status": "success", "message": "Success"}
# @router.post("/import") # @router.post("/import")

@ -3,7 +3,7 @@ from typing import Optional
import httpx import httpx
from fastapi import HTTPException, status from fastapi import HTTPException, status
from sqlalchemy import Delete, Select, func from sqlalchemy import Delete, desc, Select, select, func
from sqlalchemy.orm import selectinload from sqlalchemy.orm import selectinload
from src.aeros_equipment.service import save_default_equipment from src.aeros_equipment.service import save_default_equipment
@ -17,7 +17,7 @@ from .model import AerosProject
from .schema import AerosProjectInput from .schema import AerosProjectInput
ALLOWED_EXTENSIONS = {".aro"} ALLOWED_EXTENSIONS = {".aro"}
MAX_FILE_SIZE = 5 * 1024 * 1024 # 5MB MAX_FILE_SIZE = 100 * 1024 * 1024 # 100MB
client = httpx.AsyncClient(timeout=300.0) client = httpx.AsyncClient(timeout=300.0)
@ -43,7 +43,7 @@ async def import_aro_project(*, db_session: DbSession, aeros_project_in: AerosPr
if len(content) > MAX_FILE_SIZE: if len(content) > MAX_FILE_SIZE:
raise HTTPException( raise HTTPException(
status_code=400, status_code=400,
detail="File too large. Max size: 5MB" detail="File too large. Max size: 100MB"
) )
@ -53,20 +53,21 @@ async def import_aro_project(*, db_session: DbSession, aeros_project_in: AerosPr
# Project name # Project name
project_name = filename_without_ext project_name = filename_without_ext
## save File to windows app ## save File to windows app
# Output is string of file path, examole # Output is string of file path, examole
# # Example response "C/dsad/dsad.aro" # # Example response "C/dsad/dsad.aro"
try: try:
# Reset file position since we already read it for size check # Reset file position since we already read it for size check
await file.seek(0) # await file.seek(0)
# Prepare file for upload # Prepare file for upload
files = { files = {
"file": (file.filename, await file.read(), file.content_type or "application/octet-stream") "file": (file.filename, content, file.content_type or "application/octet-stream")
} }
response = await client.post( response = await client.post(
f"{windows_aeros_base_url}/api/aeros/upload-file", f"http://192.168.1.87:8800/api/aeros/upload-file",
files=files files=files
) )
response.raise_for_status() response.raise_for_status()
@ -94,7 +95,19 @@ async def import_aro_project(*, db_session: DbSession, aeros_project_in: AerosPr
# aro_path = r"C:/Users/user/Documents/Aeros/sample_project.aro" # aro_path = r"C:/Users/user/Documents/Aeros/sample_project.aro"
aeros_project = AerosProject(project_name=project_name, aro_file_path=aro_path) aeros_project = AerosProject(project_name=project_name, aro_file_path=aro_path)
# find aeros record first, if not found, then create a new one
stmt = select(AerosProject).order_by(desc(AerosProject.created_at)).limit(1)
result = await db_session.execute(stmt)
latest_project = result.scalar_one_or_none()
# If aeros record found, then update it
if latest_project:
latest_project.project_name = project_name
latest_project.aro_file_path = aro_path
else: # else create new aeros record
db_session.add(aeros_project) db_session.add(aeros_project)
await db_session.commit() await db_session.commit()
# Update path to AEROS APP # Update path to AEROS APP

Loading…
Cancel
Save