form for upload
This commit is contained in:
@@ -2,8 +2,9 @@ import os
|
||||
import json
|
||||
import yaml
|
||||
from loguru import logger
|
||||
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi import FastAPI, UploadFile, File, Form, HTTPException, Request
|
||||
from fastapi.responses import JSONResponse, HTMLResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from converter import minimalSbomFormatConverter
|
||||
from sbom_dt_dd import generateSBOM, loadToDTrackAndDefectDojo, ApiException
|
||||
|
||||
@@ -27,6 +28,100 @@ except KeyError as e:
|
||||
app.state.config = config
|
||||
|
||||
|
||||
@app.get("/upload-form", response_class=HTMLResponse)
|
||||
async def upload_form(request: Request):
|
||||
"""
|
||||
Route serving an HTML page with the upload form
|
||||
"""
|
||||
html_content = """
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Upload Minimal SBOM</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Upload Minimal SBOM</h1>
|
||||
<form id="sbomForm">
|
||||
<label for="file">Select SBOM file:</label><br>
|
||||
<input type="file" id="file" name="file" required><br><br>
|
||||
|
||||
<label for="reimport">Reimport:</label>
|
||||
<select name="reimport" id="reimport">
|
||||
<option value="true">true</option>
|
||||
<option value="false" selected>false</option>
|
||||
</select><br><br>
|
||||
|
||||
<button type="submit">Upload SBOM</button>
|
||||
</form>
|
||||
|
||||
<div id="result"></div>
|
||||
|
||||
<script>
|
||||
document.getElementById("sbomForm").addEventListener("submit", async function(event) {
|
||||
event.preventDefault();
|
||||
|
||||
let form = document.getElementById("sbomForm");
|
||||
let formData = new FormData(form);
|
||||
|
||||
try {
|
||||
let response = await fetch("/upload-minimal-sbom/", {
|
||||
method: "POST",
|
||||
body: formData
|
||||
});
|
||||
|
||||
let resultDiv = document.getElementById("result");
|
||||
|
||||
if (response.ok) {
|
||||
let data = await response.json();
|
||||
resultDiv.innerHTML = "<p style='color:green;'>Upload successful</p>";
|
||||
} else {
|
||||
let errorData = await response.json();
|
||||
let detail = errorData.detail;
|
||||
|
||||
// Dynamisch HTML generieren
|
||||
let html = "<p style='color:red;'>Upload failed:</p><ul>";
|
||||
|
||||
for (const [key, value] of Object.entries(detail)) {
|
||||
html += "<li style='color:red'><strong>" + key + ":</strong> " + formatValue(value) + "</li>";
|
||||
}
|
||||
|
||||
html += "</ul>";
|
||||
resultDiv.innerHTML = html;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
document.getElementById("result").innerHTML = "<p style='color:red;'>Error: " + error + "</p>";
|
||||
}
|
||||
});
|
||||
|
||||
// Hilfsfunktion für verschachtelte Objekte
|
||||
function formatValue(value) {
|
||||
if (typeof value === 'object' && value !== null) {
|
||||
return "<pre>" + escapeHtml(JSON.stringify(value, null, 2)) + "</pre>";
|
||||
} else {
|
||||
return escapeHtml(value);
|
||||
}
|
||||
}
|
||||
function escapeHtml(unsafe) {
|
||||
if (unsafe === null || unsafe === undefined) {
|
||||
return '';
|
||||
}
|
||||
return String(unsafe)
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """)
|
||||
.replace(/'/g, "'");
|
||||
}
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
"""
|
||||
return HTMLResponse(content=html_content)
|
||||
|
||||
|
||||
@app.post("/upload-minimal-sbom/")
|
||||
async def uploadMinimalSBOM(
|
||||
@@ -47,13 +142,38 @@ async def uploadMinimalSBOM(
|
||||
logger.info("Done.")
|
||||
except yaml.scanner.ScannerError as e:
|
||||
logger.warning(f"uploadMinimalSBOM, yaml ScannerError: {e.context=}, {e.context_mark=}, {e.problem=}, {e.problem_mark=}, {e.note=}")
|
||||
raise HTTPException(status_code=400, detail=f"yaml ScannerError: {e.context=}, {e.context_mark=}, {e.problem=}, {e.problem_mark=}, {e.note=}")
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail={
|
||||
"error": "yaml ScannerError",
|
||||
"context": e.context,
|
||||
"context_mark": str(e.context_mark),
|
||||
"problem": e.problem,
|
||||
"problem_mark": str(e.problem_mark),
|
||||
"note": e.note
|
||||
}
|
||||
)
|
||||
except ApiException as e:
|
||||
logger.warning(f"uploadMinimalSBOM, ApiException: {type(e.cause)=}, {e.status=}, {e.reason=}, {e.body=}")
|
||||
raise HTTPException(status_code=e.status, detail=f"{type(e.cause)=}, {e.reason=}, {e.body=}, {e.data=}")
|
||||
raise HTTPException(
|
||||
status_code=e.status,
|
||||
detail={
|
||||
"type": str(type(e.cause)),
|
||||
"reason": e.reason,
|
||||
"body": e.body,
|
||||
"data": e.data
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"uploadMinimalSBOM, Exception: {type(e)=}, {str(e)=}")
|
||||
raise HTTPException(status_code=500, detail=f"Exception: {type(e)=}, {str(e)=}")
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail={
|
||||
"error": "Exception occurred",
|
||||
"type": str(type(e)),
|
||||
"message": str(e)
|
||||
}
|
||||
)
|
||||
|
||||
return JSONResponse(content={
|
||||
"message": "Upload successful!"
|
||||
@@ -81,13 +201,38 @@ async def uploadSBOM(
|
||||
logger.info("Done.")
|
||||
except json.decoder.JSONDecodeError as e:
|
||||
logger.warning(f"uploadSBOM, JSONDecodeError: {e.msg=}")
|
||||
raise HTTPException(status_code=400, detail=f"JSON decoding error: {e.msg=}, {e.doc=}, {e.pos=}, {e.lineno=}, {e.colno=}")
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail={
|
||||
"error": "JSON decoding error",
|
||||
"msg": e.msg,
|
||||
"doc": e.doc,
|
||||
"pos": e.pos,
|
||||
"lineno": e.lineno,
|
||||
"colno": e.colno
|
||||
}
|
||||
)
|
||||
except ApiException as e:
|
||||
logger.warning(f"uploadSBOM, ApiException: {type(e.cause)=}, {e.status=}, {e.reason=}, {e.body=}")
|
||||
raise HTTPException(status_code=e.status, detail=f"{type(e.cause)=}, {e.reason=}, {e.body=}, {e.data=}")
|
||||
raise HTTPException(
|
||||
status_code=e.status,
|
||||
detail={
|
||||
"type": str(type(e.cause)),
|
||||
"reason": e.reason,
|
||||
"body": e.body,
|
||||
"data": e.data
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"uploadSBOM, Exception: {type(e)=}, {str(e)=}")
|
||||
raise HTTPException(status_code=500, detail=f"Exception: {type(e)=}, {str(e)=}")
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail={
|
||||
"error": "Exception occurred",
|
||||
"type": str(type(e)),
|
||||
"message": str(e)
|
||||
}
|
||||
)
|
||||
|
||||
return JSONResponse(content={
|
||||
"message": "Upload successful!"
|
||||
|
Reference in New Issue
Block a user