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:
parent
8ac8fe024c
commit
a32a55c264
6 changed files with 313 additions and 109 deletions
|
|
@ -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());
|
||||
|
||||
|
|
|
|||
58
src/library.js
Normal file
58
src/library.js
Normal file
|
|
@ -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 };
|
||||
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue