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

index.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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("../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. if (typeof serverSettings.workingDirectory !== "string") {
  38. throw new TypeError("No workingDirectory set in settings/index.js");
  39. }
  40. // On submission, check upload, validate input, and start generating a video
  41. app.post("/submit/", [multer(fileOptions).single("audio"), render.validate, render.route]);
  42. // If not using S3, serve videos locally
  43. if (!serverSettings.s3Bucket) {
  44. if (typeof serverSettings.storagePath !== "string") {
  45. throw new TypeError("No storagePath set in settings/index.js");
  46. }
  47. var storagePath = path.isAbsolute(serverSettings.storagePath) ? serverSettings.storagePath : path.join(__dirname, "..", serverSettings.storagePath);
  48. app.use("/video/", express.static(path.join(storagePath, "video")));
  49. }
  50. // Check the status of a current video
  51. app.get("/status/:id/", status);
  52. // Serve background images and everything else statically
  53. app.use("/img/", express.static(path.join(__dirname, "..", "settings", "backgrounds")));
  54. app.use(express.static(path.join(__dirname, "..", "editor")));
  55. app.use(errorHandlers);
  56. module.exports = app;