Files
Glimmer/internal/server/templates/admin_uploads.html
T
lucioandClaude Opus 4.8 f013e1f22b Compress over-limit images client-side to fit instead of rejecting them
Previously an image at/over the upload size limit was simply rejected with
"File too large", and the limit was hardcoded to 50 MB in the client even when
the server was configured lower. Now:

- The configured image limit (upload.max_size_mb) is surfaced to the client via
  pageData.UploadMaxMB and passed to GlimmerUpload.init({maxImageMB}).
- Selecting an over-limit image prompts to compress it in the browser to fit;
  the oversized original is never uploaded (Cancel aborts). Images over 10 MB
  but under the limit get an optional "compress for faster upload" prompt.
- compressToTarget iteratively fits the file just under the limit: it tries a
  JPEG quality ladder (0.85 → 0.4) at each resolution and halves the resolution
  on failure, down to a 320 px floor, targeting 95% of the limit for headroom.
  If it can't get under the limit it reports the smallest size achieved and
  uploads nothing.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-06-04 23:24:11 -07:00

192 lines
7.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="en" data-theme="dark">
<head>
<link rel="icon" type="image/x-icon" href="/static/favicon.ico">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Uploads — Admin</title>
<link rel="stylesheet" href="/static/pico.min.css">
<link rel="stylesheet" href="/static/style.css">
</head>
<body>
<nav>
<ul><li><strong><a href="/">Glimmer</a></strong></li></ul>
<ul>
<li><a href="/">Home</a></li>
<li>
<form method="post" action="/admin/logout" style="margin:0">
<input type="hidden" name="csrf_token" value="{{$.CSRFToken}}">
<button type="submit" class="outline secondary btn-sm">Logout</button>
</form>
</li>
</ul>
</nav>
<div class="admin-tabs">
<a href="/admin" class="tab-link">Links</a>
<a href="/admin/bin" class="tab-link">Pastes</a>
<a href="/admin/uploads" class="tab-link active">Uploads</a>
</div>
<main class="container" style="max-width:960px">
{{if .Error}}
<article aria-label="error" class="alert-error">{{.Error}}</article>
{{end}}
<article class="card-table">
<div class="section-header">
<hgroup>
<h2>Uploads</h2>
<p>{{len .Uploads}} files</p>
</hgroup>
<div class="upload-controls">
<button type="button" id="upload-img-btn" class="btn-sm" title="PNG, JPEG, GIF, WebP — up to 50 MB">Upload Image <small style="opacity:.7">(50 MB)</small></button>
<button type="button" id="upload-file-btn" class="btn-sm outline" title="Any file type — up to 5 MB">Upload File <small style="opacity:.7">(5 MB)</small></button>
<input type="file" id="img-input" accept="image/png,image/jpeg,image/gif,image/webp" style="display:none">
<input type="file" id="file-input" style="display:none">
<small id="upload-status"></small>
</div>
</div>
<figure>
<table role="grid">
<thead>
<tr>
<th>Preview</th>
<th>File</th>
<th>Size</th>
<th>Dimensions</th>
<th>Uploaded</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{{range .Uploads}}
<tr>
<td>
{{if .IsImage}}<a href="{{.URL}}" target="_blank"><img src="{{.URL}}" style="max-width:72px;max-height:52px;object-fit:contain;border-radius:4px"></a>
{{else}}<a href="{{.URL}}" target="_blank" style="font-size:1.5rem" title="Download file">&#128196;</a>
{{end}}
</td>
<td>
<div style="display:flex;align-items:center;gap:.4rem;flex-wrap:wrap">
<span class="ext-chip">{{.Ext}}</span>
{{if .OriginalName}}<span style="font-size:0.85rem">{{.OriginalName}}</span>{{end}}
</div>
<code style="font-size:0.7rem;opacity:.6">{{.Filename}}</code>
</td>
<td id="size-{{.Filename}}">{{.SizeHuman}}</td>
<td id="dim-{{.Filename}}">{{if .Width}}{{.Width}}&times;{{.Height}}{{else}}&mdash;{{end}}</td>
<td>{{.ModTime.Format "2006-01-02 15:04"}}</td>
<td>
<div role="group">
<button type="button" class="outline btn-sm" onclick="copyMarkdown('{{.Filename}}', {{.IsImage}}, {{.OriginalName}}, this)">Copy</button>
{{if .CanResize}}<button type="button" class="outline btn-sm" onclick="openResize('{{.Filename}}', {{.Width}}, {{.Height}})">Resize</button>{{end}}
<form method="post" action="/admin/uploads/delete/{{.Filename}}" onsubmit="return confirm('Delete this file?')" style="margin:0">
<input type="hidden" name="csrf_token" value="{{$.CSRFToken}}">
<button type="submit" class="outline secondary btn-sm">Del</button>
</form>
</div>
</td>
</tr>
{{else}}
<tr>
<td colspan="6">
<div class="empty-state">
<span class="empty-icon">&#128196;</span>
<p>No uploads yet. Use the buttons above to upload an image or file.</p>
</div>
</td>
</tr>
{{end}}
</tbody>
</table>
</figure>
</article>
</main>
<dialog id="resize-dialog">
<article>
<header>
<button aria-label="Close" rel="prev" onclick="document.getElementById('resize-dialog').close()"></button>
<h3>Resize Image</h3>
</header>
<p id="resize-current" style="color:var(--pico-muted-color)"></p>
<label>
Max dimension (px)
<input type="number" id="resize-dim" value="1200" min="64" max="8192" step="1">
</label>
<small id="resize-status" style="color:var(--pico-muted-color)"></small>
<footer>
<button type="button" class="secondary outline" onclick="document.getElementById('resize-dialog').close()">Cancel</button>
<button type="button" id="resize-confirm">Resize</button>
</footer>
</article>
</dialog>
<script src="/static/upload.js"></script>
<script>
var currentResizeFile = '';
function copyMarkdown(filename, isImage, originalName, btn) {
var url = '/uploads/' + filename;
var label = originalName || filename;
var md = isImage ? '![](' + url + ')' : '[' + label + '](' + url + ')';
navigator.clipboard.writeText(md).then(function() {
var orig = btn.textContent;
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = orig; }, 1500);
});
}
function openResize(filename, w, h) {
currentResizeFile = filename;
document.getElementById('resize-current').textContent = 'Current: ' + w + '×' + h;
document.getElementById('resize-status').textContent = '';
document.getElementById('resize-confirm').disabled = false;
document.getElementById('resize-dialog').showModal();
}
document.getElementById('resize-confirm').addEventListener('click', function() {
var dim = document.getElementById('resize-dim').value;
var status = document.getElementById('resize-status');
var btn = this;
status.textContent = 'Resizing…';
btn.disabled = true;
var fd = new FormData();
fd.append('csrf_token', window.GlimmerUpload.getCsrfToken());
fd.append('max_dim', dim);
fetch('/admin/uploads/resize/' + currentResizeFile, { method: 'POST', body: fd, credentials: 'same-origin' })
.then(function(r) { return r.json(); })
.then(function(data) {
btn.disabled = false;
if (data.error) {
status.textContent = 'Error: ' + data.error;
return;
}
var sizeEl = document.getElementById('size-' + currentResizeFile);
var dimEl = document.getElementById('dim-' + currentResizeFile);
if (sizeEl) sizeEl.textContent = data.size_human;
if (dimEl) dimEl.textContent = data.width + '×' + data.height;
status.textContent = 'Done — ' + data.size_human;
var imgs = document.querySelectorAll('img[src="/uploads/' + currentResizeFile + '"]');
imgs.forEach(function(img) { img.src = '/uploads/' + currentResizeFile + '?t=' + Date.now(); });
})
.catch(function() {
btn.disabled = false;
status.textContent = 'Failed';
});
});
window.GlimmerUpload.init({
imgBtn: document.getElementById('upload-img-btn'),
fileBtn: document.getElementById('upload-file-btn'),
imgInput: document.getElementById('img-input'),
fileInput: document.getElementById('file-input'),
statusEl: document.getElementById('upload-status'),
maxImageMB: {{.UploadMaxMB}},
onUploaded: function() { location.reload(); }
});
</script>
</body>
</html>