Files
Glimmer/internal/server/templates/admin_bin_edit.html
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

194 lines
7.8 KiB
HTML

<!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>{{if .Paste}}Edit Paste{{else}}New Paste{{end}} — 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 active">Pastes</a>
<a href="/admin/uploads" class="tab-link">Uploads</a>
</div>
<main class="container" style="max-width:860px">
<article>
<header>
<h2>{{if .Paste}}Edit Paste{{else}}New Paste{{end}}</h2>
</header>
{{if .Error}}
<article aria-label="error" class="alert-error">{{.Error}}</article>
{{end}}
{{if .Paste}}
{{with .Paste}}
<form method="post" action="/admin/bin/edit/{{.ID}}">
{{else}}{{end}}
{{else}}
<form method="post" action="/admin/bin/new">
{{end}}
<input type="hidden" name="csrf_token" value="{{$.CSRFToken}}">
<div style="display:grid;grid-template-columns:1fr 2fr;gap:1rem;">
<label>
Name <small>(URL slug)</small>
{{if .Paste}}
{{with .Paste}}<input type="text" name="name" value="{{.Name}}" required pattern="[a-zA-Z0-9_-]+" title="Letters, numbers, hyphens, underscores">{{end}}
{{else}}
<input type="text" name="name" required pattern="[a-zA-Z0-9_-]+" placeholder="my-script" title="Letters, numbers, hyphens, underscores">
{{end}}
</label>
<label>
Title <small>(optional)</small>
{{if .Paste}}
{{with .Paste}}<input type="text" name="title" value="{{.Title}}" placeholder="A short description">{{end}}
{{else}}
<input type="text" name="title" placeholder="A short description">
{{end}}
</label>
</div>
<label>Format
<select name="format">
{{if .Paste}}
{{with .Paste}}
<option value="markdown"{{if eq .Format "markdown"}} selected{{end}}>Markdown</option>
<option value="text"{{if eq .Format "text"}} selected{{end}}>Plain text</option>
{{end}}
{{else}}
<option value="markdown" selected>Markdown</option>
<option value="text">Plain text</option>
{{end}}
</select>
</label>
<label>Content</label>
<div class="upload-controls">
<button type="button" id="upload-img-btn" class="outline secondary" 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="outline secondary" 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>
{{if .Paste}}
{{with .Paste}}<textarea name="content" rows="20" style="font-family:monospace;font-size:0.9rem">{{.Content}}</textarea>{{end}}
{{else}}
<textarea name="content" rows="20" style="font-family:monospace;font-size:0.9rem" placeholder="# Hello&#10;&#10;Write your markdown here..."></textarea>
{{end}}
<fieldset>
<legend>Access Token</legend>
{{if .Paste}}
{{with .Paste}}
{{if .Token}}
<p>Current token: <code>{{.Token}}</code> &mdash; URL: <code>/bin/{{.Name}}/{{.Token}}</code></p>
<label><input type="radio" name="token_action" value="keep" checked onchange="toggleCustomToken()"> Keep current token</label>
<label><input type="radio" name="token_action" value="regenerate" onchange="toggleCustomToken()"> Regenerate token</label>
<label><input type="radio" name="token_action" value="custom" onchange="toggleCustomToken()"> Set custom token</label>
<label><input type="radio" name="token_action" value="disable" onchange="toggleCustomToken()"> Disable token (public access)</label>
{{else}}
<p>Token is currently <strong>disabled</strong> — paste is publicly accessible.</p>
<label><input type="radio" name="token_action" value="keep" checked onchange="toggleCustomToken()"> Keep disabled</label>
<label><input type="radio" name="token_action" value="regenerate" onchange="toggleCustomToken()"> Enable with new token</label>
<label><input type="radio" name="token_action" value="custom" onchange="toggleCustomToken()"> Set custom token</label>
{{end}}
<div id="custom-token-wrap" style="display:none;margin-left:2rem">
<input type="text" name="custom_token" placeholder="Enter custom token" pattern="[a-zA-Z0-9_-]+" maxlength="64">
</div>
<label style="margin-top:0.5rem">
<input type="checkbox" name="hidden" value="1" {{if .Hidden}}checked{{end}}>
Hide paste (return 404 if accessed without token)
</label>
{{end}}
{{else}}
<label><input type="radio" name="enable_token" value="0" checked onchange="toggleCustomToken()"> No token (public access)</label>
<label><input type="radio" name="enable_token" value="1" onchange="toggleCustomToken()"> Generate access token</label>
<label><input type="radio" name="enable_token" value="custom" onchange="toggleCustomToken()"> Custom token</label>
<div id="custom-token-wrap" style="display:none;margin-left:2rem">
<input type="text" name="custom_token" placeholder="Enter custom token" pattern="[a-zA-Z0-9_-]+" maxlength="64">
</div>
<label style="margin-top:0.5rem">
<input type="checkbox" name="hidden" value="1">
Hide paste (return 404 if accessed without token)
</label>
{{end}}
</fieldset>
<div role="group" style="margin-top:1rem">
<button type="submit">Save</button>
<a href="/admin/bin" role="button" class="outline secondary">Cancel</a>
</div>
</form>
{{if .Paste}}{{with .Paste}}{{end}}{{end}}
</article>
</main>
<script src="/static/upload.js"></script>
<script>
function toggleCustomToken() {
var wrap = document.getElementById('custom-token-wrap');
if (!wrap) return;
var checked = document.querySelector('input[name="token_action"][value="custom"]:checked') ||
document.querySelector('input[name="enable_token"][value="custom"]:checked');
wrap.style.display = checked ? 'block' : 'none';
}
(function() {
var textarea = document.querySelector('textarea[name="content"]');
var status = document.getElementById('upload-status');
function insertAtCursor(text) {
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
var before = textarea.value.substring(0, start);
var after = textarea.value.substring(end);
textarea.value = before + text + '\n' + after;
textarea.selectionStart = textarea.selectionEnd = start + text.length + 1;
textarea.focus();
}
var uploader = 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: status,
maxImageMB: {{.UploadMaxMB}},
onUploaded: function(data) {
insertAtCursor(data.markdown);
status.textContent = 'Uploaded';
setTimeout(function() { status.textContent = ''; }, 3000);
}
});
textarea.addEventListener('paste', function(e) {
var items = (e.clipboardData || {}).items || [];
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf('image') !== -1) {
e.preventDefault();
uploader.doUpload(items[i].getAsFile(), true);
return;
}
}
});
})();
</script>
</body>
</html>