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

index.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. fs = require("fs");;
  15. // Settings
  16. var serverSettings = require("../lib/settings/");
  17. var app = express();
  18. app.use(compression());
  19. app.use(logger.morgan());
  20. // Options for where to store uploaded audio and max size
  21. var fileOptions = {
  22. storage: multer.diskStorage({
  23. destination: function(req, file, cb) {
  24. var dir = path.join(serverSettings.workingDirectory, uuid.v1());
  25. mkdirp(dir, function(err) {
  26. return cb(err, dir);
  27. });
  28. },
  29. filename: function(req, file, cb) {
  30. cb(null, "audio");
  31. }
  32. })
  33. };
  34. var newThemeFileOptions = {
  35. storage: multer.diskStorage({
  36. destination: function(req, file, cb) {
  37. var dir = path.join(serverSettings.themeStoragePath);
  38. mkdirp(dir, function(err) {
  39. return cb(err, dir);
  40. });
  41. },
  42. filename: function(req, file, cb) {
  43. cb(null, file.originalname);
  44. }
  45. })
  46. };
  47. if (serverSettings.maxUploadSize) {
  48. fileOptions.limits = {
  49. fileSize: +serverSettings.maxUploadSize
  50. };
  51. newThemeFileOptions.limits = {
  52. fileSize: +serverSettings.maxUploadSize
  53. };
  54. }
  55. // On submission, check upload, validate input, and start generating a video
  56. app.post("/submit/", [multer(fileOptions).single("audio"), render.validate, render.route]);
  57. // Upload new theme
  58. app.post("/theme/upload/", [multer(newThemeFileOptions).single("newTheme"), function (req, res) {
  59. var themesFile = path.join(serverSettings.settingsPath, "themes.json");
  60. fs.readFile(themesFile, "utf8", function readFileCallback(err, data) {
  61. if (err) {
  62. console.log('err', err);
  63. return null;
  64. } else {
  65. var caption = req.body.newCaption;
  66. var themes = JSON.parse(data);
  67. themes[caption] = {
  68. "backgroundImage": req.file.filename
  69. };
  70. var jt = JSON.stringify(themes);
  71. fs.writeFile(themesFile, jt, "utf8", function (err) {
  72. if (err) {
  73. console.log(err);
  74. return null;
  75. }
  76. });
  77. }
  78. });
  79. res.end();
  80. }]);
  81. // If not using S3, serve videos locally
  82. if (!serverSettings.s3Bucket) {
  83. app.use("/video/", express.static(path.join(serverSettings.storagePath, "video")));
  84. }
  85. // Serve custom fonts
  86. app.get("/fonts/fonts.css", fonts.css);
  87. app.get("/fonts/fonts.js", fonts.js);
  88. if (serverSettings.fonts) {
  89. app.get("/fonts/:font", fonts.font);
  90. }
  91. // Check the status of a current video
  92. app.get("/status/:id/", status);
  93. // Serve background images and themes JSON statically
  94. app.use("/settings/", function(req, res, next) {
  95. // Limit to themes.json and bg images
  96. if (req.url.match(/^\/?themes.json$/i) || req.url.match(/^\/?backgrounds\/[^/]+$/i)) {
  97. return next();
  98. }
  99. return res.status(404).send("Cannot GET " + path.join("/settings", req.url));
  100. }, express.static(path.join(__dirname, "..", "settings")));
  101. // Serve editor files statically
  102. app.use(express.static(path.join(__dirname, "..", "editor")));
  103. app.use(errorHandlers);
  104. module.exports = app;