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

index.js 2.1KB

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