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

render.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. if (!req.file || !req.file.filename) {
  14. return res.status(500).send("No valid audio received.");
  15. }
  16. // Start at the beginning, or specified time
  17. if (req.body.start) {
  18. req.body.start = +req.body.start;
  19. }
  20. if (req.body.end) {
  21. req.body.end = +req.body.end;
  22. }
  23. return next();
  24. }
  25. function route(req, res) {
  26. var id = req.file.destination.split(path.sep).pop();
  27. transports.uploadAudio(path.join(req.file.destination, "audio"), "audio/" + id,function(err) {
  28. if (err) {
  29. throw err;
  30. }
  31. // Queue up the job with a timestamp
  32. transports.addJob(_.extend({ id: id, created: (new Date()).getTime() }, req.body));
  33. res.json({ id: id });
  34. // If there's no separate worker, spawn one right away
  35. if (!serverSettings.worker) {
  36. logger.debug("Spawning worker");
  37. // Empty args to avoid child_process Linux error
  38. spawn("bin/worker", [], {
  39. stdio: "inherit",
  40. cwd: path.join(__dirname, ".."),
  41. env: _.extend({}, process.env, { SPAWNED: true })
  42. });
  43. }
  44. });
  45. };
  46. module.exports = {
  47. validate: validate,
  48. route: route
  49. };