Media page shows the whole library, multi-file upload; pill pickers on Redeems

- src/library.js: one list of every media file (built-in public/alerts + uploads),
  shared by the Media page and the Redeems pickers.
- Media page rebuilt: drag & drop / multi-file upload with progress (audio, video,
  images for stickers), Videos / Sounds / Stickers & art with previews, size,
  'used by N' and a filter; only uploads can be deleted.
- Fix: /media no longer 301-redirects to /media/ (express.static redirect:false).
- Redeems: sound / video / sticker / TTS voice are pills (click to change, x to
  clear) with a type-to-search list (arrows, Enter, Esc) instead of dropdowns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-11-22 16:24:18 +02:00
parent 8ac8fe024c
commit a32a55c264
6 changed files with 313 additions and 109 deletions

View file

@ -1,53 +1,124 @@
<style>
.drop { border:2px dashed var(--pico-muted-border-color); border-radius:12px; padding:1.2rem; text-align:center; cursor:pointer;
transition:border-color .15s, background .15s; margin-bottom:.6rem; }
.drop.over { border-color:var(--pico-primary); background:rgba(1, 114, 173, .12); }
.drop strong { display:block; margin-bottom:.2rem; }
.drop input { display:none; }
.upbar { height:6px; border-radius:3px; background:var(--pico-muted-border-color); overflow:hidden; margin:.4rem 0; }
.upbar > div { height:100%; width:0; background:var(--pico-primary); transition:width .1s; }
.tools { display:flex; gap:.8rem; align-items:center; flex-wrap:wrap; margin:1rem 0 .4rem; }
.tools input { margin:0; max-width:320px; }
.tools .meta, .meta { color:var(--pico-muted-color); font-size:.85rem; }
.lib-h { margin:1.4rem 0 .5rem; display:flex; align-items:baseline; gap:.6rem; }
.grid { display:grid; grid-template-columns:repeat(auto-fill, minmax(210px, 1fr)); gap:.6rem; }
.item { border:1px solid var(--pico-muted-border-color); border-radius:10px; padding:.5rem; display:flex; flex-direction:column; gap:.35rem; }
.item .pv { height:110px; border-radius:6px; background:#0004; display:flex; align-items:center; justify-content:center; overflow:hidden; }
.item .pv img { max-width:100%; max-height:100%; object-fit:contain; }
.item .pv video { max-width:100%; max-height:100%; }
.item .name { font-weight:600; font-size:.88rem; overflow-wrap:anywhere; }
.item .row { display:flex; gap:.35rem; align-items:center; flex-wrap:wrap; font-size:.75rem; color:var(--pico-muted-color); }
.pill { font-size:.7rem; padding:.02rem .45rem; border-radius:99px; border:1px solid var(--pico-muted-border-color); }
.pill.up { border-color:#3fb950; color:#7ee08f; }
.pill.used { border-color:#a970ff; color:#c9a8ff; }
.item button, .item audio { margin:0; }
.item audio { width:100%; height:30px; }
.btn-sm { margin:0; padding:.2rem .6rem; font-size:.75rem; width:auto; }
.item form { margin:0 0 0 auto; }
.hidden-by-filter { display:none !important; }
</style>
<h2>Media library</h2>
<p><small>Upload audio (MP3, WAV, OGG) or video (MP4, WebM) files to use in actions. Max 50 MB.</small></p>
<form method="POST" action="/media/upload" enctype="multipart/form-data">
<div style="display:flex; gap:1rem; align-items:flex-end;">
<label style="flex:1;">
File
<input type="file" name="file" accept="audio/*,video/*" required>
</label>
<button type="submit">Upload</button>
<div class="drop" id="drop" title="Audio: MP3, WAV, OGG, M4A. Video: WebM (transparent looks best over the game), MP4. Images (stickers): PNG, WebP, GIF, JPG. Max 50 MB per file, up to 20 at once.">
<strong>Drop files here, or click to choose</strong>
<span class="meta">sounds, videos or sticker images · several at once</span>
<input type="file" id="files" multiple accept="audio/*,video/*,image/png,image/webp,image/gif,image/jpeg">
</div>
<div id="up" hidden><div class="upbar"><div id="upfill"></div></div><span class="meta" id="upmsg"></span></div>
<div class="tools">
<input type="search" id="filter" placeholder="Filter by name…">
<span class="meta" title="Built-in files ship with HeyStreamer (public/alerts) and are changed in the code. Uploads live only on the server and can be deleted here.">
<span class="pill">built-in</span> <span class="pill up">uploaded</span> <span class="pill used">used by N</span> ⓘ
</span>
</div>
<% for (const g of groups) { %>
<h3 class="lib-h" data-group><%= g.name %> <span class="meta"><%= g.items.length %></span></h3>
<% if (!g.items.length) { %><p class="meta"><%= g.type === 'video' ? 'No videos yet: drop some above.' : 'Nothing here yet.' %></p><% } %>
<div class="grid">
<% for (const i of g.items) { %>
<div class="item" data-name="<%= (i.label + ' ' + i.file).toLowerCase() %>">
<div class="pv">
<% if (i.type === 'image') { %><img src="<%= i.url %>" alt="" loading="lazy">
<% } else if (i.type === 'video') { %><video src="<%= i.url %>#t=0.5" preload="metadata" muted playsinline data-hoverplay title="Hover to play (muted). Click for sound."></video>
<% } else { %><span style="font-size:2rem;">🔊</span><% } %>
</div>
<div class="name" title="<%= i.url %>"><%= i.label %></div>
<% if (i.type === 'audio') { %><audio controls preload="none" src="<%= i.url %>"></audio><% } %>
<div class="row">
<span class="pill <%= i.source === 'upload' ? 'up' : '' %>"><%= i.source === 'upload' ? 'uploaded' : 'built-in' %></span>
<% if (i.note) { %><span class="pill" title="Loudness-matched sound"><%= i.note %></span><% } %>
<% if (i.used) { %><span class="pill used" title="Used by <%= i.used %> redeem variation<%= i.used === 1 ? '' : 's' %>">used by <%= i.used %></span><% } %>
<span><%= i.size > 1048576 ? (i.size / 1048576).toFixed(1) + ' MB' : Math.max(1, Math.round(i.size / 1024)) + ' KB' %></span>
<% if (i.source === 'upload') { %>
<form method="POST" action="/media/<%= i.id %>/delete">
<button type="button" data-confirm class="outline btn-sm" style="color:#f88; border-color:#f88;"
title="<%= i.used ? `Used by ${i.used} variation(s): they will play nothing until you pick another file` : 'Delete this upload' %>">Delete</button>
</form>
<% } %>
</div>
</div>
<% } %>
</div>
</form>
<% if (files.length === 0) { %>
<p><em>No files uploaded yet.</em></p>
<% } else { %>
<figure>
<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Size</th>
<th>Uploaded</th>
<th>Preview</th>
<th></th>
</tr>
</thead>
<tbody>
<% for (const f of files) { %>
<tr>
<td><%= f.original_name %></td>
<td><code><%= f.media_type %></code></td>
<td><%= (f.size_bytes / 1024).toFixed(0) %> KB</td>
<td><small><%= f.uploaded_at %></small></td>
<td>
<% if (f.media_type === 'audio') { %>
<audio controls src="/media/<%= f.filename %>" style="height:28px; width:160px;"></audio>
<% } else { %>
<video controls src="/media/<%= f.filename %>" style="height:40px; width:80px;"></video>
<% } %>
</td>
<td>
<form method="POST" action="/media/<%= f.id %>/delete">
<button type="button" data-confirm class="outline" style="padding:0.2rem 0.6rem; font-size:0.8rem; color:#f88;">Delete</button>
</form>
</td>
</tr>
<% } %>
</tbody>
</table>
</figure>
<% } %>
<script>
(() => {
// upload: drag & drop or click, several files, progress bar, then reload the list
const drop = document.getElementById('drop'), input = document.getElementById('files');
const up = document.getElementById('up'), fill = document.getElementById('upfill'), msg = document.getElementById('upmsg');
drop.addEventListener('click', () => input.click());
['dragenter', 'dragover'].forEach(t => drop.addEventListener(t, e => { e.preventDefault(); drop.classList.add('over'); }));
['dragleave', 'drop'].forEach(t => drop.addEventListener(t, e => { e.preventDefault(); drop.classList.remove('over'); }));
drop.addEventListener('drop', e => send(e.dataTransfer.files));
input.addEventListener('change', () => send(input.files));
function send(files) {
if (!files || !files.length) return;
const fd = new FormData(); for (const f of files) fd.append('files', f);
const x = new XMLHttpRequest();
x.open('POST', '/media/upload'); x.setRequestHeader('Accept', 'application/json'); x.setRequestHeader('X-File-Count', files.length);
up.hidden = false; fill.style.width = '0'; msg.textContent = `Uploading ${files.length} file${files.length === 1 ? '' : 's'}…`;
x.upload.onprogress = e => { if (e.lengthComputable) fill.style.width = (100 * e.loaded / e.total) + '%'; };
x.onload = () => {
let j = {}; try { j = JSON.parse(x.responseText); } catch {}
if (x.status === 200 && j.ok) {
msg.textContent = `Uploaded ${j.saved}` + (j.skipped ? ` · skipped ${j.skipped} (not audio, video or image)` : '') + ' · refreshing…';
setTimeout(() => location.reload(), 600);
} else msg.textContent = 'Upload failed: ' + (j.error || x.status);
};
x.onerror = () => { msg.textContent = 'Upload failed (connection)'; };
x.send(fd);
}
// filter
document.getElementById('filter').addEventListener('input', e => {
const q = e.target.value.trim().toLowerCase();
document.querySelectorAll('.item').forEach(i => i.classList.toggle('hidden-by-filter', !!q && !i.dataset.name.includes(q)));
// hide sections with nothing left
document.querySelectorAll('h3[data-group]').forEach(h => {
const grid = h.nextElementSibling?.classList.contains('grid') ? h.nextElementSibling : h.nextElementSibling?.nextElementSibling;
const any = !!grid?.querySelector('.item:not(.hidden-by-filter)');
h.classList.toggle('hidden-by-filter', !!q && !any);
});
});
// videos: play muted on hover, with sound on click
document.querySelectorAll('[data-hoverplay]').forEach(v => {
v.addEventListener('mouseenter', () => { v.play().catch(() => {}); });
v.addEventListener('mouseleave', () => { v.pause(); v.currentTime = 0.5; v.muted = true; });
v.addEventListener('click', () => { v.muted = false; v.currentTime = 0; v.play().catch(() => {}); });
});
})();
</script>