index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var express = require('express');
  2. var router = express.Router();
  3. var bodyParser = require('body-parser');
  4. var expressValidator = require('express-validator');
  5. router.use(bodyParser.urlencoded({extended: true}));
  6. router.use(bodyParser.json());
  7. router.use(expressValidator());
  8. var child_process = require('child_process');
  9. /* GET home page. */
  10. router.get('/', function(req, res, next) {
  11. res.render('index', { title: 'LAN Jukebox' });
  12. });
  13. router.get('/youtube', function(req, res, next) {
  14. res.render('youtube', { title: 'LAN Jukebox - Add a Youtube Video' });
  15. });
  16. router.use(expressValidator({
  17. customValidators: {
  18. dlSuccess: function(video) {
  19. // Test if the requested video is available.
  20. var youtube_dl = child_process.spawnSync('/usr/bin/youtube-dl', ['-s', video]);
  21. console.log(youtube_dl.stderr.toString());
  22. if (youtube_dl.stderr.toString()) {
  23. return false;
  24. }
  25. return true;
  26. }
  27. }
  28. }
  29. ));
  30. router.post('/youtube', function(req, res) {
  31. var video = req.body.video;
  32. req.checkBody('video', 'URL is not valid.').dlSuccess();
  33. var errors = req.validationErrors();
  34. if(errors){
  35. res.render('youtube', {
  36. title: 'LAN Jukebox - Add a Youtube Video',
  37. errors:errors
  38. });
  39. } else {
  40. var youtube_dl = child_process.spawn('/usr/bin/youtube-dl', ['-x', '-o', 'downloads/%(title)s.%(ext)s', video]);
  41. // TODO eval video
  42. res.render('index', { title: 'LAN Jukebox' });
  43. }
  44. });
  45. router.get('/file', function(req, res, next) {
  46. res.render('file', { title: 'LAN Jukebox - Add a Music File' });
  47. });
  48. module.exports = router;