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

index.js 2.5KB

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