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

index.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. 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. if (serverSettings.fonts) {
  47. app.use("/fonts/:font", fonts.font);
  48. }
  49. // Check the status of a current video
  50. app.get("/status/:id/", status);
  51. // Serve background images and themes JSON statically
  52. app.use("/settings/", function(req, res, next) {
  53. // Limit to themes.json and bg images
  54. if (req.url.match(/^\/?themes.json$/i) || req.url.match(/^\/?backgrounds\/[^/]+$/i)) {
  55. return next();
  56. }
  57. return res.status(404).send("Cannot GET " + path.join("/settings", req.url));
  58. }, express.static(path.join(__dirname, "..", "settings")));
  59. // Serve editor files statically
  60. app.use(express.static(path.join(__dirname, "..", "editor")));
  61. app.use(errorHandlers);
  62. module.exports = app;