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

index.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // Healthcheck
  53. app.get("/healthcheck/", function(req, res, next) {
  54. return res.status(200).send('I\'m a happy healthcheck.');
  55. });
  56. // Serve background images and themes JSON statically
  57. app.use("/settings/", function(req, res, next) {
  58. // Limit to themes.json and bg images
  59. if (req.url.match(/^\/?themes.json$|\/?labels.json$/i) || req.url.match(/^\/?backgrounds\/[^/]+$/i)) {
  60. return next();
  61. }
  62. return res.status(404).send("Cannot GET " + path.join("/settings", req.url));
  63. }, express.static(path.join(__dirname, "..", "settings")));
  64. // Serve editor files statically
  65. app.use(express.static(path.join(__dirname, "..", "editor")));
  66. app.use(errorHandlers);
  67. module.exports = app;