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

index.js 3.4KB

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