Upload files to "templates/admin"

This commit is contained in:
2026-07-24 15:29:41 +00:00
parent a24eae242c
commit 4a871e9114
5 changed files with 1942 additions and 0 deletions

View File

@@ -0,0 +1,157 @@
{% extends 'admin/custom_base.html' %}
{% block body %}
<div class="container-fluid mt-3">
<h4 class="mb-3">DLL Archive</h4>
{% if SCOPE_ADMIN in scopes %}
<div class="card mb-4">
<div class="card-header bg-primary text-white"><strong>+ Upload New DLL</strong></div>
<div class="card-body">
<div class="form-row align-items-end">
<div class="form-group col-md-4 mb-2">
<label class="small font-weight-bold">Label (optional)</label>
<input type="text" class="form-control" id="up-label" placeholder="e.g. v1.3 March patch">
</div>
<div class="form-group col-md-5 mb-2">
<label class="small font-weight-bold">DLL File <span class="text-danger">*</span></label>
<div class="custom-file">
<input type="file" class="custom-file-input" id="up-file" accept=".dll"
onchange="document.getElementById('up-file-lbl').textContent = this.files[0]?.name || 'Choose .dll file'">
<label class="custom-file-label" id="up-file-lbl">Choose .dll file</label>
</div>
</div>
<div class="form-group col-md-3 mb-2">
<label class="small font-weight-bold">&nbsp;</label>
<button class="btn btn-success btn-block d-block" onclick="doUpload()">Upload</button>
</div>
</div>
<div id="up-result" class="mt-2"></div>
</div>
</div>
{% endif %}
<div class="card">
<div class="card-header"><strong>All DLLs</strong></div>
<div class="card-body p-0">
<table class="table table-sm table-hover mb-0">
<thead class="thead-dark">
<tr>
<th>Label</th>
<th>Filename</th>
<th>Size</th>
<th>Uploaded</th>
<th>By</th>
<th>Branch</th>
<th>SHA-512</th>
{% if SCOPE_ADMIN in scopes %}<th>Actions</th>{% endif %}
</tr>
</thead>
<tbody id="dll-tbody">
<tr><td colspan="8" class="text-center text-muted py-4">Loading…</td></tr>
</tbody>
</table>
</div>
</div>
</div>
<script>
const IS_ADMIN = {{ 'true' if SCOPE_ADMIN in scopes else 'false' }};
const BRANCHES = {
'bff0c094-2d59-4f81-93dd-fc6f177ca584': 'Prod',
'fb4ce906-59c1-4ffd-af5e-6696dfad1be2': 'Dev',
'f94fa96b-7438-4323-825f-ff4dc4055db1': 'BVT',
};
function fmtSize(b) {
if (!b) return '—';
if (b >= 1073741824) return (b/1073741824).toFixed(2)+' GB';
if (b >= 1048576) return (b/1048576).toFixed(2)+' MB';
return (b/1024).toFixed(1)+' KB';
}
function esc(s) { return String(s).replace(/[&<>"']/g, c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c])); }
async function loadDlls() {
try {
const r = await fetch('/admin/api/dll_archive');
const rows = await r.json();
const tbody = document.getElementById('dll-tbody');
if (!rows.length) {
tbody.innerHTML = '<tr><td colspan="8" class="text-center text-muted py-4">No DLLs in archive yet.</td></tr>';
return;
}
tbody.innerHTML = rows.map(d => {
const branchBadge = d.branch
? `<span class="badge badge-success">${esc(d.branch)}</span>`
: '<span class="text-muted">—</span>';
const sha = d.sha512 ? `<code class="small">${esc(d.sha512.slice(0,16))}…</code>` : '—';
const actions = IS_ADMIN ? `
<button class="btn btn-xs btn-outline-secondary mr-1" onclick="editLabel('${esc(d.filename)}','${esc(d.label)}')">Label</button>
<div class="btn-group">
<button class="btn btn-xs btn-outline-primary dropdown-toggle" data-toggle="dropdown">Assign</button>
<div class="dropdown-menu">
${Object.entries(BRANCHES).map(([id,name])=>`<a class="dropdown-item" href="#" onclick="assignBranch('${esc(d.filename)}','${id}');return false;">${name}</a>`).join('')}
</div>
</div>` : '';
return `<tr>
<td>${d.label ? `<strong>${esc(d.label)}</strong>` : '<span class="text-muted">—</span>'}</td>
<td><code class="small">${esc(d.filename)}</code></td>
<td>${fmtSize(d.file_size)}</td>
<td>${esc(d.uploaded_at||'—')}</td>
<td>${esc(d.uploaded_by||'—')}</td>
<td>${branchBadge}</td>
<td>${sha}</td>
${IS_ADMIN ? `<td style="white-space:nowrap">${actions}</td>` : ''}
</tr>`;
}).join('');
} catch(e) {
document.getElementById('dll-tbody').innerHTML = '<tr><td colspan="8" class="text-danger text-center py-3">Failed to load.</td></tr>';
}
}
async function assignBranch(filename, branchId) {
const name = BRANCHES[branchId];
if (!confirm(`Assign "${filename}" to ${name}? This updates KittyRecDlls.json immediately.`)) return;
const r = await fetch('/admin/api/dll_archive/assign', {method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify({filename, branch_id: branchId})});
const d = await r.json();
if (d.ok) loadDlls();
else KR.toast(d.error || 'Failed', 'error');
}
async function editLabel(filename, current) {
const label = prompt('Label for this DLL:', current);
if (label === null) return;
const r = await fetch('/admin/api/dll_archive/update_label', {method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify({filename, label: label.trim()})});
const d = await r.json();
if (d.ok) loadDlls();
else KR.toast(d.error || 'Failed', 'error');
}
async function doUpload() {
const fileEl = document.getElementById('up-file');
const label = document.getElementById('up-label').value.trim();
if (!fileEl.files[0]) { KR.toast('Select a .dll file.', 'error'); return; }
const result = document.getElementById('up-result');
result.innerHTML = '<div class="text-muted small">Uploading…</div>';
const fd = new FormData();
fd.append('file', fileEl.files[0]);
fd.append('label', label);
try {
const r = await fetch('/admin/api/dll_archive/upload', {method:'POST', body: fd});
const d = await r.json();
if (d.ok) {
result.innerHTML = `<div class="alert alert-success p-2 small">Uploaded: <code>${d.filename}</code></div>`;
fileEl.value = '';
document.getElementById('up-file-lbl').textContent = 'Choose .dll file';
document.getElementById('up-label').value = '';
loadDlls();
} else {
result.innerHTML = `<div class="alert alert-danger p-2 small">${d.error||'Upload failed'}</div>`;
}
} catch(e) {
result.innerHTML = '<div class="alert alert-danger p-2 small">Network error</div>';
}
}
loadDlls();
</script>
{% endblock %}