Turn audio into a shareable video. forked from nypublicradio/audiogram

render.js 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var serverSettings = require("../lib/settings/"),
  2. spawn = require("child_process").spawn,
  3. path = require("path"),
  4. _ = require("underscore"),
  5. logger = require("../lib/logger"),
  6. transports = require("../lib/transports");
  7. function validate(req, res, next) {
  8. try {
  9. req.body.theme = JSON.parse(req.body.theme);
  10. } catch(e) {
  11. return res.status(500).send("Unknown settings error.");
  12. }
  13. var file;
  14. if (req.files) {
  15. if (req.files['audio']) {
  16. file = req.files['audio'][0];
  17. }
  18. } else {
  19. if (req.file) {
  20. file = req.file;
  21. }
  22. }
  23. if (!file) {
  24. return res.status(500).send("No valid audio received.");
  25. }
  26. // Start at the beginning, or specified time
  27. if (req.body.start) {
  28. req.body.start = +req.body.start;
  29. }
  30. if (req.body.end) {
  31. req.body.end = +req.body.end;
  32. }
  33. return next();
  34. }
  35. function route(req, res) {
  36. var file;
  37. if (req.files) {
  38. if (req.files['audio']) {
  39. file = req.files['audio'][0];
  40. }
  41. } else {
  42. if (req.file) {
  43. file = req.file;
  44. }
  45. }
  46. if (!file) {
  47. return res.status(500).send("No valid audio received.");
  48. }
  49. var id = file.destination.split(path.sep).pop();
  50. transports.uploadAudio(path.join(file.destination, "audio"), "audio/" + id,function(err) {
  51. if (err) {
  52. throw err;
  53. }
  54. // Queue up the job with a timestamp
  55. transports.addJob(_.extend({ id: id, created: (new Date()).getTime() }, req.body));
  56. res.json({ id: id });
  57. // If there's no separate worker, spawn one right away
  58. if (!serverSettings.worker) {
  59. logger.debug("Spawning worker");
  60. // Empty args to avoid child_process Linux error
  61. spawn("bin/worker", [], {
  62. stdio: "inherit",
  63. cwd: path.join(__dirname, ".."),
  64. env: _.extend({}, process.env, { SPAWNED: true })
  65. });
  66. }
  67. });
  68. };
  69. module.exports = {
  70. validate: validate,
  71. route: route
  72. };