Probably added gzip

This commit is contained in:
Jonas Linter
2025-10-08 14:36:21 +02:00
parent 9f0a77ca39
commit 6701dcd6bf

View File

@@ -555,6 +555,7 @@ async def handle_xml_upload(
): ):
"""Endpoint for receiving XML files for conversion processing. """Endpoint for receiving XML files for conversion processing.
Requires basic authentication and saves XML files to log directory. Requires basic authentication and saves XML files to log directory.
Supports gzip compression via Content-Encoding header.
""" """
try: try:
# Get the raw body content # Get the raw body content
@@ -565,6 +566,20 @@ async def handle_xml_upload(
status_code=400, detail="ERROR: No XML content provided" status_code=400, detail="ERROR: No XML content provided"
) )
# Check if content is gzip compressed
content_encoding = request.headers.get("content-encoding", "").lower()
is_gzipped = content_encoding == "gzip"
# Decompress if gzipped
if is_gzipped:
try:
body = gzip.decompress(body)
except Exception as e:
raise HTTPException(
status_code=400,
detail=f"ERROR: Failed to decompress gzip content: {e}",
) from e
# Try to decode as UTF-8 # Try to decode as UTF-8
try: try:
xml_content = body.decode("utf-8") xml_content = body.decode("utf-8")
@@ -594,21 +609,20 @@ async def handle_xml_upload(
_LOGGER.info("XML file saved to %s by user %s", log_filename, username) _LOGGER.info("XML file saved to %s by user %s", log_filename, username)
return { response_headers = {
"status": "success", "Content-Type": "application/xml; charset=utf-8",
"message": "XML file received and saved", "X-AlpineBits-Server-Accept-Encoding": "gzip",
"filename": log_filename.name,
"size_bytes": len(body),
"authenticated_user": username,
} }
return Response(
content="Xml received", headers=response_headers, status_code=200
)
except HTTPException: except HTTPException:
raise raise
except Exception as e: except Exception:
_LOGGER.exception("Error in handle_xml_upload") _LOGGER.exception("Error in handle_xml_upload")
raise HTTPException( raise HTTPException(status_code=500, detail="Error processing XML upload")
status_code=500, detail="Error processing XML upload"
) from e
# UNUSED # UNUSED