heystreamer/src/library.js
Bashy a32a55c264 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>
2026-11-22 16:24:18 +02:00

58 lines
2.5 KiB
JavaScript

// 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 };