|
|
|
@ -21,43 +21,77 @@ MAX_FILE_SIZE = 5 * 1024 * 1024 # 5MB
|
|
|
|
client = httpx.AsyncClient(timeout=300.0)
|
|
|
|
client = httpx.AsyncClient(timeout=300.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def import_aro_project(*, db_session: DbSession):
|
|
|
|
async def import_aro_project(*, db_session: DbSession, aeros_project_in: AerosProjectInput):
|
|
|
|
# file = aeros_project_in.aro_file
|
|
|
|
windows_aeros_base_url = os.getenv("WINDOWS_AEROS_BASE_URL", "http://localhost:8800")
|
|
|
|
|
|
|
|
file = aeros_project_in.aro_file
|
|
|
|
# file_ext = os.path.splitext(file.filename)[1].lower()
|
|
|
|
|
|
|
|
|
|
|
|
# Get filename
|
|
|
|
|
|
|
|
filename_without_ext = os.path.splitext(file.filename)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Get file extension
|
|
|
|
|
|
|
|
file_ext = os.path.splitext(file.filename)[1].lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Validate file extension
|
|
|
|
|
|
|
|
if file_ext not in ALLOWED_EXTENSIONS:
|
|
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
|
|
status_code=400,
|
|
|
|
|
|
|
|
detail=f"File type not allowed. Allowed: {ALLOWED_EXTENSIONS}"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# if file_ext not in ALLOWED_EXTENSIONS:
|
|
|
|
# Read and check file size
|
|
|
|
# raise HTTPException(
|
|
|
|
content = await file.read()
|
|
|
|
# status_code=400,
|
|
|
|
if len(content) > MAX_FILE_SIZE:
|
|
|
|
# detail=f"File type not allowed. Allowed: {ALLOWED_EXTENSIONS}"
|
|
|
|
raise HTTPException(
|
|
|
|
# )
|
|
|
|
status_code=400,
|
|
|
|
|
|
|
|
detail="File too large. Max size: 5MB"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# # Read and check file size
|
|
|
|
|
|
|
|
# content = await file.read()
|
|
|
|
|
|
|
|
# if len(content) > MAX_FILE_SIZE:
|
|
|
|
|
|
|
|
# raise HTTPException(
|
|
|
|
|
|
|
|
# status_code=400,
|
|
|
|
|
|
|
|
# detail="File too large. Max size: 5MB"
|
|
|
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
project_name = "trialapi"
|
|
|
|
# Project name hardcode
|
|
|
|
|
|
|
|
# project_name = "trialapi"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Project name
|
|
|
|
|
|
|
|
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:
|
|
|
|
# response = await client.post(
|
|
|
|
# Reset file position since we already read it for size check
|
|
|
|
# f"{windows_BASE_URL}/upload",
|
|
|
|
await file.seek(0)
|
|
|
|
# json=f"/",
|
|
|
|
|
|
|
|
# headers={"Content-Type": "application/json"},
|
|
|
|
# Prepare file for upload
|
|
|
|
# )
|
|
|
|
files = {
|
|
|
|
# response.raise_for_status()
|
|
|
|
"file": (file.filename, await file.read(), file.content_type or "application/octet-stream")
|
|
|
|
# except Exception as e:
|
|
|
|
}
|
|
|
|
# raise HTTPException(
|
|
|
|
|
|
|
|
# status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)
|
|
|
|
response = await client.post(
|
|
|
|
# )
|
|
|
|
f"{windows_aeros_base_url}/api/aeros/upload-file",
|
|
|
|
|
|
|
|
files=files
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Get the file path from the response
|
|
|
|
|
|
|
|
upload_result = response.json()
|
|
|
|
|
|
|
|
aro_path = upload_result.get("full_path")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not aro_path:
|
|
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
|
|
status_code=500,
|
|
|
|
|
|
|
|
detail="Failed to get file path from upload response"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except httpx.HTTPStatusError as e:
|
|
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
|
|
status_code=e.response.status_code,
|
|
|
|
|
|
|
|
detail=f"Upload failed: {e.response.text}"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
db_session.add(aeros_project)
|
|
|
|
db_session.add(aeros_project)
|
|
|
|
|