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
|
|
@ -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