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

render.js 1.7KB

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