feat: download support in attestation view
Browse files- static/js/main.js +30 -1
- templates/attestations.html +4 -4
- web/routes.py +33 -0
static/js/main.js
CHANGED
|
@@ -611,9 +611,12 @@ async function viewAttestation(id) {
|
|
| 611 |
<pre class="bg-dark p-3 rounded"><code>${context && Object.keys(context).length > 0 ? JSON.stringify(context, null, 2) : 'No context available.'}</code></pre>
|
| 612 |
</div>
|
| 613 |
<div class="mt-3 text-end">
|
| 614 |
-
<button class="btn btn-outline-secondary btn-sm" id="toggle-json-viewer">
|
| 615 |
<i data-feather="code"></i> View Raw JSON
|
| 616 |
</button>
|
|
|
|
|
|
|
|
|
|
| 617 |
</div>
|
| 618 |
</div>
|
| 619 |
<div id="attestation-json-section" style="display:none;">
|
|
@@ -636,7 +639,33 @@ async function viewAttestation(id) {
|
|
| 636 |
setTimeout(() => {
|
| 637 |
const toggleJsonBtn = document.getElementById('toggle-json-viewer');
|
| 638 |
const toggleDetailsBtn = document.getElementById('toggle-details-viewer');
|
|
|
|
| 639 |
let jsonEditorInstance = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 640 |
if (toggleJsonBtn) {
|
| 641 |
toggleJsonBtn.onclick = async () => {
|
| 642 |
document.getElementById('attestation-details-section').style.display = 'none';
|
|
|
|
| 611 |
<pre class="bg-dark p-3 rounded"><code>${context && Object.keys(context).length > 0 ? JSON.stringify(context, null, 2) : 'No context available.'}</code></pre>
|
| 612 |
</div>
|
| 613 |
<div class="mt-3 text-end">
|
| 614 |
+
<button class="btn btn-outline-secondary btn-sm me-2" id="toggle-json-viewer">
|
| 615 |
<i data-feather="code"></i> View Raw JSON
|
| 616 |
</button>
|
| 617 |
+
<button class="btn btn-outline-primary btn-sm" id="download-attestation-file">
|
| 618 |
+
<i data-feather="download"></i> Download JSON
|
| 619 |
+
</button>
|
| 620 |
</div>
|
| 621 |
</div>
|
| 622 |
<div id="attestation-json-section" style="display:none;">
|
|
|
|
| 639 |
setTimeout(() => {
|
| 640 |
const toggleJsonBtn = document.getElementById('toggle-json-viewer');
|
| 641 |
const toggleDetailsBtn = document.getElementById('toggle-details-viewer');
|
| 642 |
+
const downloadBtn = document.getElementById('download-attestation-file');
|
| 643 |
let jsonEditorInstance = null;
|
| 644 |
+
|
| 645 |
+
// Setup download button
|
| 646 |
+
if (downloadBtn) {
|
| 647 |
+
downloadBtn.onclick = async () => {
|
| 648 |
+
try {
|
| 649 |
+
// Get the filename from the storage path or use a default name
|
| 650 |
+
const fileName = attestation.storage_path ?
|
| 651 |
+
attestation.storage_path.split('/').pop() :
|
| 652 |
+
`attestation_${id}.json`;
|
| 653 |
+
|
| 654 |
+
// Create a download link for the file
|
| 655 |
+
const downloadUrl = `/attestations/${id}/file?download=1`;
|
| 656 |
+
const link = document.createElement('a');
|
| 657 |
+
link.href = downloadUrl;
|
| 658 |
+
link.setAttribute('download', fileName);
|
| 659 |
+
document.body.appendChild(link);
|
| 660 |
+
link.click();
|
| 661 |
+
document.body.removeChild(link);
|
| 662 |
+
} catch (err) {
|
| 663 |
+
console.error('Download error:', err);
|
| 664 |
+
alert('Failed to download attestation file');
|
| 665 |
+
}
|
| 666 |
+
};
|
| 667 |
+
}
|
| 668 |
+
|
| 669 |
if (toggleJsonBtn) {
|
| 670 |
toggleJsonBtn.onclick = async () => {
|
| 671 |
document.getElementById('attestation-details-section').style.display = 'none';
|
templates/attestations.html
CHANGED
|
@@ -105,14 +105,14 @@
|
|
| 105 |
</div>
|
| 106 |
</td>
|
| 107 |
<td>
|
| 108 |
-
<
|
| 109 |
{% if attestation.sig_status == 'signed' %}
|
| 110 |
-
<i data-feather="lock" class="
|
| 111 |
{% else %}
|
| 112 |
-
<i data-feather="unlock" class="
|
| 113 |
{% endif %}
|
| 114 |
{{ attestation.target_type }}
|
| 115 |
-
</
|
| 116 |
</td>
|
| 117 |
<td>{{ attestation.tool or 'Unknown' }}</td>
|
| 118 |
<td>
|
|
|
|
| 105 |
</div>
|
| 106 |
</td>
|
| 107 |
<td>
|
| 108 |
+
<div class="d-inline-flex align-items-center">
|
| 109 |
{% if attestation.sig_status == 'signed' %}
|
| 110 |
+
<i data-feather="lock" class="text-success me-2" style="width: 16px; height: 16px;" title="Signed attestation"></i>
|
| 111 |
{% else %}
|
| 112 |
+
<i data-feather="unlock" class="text-warning me-2" style="width: 16px; height: 16px;" title="Unsigned attestation"></i>
|
| 113 |
{% endif %}
|
| 114 |
{{ attestation.target_type }}
|
| 115 |
+
</div>
|
| 116 |
</td>
|
| 117 |
<td>{{ attestation.tool or 'Unknown' }}</td>
|
| 118 |
<td>
|
web/routes.py
CHANGED
|
@@ -445,7 +445,40 @@ def attestation_file_json(attestation_id):
|
|
| 445 |
content, content_type, is_json, error = get_attestation_file_content(attestation)
|
| 446 |
if error:
|
| 447 |
return jsonify({'error': error}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
|
|
|
|
| 449 |
if is_json:
|
| 450 |
# Already parsed JSON object
|
| 451 |
return jsonify(content)
|
|
|
|
| 445 |
content, content_type, is_json, error = get_attestation_file_content(attestation)
|
| 446 |
if error:
|
| 447 |
return jsonify({'error': error}), 500
|
| 448 |
+
|
| 449 |
+
# Check if download is requested
|
| 450 |
+
if request.args.get('download') == '1':
|
| 451 |
+
# Create a filename from the storage path or use a default
|
| 452 |
+
filename = os.path.basename(attestation.storage_path) if attestation.storage_path else f"attestation_{attestation_id}.json"
|
| 453 |
+
|
| 454 |
+
# For JSON content, pretty print it
|
| 455 |
+
if is_json:
|
| 456 |
+
file_data = json.dumps(content, indent=2).encode('utf-8')
|
| 457 |
+
return send_file(
|
| 458 |
+
io.BytesIO(file_data),
|
| 459 |
+
mimetype='application/json',
|
| 460 |
+
as_attachment=True,
|
| 461 |
+
download_name=filename
|
| 462 |
+
)
|
| 463 |
+
# For binary content
|
| 464 |
+
elif isinstance(content, bytes):
|
| 465 |
+
return send_file(
|
| 466 |
+
io.BytesIO(content),
|
| 467 |
+
mimetype=content_type or 'application/octet-stream',
|
| 468 |
+
as_attachment=True,
|
| 469 |
+
download_name=filename
|
| 470 |
+
)
|
| 471 |
+
# For text content
|
| 472 |
+
else:
|
| 473 |
+
text_content = str(content).encode('utf-8')
|
| 474 |
+
return send_file(
|
| 475 |
+
io.BytesIO(text_content),
|
| 476 |
+
mimetype='text/plain',
|
| 477 |
+
as_attachment=True,
|
| 478 |
+
download_name=filename
|
| 479 |
+
)
|
| 480 |
|
| 481 |
+
# Normal JSON response for the viewer
|
| 482 |
if is_json:
|
| 483 |
# Already parsed JSON object
|
| 484 |
return jsonify(content)
|