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

index.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Dependencies
  2. var express = require("express"),
  3. compression = require("compression"),
  4. path = require("path"),
  5. multer = require("multer"),
  6. uuid = require("node-uuid"),
  7. mkdirp = require("mkdirp");
  8. // Routes and middleware
  9. var logger = require("../lib/logger/"),
  10. render = require("./render.js"),
  11. status = require("./status.js"),
  12. errorHandlers = require("./error.js");
  13. // Settings
  14. var serverSettings = require("../lib/settings/");
  15. var app = express();
  16. app.use(compression());
  17. app.use(logger.morgan());
  18. // Options for where to store uploaded audio and max size
  19. var fileOptions = {
  20. storage: multer.diskStorage({
  21. destination: function(req, file, cb) {
  22. var dir = path.join(serverSettings.workingDirectory, uuid.v1());
  23. mkdirp(dir, function(err) {
  24. return cb(err, dir);
  25. });
  26. },
  27. filename: function(req, file, cb) {
  28. cb(null, "audio");
  29. }
  30. })
  31. };
  32. if (serverSettings.maxUploadSize) {
  33. fileOptions.limits = {
  34. fileSize: +serverSettings.maxUploadSize
  35. };
  36. }
  37. // On submission, check upload, validate input, and start generating a video
  38. app.post("/submit/", [multer(fileOptions).single("audio"), render.validate, render.route]);
  39. // If not using S3, serve videos locally
  40. if (!serverSettings.s3Bucket) {
  41. app.use("/video/", express.static(path.join(serverSettings.storagePath, "video")));
  42. }
  43. // Check the status of a current video
  44. app.get("/status/:id/", status);
  45. // Serve background images and themes JSON statically
  46. app.use("/settings/", function(req, res, next) {
  47. // Limit to themes.json and bg images
  48. if (req.url.match(/^\/?themes.json$/i) || req.url.match(/^\/?backgrounds\/[^/]+$/i)) {
  49. return next();
  50. }
  51. return res.status(404).send("Cannot GET " + path.join("/settings", req.url));
  52. }, express.static(path.join(__dirname, "..", "settings")));
  53. // Serve editor files statically
  54. app.use(express.static(path.join(__dirname, "..", "editor")));
  55. app.use(errorHandlers);
  56. module.exports = app;