Fix muxing

This commit is contained in:
2026-05-15 04:10:06 +02:00
parent 05faf1ce3b
commit 6deb883472
26 changed files with 662 additions and 169 deletions

View File

@@ -65,23 +65,69 @@ router.get('/', (_req, res) => {
.sort((a, b) => b.filePath.localeCompare(a.filePath));
res.json({ files });
});
router.post('/youtube', (req, res) => {
router.delete('/:filename', (req, res) => {
const raw = req.params.filename;
const requested = Array.isArray(raw) ? raw[0] : raw;
if (!requested) {
res.status(400).json({ error: 'filename is required' });
return;
}
const resolved = path_1.default.resolve(UPLOADS_DIR, requested);
const uploadsWithSep = UPLOADS_DIR.endsWith(path_1.default.sep) ? UPLOADS_DIR : UPLOADS_DIR + path_1.default.sep;
if (!resolved.startsWith(uploadsWithSep)) {
res.status(400).json({ error: 'Invalid filename' });
return;
}
if (!fs_1.default.existsSync(resolved)) {
res.status(404).json({ error: 'File not found' });
return;
}
try {
fs_1.default.unlinkSync(resolved);
res.json({ ok: true });
}
catch (err) {
res.status(500).json({ error: `Failed to delete: ${err.message}` });
}
});
// Stream yt-dlp download progress over SSE.
// Returns events: {type:'progress', percent} ... {type:'done', filePath, filename, title}
// or {type:'error', message}
router.get('/youtube/stream', (req, res) => {
const url = req.query.url || '';
if (!url) {
res.status(400).json({ error: 'url query param is required' });
return;
}
if (!(0, ytDlp_1.isYtDlpAvailable)()) {
res.status(400).json({ error: 'yt-dlp is not installed or not in PATH' });
return;
}
const { url } = req.body;
if (!url) {
res.status(400).json({ error: 'URL is required' });
return;
}
try {
const result = (0, ytDlp_1.downloadVideo)(url, UPLOADS_DIR);
res.json(result);
}
catch (err) {
res.status(500).json({ error: `Failed to download: ${err.message}` });
}
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
res.setHeader('X-Accel-Buffering', 'no');
res.flushHeaders?.();
const send = (data) => {
res.write(`data: ${JSON.stringify(data)}\n\n`);
};
let clientGone = false;
req.on('close', () => { clientGone = true; });
(0, ytDlp_1.downloadVideo)(url, UPLOADS_DIR, (percent) => {
if (clientGone)
return;
send({ type: 'progress', percent });
}).then((result) => {
if (clientGone)
return;
send({ type: 'done', ...result });
res.end();
}).catch((err) => {
if (clientGone)
return;
send({ type: 'error', message: err.message });
res.end();
});
});
exports.default = router;
//# sourceMappingURL=files.js.map