From a32a55c2646ad8e45fb5c6cb587e642cf292f9ef Mon Sep 17 00:00:00 2001 From: Bashy Date: Sun, 22 Nov 2026 16:24:18 +0200 Subject: [PATCH] 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 --- src/app.js | 3 +- src/library.js | 58 ++++++++++++++ src/routers/media.js | 44 ++++++++--- src/routers/redeems.js | 32 ++------ views/media.ejs | 169 +++++++++++++++++++++++++++++------------ views/redeems.ejs | 116 +++++++++++++++++++++++----- 6 files changed, 313 insertions(+), 109 deletions(-) create mode 100644 src/library.js diff --git a/src/app.js b/src/app.js index 72d74ac..93ba2fa 100644 --- a/src/app.js +++ b/src/app.js @@ -22,7 +22,8 @@ app.use((req, res, next) => { next(); }); -app.use(express.static(path.join(__dirname, '../public'))); +// redirect:false: /media (the uploads folder) must not bounce to /media/ before the Media page route +app.use(express.static(path.join(__dirname, '../public'), { redirect: false })); app.use(express.urlencoded({ extended: true })); app.use(express.json()); diff --git a/src/library.js b/src/library.js new file mode 100644 index 0000000..d1be6fa --- /dev/null +++ b/src/library.js @@ -0,0 +1,58 @@ +// Every media file HeyStreamer can use, in one list: the built-in library under public/alerts (shipped with +// the code) and uploads from the Media page (public/media, kept only on the server, listed in media_files). +// Used by the Media page and by the Redeems pickers, so both always show the same files. +// { url, label, type: 'audio' | 'video' | 'image', source: 'library' | 'upload', id?, size, file } +const fs = require('fs'); +const path = require('path'); +const db = require('./db'); + +const PUBLIC = path.join(__dirname, '..', 'public'); +const RE = { audio: /\.(mp3|wav|ogg|m4a)$/i, video: /\.(webm|mp4|mov)$/i, image: /\.(png|webp|gif|jpe?g)$/i }; + +// built-in folders, in the order they are listed +const DIRS = [ + { dir: 'alerts/videos', type: 'video' }, + { dir: 'alerts/sounds/redeems', type: 'audio', note: 'normalized' }, + { dir: 'alerts/sounds', type: 'audio' }, + { dir: 'alerts/stickers', type: 'image' }, + { dir: 'alerts/art', type: 'image' }, +]; + +const pretty = f => f.replace(/\.\w+$/, '').replace(/_Contrast$/, '').replace(/[_]+/g, ' '); + +function typeOf(mime, name) { + if (/^video\//.test(mime) || RE.video.test(name)) return 'video'; + if (/^audio\//.test(mime) || RE.audio.test(name)) return 'audio'; + if (/^image\/(png|webp|gif|jpeg)$/.test(mime) || RE.image.test(name)) return 'image'; + return null; +} + +function list() { + const out = []; + for (const d of DIRS) { + let files = []; + try { files = fs.readdirSync(path.join(PUBLIC, d.dir)).filter(f => RE[d.type].test(f)).sort(); } catch { /* folder missing */ } + for (const f of files) { + let size = 0; + try { size = fs.statSync(path.join(PUBLIC, d.dir, f)).size; } catch {} + out.push({ url: `/${d.dir}/${f}`, label: pretty(f), file: f, type: d.type, source: 'library', size, note: d.note || null }); + } + } + for (const m of db.getAllMedia()) { + const type = m.media_type === 'image' ? 'image' : m.media_type === 'video' ? 'video' : 'audio'; + out.push({ url: `/media/${m.filename}`, label: pretty(m.original_name), file: m.original_name, type, source: 'upload', + id: m.id, size: m.size_bytes, uploaded_at: m.uploaded_at }); + } + return out; +} + +const ofType = type => list().filter(i => i.type === type); + +// how many redeem variations use each URL (as sound, video or sticker) +function usage() { + const n = new Map(); + for (const v of db.getAllVariations()) for (const u of [v.sound, v.video, v.art]) if (u) n.set(u, (n.get(u) || 0) + 1); + return n; +} + +module.exports = { list, ofType, usage, typeOf, PUBLIC }; diff --git a/src/routers/media.js b/src/routers/media.js index 42159c4..d327b7f 100644 --- a/src/routers/media.js +++ b/src/routers/media.js @@ -5,27 +5,47 @@ const path = require('path'); const fs = require('fs'); const { v4: uuidv4 } = require('uuid'); const db = require('../db'); +const library = require('../library'); +// uploads go to public/media (not in git, never touched by deploys); audio, video and images (stickers) const storage = multer.diskStorage({ destination: path.join(__dirname, '../../public/media'), - filename: (req, file, cb) => cb(null, uuidv4() + path.extname(file.originalname)), + filename: (req, file, cb) => cb(null, uuidv4() + path.extname(file.originalname).toLowerCase()), +}); +const upload = multer({ + storage, limits: { fileSize: 50 * 1024 * 1024, files: 20 }, + fileFilter: (req, file, cb) => cb(null, !!library.typeOf(file.mimetype, file.originalname)), }); -const upload = multer({ storage, limits: { fileSize: 50 * 1024 * 1024 } }); router.get('/', (req, res) => { - res.render('media', { files: db.getAllMedia() }); + const items = library.list(); + const used = library.usage(); + for (const i of items) i.used = used.get(i.url) || 0; + res.render('media', { + groups: [ + { type: 'video', name: 'Videos', items: items.filter(i => i.type === 'video') }, + { type: 'audio', name: 'Sounds', items: items.filter(i => i.type === 'audio') }, + { type: 'image', name: 'Stickers & art', items: items.filter(i => i.type === 'image') }, + ], + }); }); -router.post('/upload', upload.single('file'), (req, res) => { - if (!req.file) return res.redirect('/media'); - db.createMedia({ - filename: req.file.filename, - original_name: req.file.originalname, - media_type: req.file.mimetype.startsWith('video') ? 'video' : 'audio', - size_bytes: req.file.size, +// several files at once; answers JSON for the page's upload (progress bar), redirects for a plain form post +router.post('/upload', (req, res) => { + upload.array('files', 20)(req, res, err => { + const json = (req.get('Accept') || '').includes('application/json'); + if (err) { + const msg = err.code === 'LIMIT_FILE_SIZE' ? 'File too big (max 50 MB)' : err.message; + return json ? res.status(400).json({ ok: false, error: msg }) : res.status(400).send(msg); + } + const files = req.files || []; + for (const f of files) { + db.createMedia({ filename: f.filename, original_name: f.originalname, media_type: library.typeOf(f.mimetype, f.originalname), size_bytes: f.size }); + console.log(`[media] Uploaded: ${f.originalname} (${f.size} bytes)`); + } + if (json) return res.json({ ok: true, saved: files.length, skipped: Math.max(0, (req.headers['x-file-count'] | 0) - files.length) }); + res.redirect('/media'); }); - console.log(`[media] Uploaded: ${req.file.originalname} (${req.file.size} bytes)`); - res.redirect('/media'); }); router.post('/:id/delete', (req, res) => { diff --git a/src/routers/redeems.js b/src/routers/redeems.js index 43ee624..c213962 100644 --- a/src/routers/redeems.js +++ b/src/routers/redeems.js @@ -1,32 +1,15 @@ const express = require('express'); -const fs = require('fs'); -const path = require('path'); const router = express.Router(); const db = require('../db'); const twitch = require('../twitch'); const redeems = require('../redeems'); -const PUBLIC = path.join(__dirname, '../../public'); -const listDir = (dir, re) => { try { return fs.readdirSync(path.join(PUBLIC, dir)).filter(f => re.test(f)).sort(); } catch { return []; } }; -function soundOptions() { - const re = /\.(mp3|wav|ogg|m4a)$/i; - const lib = [ - ...listDir('alerts/sounds/redeems', re).map(f => ({ url: `/alerts/sounds/redeems/${f}`, label: `${f} (normalized)` })), - ...listDir('alerts/sounds', re).map(f => ({ url: `/alerts/sounds/${f}`, label: f })), - ]; - const uploads = db.getAllMedia().filter(m => m.media_type === 'audio') - .map(m => ({ url: `/media/${m.filename}`, label: `${m.original_name} (uploaded)` })); - return [...lib, ...uploads]; -} - -// videos for 'Video' variations: files in public/alerts/videos + videos uploaded on the Media page -function videoOptions() { - const lib = listDir('alerts/videos', /\.(webm|mp4|mov)$/i).map(f => ({ url: `/alerts/videos/${f}`, label: f })); - const uploads = db.getAllMedia().filter(m => m.media_type === 'video') - .map(m => ({ url: `/media/${m.filename}`, label: `${m.original_name} (uploaded)` })); - return [...lib, ...uploads]; -} +// pickers: the same files the Media page lists (src/library.js) +const library = require('../library'); +const soundOptions = () => library.ofType('audio').map(i => ({ url: i.url, label: i.label, note: i.source === 'upload' ? 'uploaded' : i.note })); +const videoOptions = () => library.ofType('video').map(i => ({ url: i.url, label: i.label, note: i.source === 'upload' ? 'uploaded' : null })); +const stickerOptions = () => library.ofType('image').map(i => ({ url: i.url, label: i.label, note: i.source === 'upload' ? 'uploaded' : null })); // live list of the channel's Custom Power-ups (bits); null when Twitch is not reachable let loggedPowerUps = false; @@ -98,10 +81,7 @@ router.get('/', async (req, res) => { sounds: soundOptions(), videos: videoOptions(), ttsVoices: db.getAllTtsVoices(), - stickers: [ - ...listDir('alerts/stickers', /\.(png|webp|gif)$/i).map(f => ({ url: `/alerts/stickers/${f}`, label: f.replace(/\.\w+$/, '') })), - ...listDir('alerts/art', /\.(png|webp|gif)$/i).map(f => ({ url: `/alerts/art/${f}`, label: f.replace('_Contrast', '').replace(/\.\w+$/, '') })), - ], + stickers: stickerOptions(), }); }); diff --git a/views/media.ejs b/views/media.ejs index 40bb214..05c0788 100644 --- a/views/media.ejs +++ b/views/media.ejs @@ -1,53 +1,124 @@ + +

Media library

-

Upload audio (MP3, WAV, OGG) or video (MP4, WebM) files to use in actions. Max 50 MB.

-
-
- - +
+ Drop files here, or click to choose + sounds, videos or sticker images · several at once + +
+ + +
+ + + built-in uploaded used by N ⓘ + +
+ +<% for (const g of groups) { %> +

<%= g.name %> <%= g.items.length %>

+ <% if (!g.items.length) { %>

<%= g.type === 'video' ? 'No videos yet: drop some above.' : 'Nothing here yet.' %>

<% } %> +
+ <% for (const i of g.items) { %> +
+
+ <% if (i.type === 'image') { %> + <% } else if (i.type === 'video') { %> + <% } else { %>🔊<% } %> +
+
<%= i.label %>
+ <% if (i.type === 'audio') { %><% } %> +
+ <%= i.source === 'upload' ? 'uploaded' : 'built-in' %> + <% if (i.note) { %><%= i.note %><% } %> + <% if (i.used) { %>used by <%= i.used %><% } %> + <%= i.size > 1048576 ? (i.size / 1048576).toFixed(1) + ' MB' : Math.max(1, Math.round(i.size / 1024)) + ' KB' %> + <% if (i.source === 'upload') { %> + + + + <% } %> +
+
+ <% } %>
- - -<% if (files.length === 0) { %> -

No files uploaded yet.

-<% } else { %> -
- - - - - - - - - - - - - <% for (const f of files) { %> - - - - - - - - - <% } %> - -
NameTypeSizeUploadedPreview
<%= f.original_name %><%= f.media_type %><%= (f.size_bytes / 1024).toFixed(0) %> KB<%= f.uploaded_at %> - <% if (f.media_type === 'audio') { %> - - <% } else { %> - - <% } %> - -
- -
-
-
<% } %> + + diff --git a/views/redeems.ejs b/views/redeems.ejs index de23644..be88ffd 100644 --- a/views/redeems.ejs +++ b/views/redeems.ejs @@ -50,6 +50,25 @@ .thumb { width:34px; height:34px; object-fit:contain; } .vol-out { font-size:.75rem; color:var(--pico-muted-color); width:2.6em; } .preview { margin-top:.5rem; max-width:320px; border-radius:8px; background:#0006; } + /* pickers: a pill with the current choice (click to change, x to clear) or "+ …"; typing filters */ + .pick { position:relative; display:inline-flex; align-items:center; } + .chip { display:inline-flex; align-items:center; gap:.35rem; padding:.18rem .3rem .18rem .6rem; border-radius:99px; font-size:.82rem; + background:rgba(1,114,173,.18); border:1px solid rgba(1,114,173,.55); cursor:pointer; max-width:340px; } + .chip .lbl { overflow:hidden; text-overflow:ellipsis; white-space:nowrap; } + .chip img { width:22px; height:22px; object-fit:contain; } + .chip .x { border:0; background:none; color:inherit; padding:0 .3rem; margin:0; width:auto; font-size:.95rem; line-height:1; opacity:.7; cursor:pointer; } + .chip .x:hover { opacity:1; } + .chip .tag { font-size:.65rem; opacity:.7; } + .chip.add { background:none; border-style:dashed; color:var(--pico-muted-color); padding-right:.6rem; } + .pick.disabled .chip { opacity:.45; } + .pick input.q { margin:0; padding:.25rem .6rem; font-size:.82rem; height:auto; width:260px; border-radius:99px; } + .pick ul.opts { position:absolute; top:100%; left:0; z-index:30; list-style:none; margin:.2rem 0 0; padding:.25rem; min-width:260px; max-height:260px; overflow:auto; + background:var(--pico-card-background-color); border:1px solid var(--pico-muted-border-color); border-radius:10px; box-shadow:0 8px 20px rgba(0,0,0,.4); } + .pick ul.opts li { list-style:none; padding:.25rem .5rem; border-radius:6px; font-size:.82rem; cursor:pointer; display:flex; align-items:center; gap:.4rem; } + .pick ul.opts li img { width:22px; height:22px; object-fit:contain; } + .pick ul.opts li .tag { margin-left:auto; font-size:.68rem; color:var(--pico-muted-color); } + .pick ul.opts li.on { background:rgba(1,114,173,.3); } + .pick ul.opts li.none { color:var(--pico-muted-color); cursor:default; } [data-show] { display:none !important; } .vcard[data-display="cassette"] [data-show~="cassette"], .vcard[data-display="sound"] [data-show~="sound"], @@ -121,31 +140,22 @@
- + +
- - + + - + +
- + + <% if (!videos.length) { %>upload a video on Media<% } %>
@@ -166,6 +176,15 @@