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) => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue