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

validate-settings.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. var fs = require("fs"),
  2. path = require("path"),
  3. mkdirp = require("mkdirp"),
  4. _ = require("underscore");
  5. module.exports = function(settings) {
  6. // Check paths
  7. if (typeof settings.workingDirectory !== "string") {
  8. throw new Error("settings.workingDirectory is required. See https://github.com/nypublicradio/audiogram/blob/master/SERVER.md#all-settings for details.");
  9. }
  10. if (typeof settings.storagePath !== "string" && typeof settings.s3Bucket !== "string") {
  11. throw new Error("settings.storagePath or settings.s3Bucket is required. See https://github.com/nypublicradio/audiogram/blob/master/SERVER.md#all-settings for details.");
  12. }
  13. // TODO normalize workingDirectory, s3Bucket, and storagePath
  14. settings.workingDirectory = normalize(settings.workingDirectory);
  15. tryToCreate(settings.workingDirectory, "workingDirectory");
  16. if (typeof settings.s3Bucket === "string") {
  17. // Remove s3:// and trailing slash, bucket name only
  18. settings.s3Bucket = settings.s3Bucket.replace(/^(s3[:]\/\/)|\/$/g, "");
  19. if (typeof settings.storagePath === "string") {
  20. // No leading slash, one trailing slash
  21. settings.storagePath = settings.storagePath.replace(/^\/|\/$/g, "");
  22. if (settings.storagePath) {
  23. settings.storagePath += "/";
  24. }
  25. } else {
  26. settings.storagePath = "";
  27. }
  28. } else {
  29. settings.storagePath = normalize(settings.storagePath);
  30. tryToCreate(settings.storagePath, "storagePath");
  31. }
  32. // Check maxUploadSize
  33. if ("maxUploadSize" in settings && typeof settings.maxUploadSize !== "number") {
  34. throw new TypeError("settings.maxUploadSize must be an integer. See https://github.com/nypublicradio/audiogram/blob/master/SERVER.md#all-settings for details.");
  35. }
  36. // Check fonts
  37. if ("fonts" in settings) {
  38. if (!Array.isArray(settings.fonts)) {
  39. throw new TypeError("settings.fonts must be an array of fonts. See https://github.com/nypublicradio/audiogram/blob/master/THEMES.md#a-note-about-fonts for details.")
  40. }
  41. settings.fonts.forEach(function(font){
  42. if (!font || typeof font.family !== "string" || typeof font.file !== "string") {
  43. return console.warn("Custom font in settings.fonts missing a 'family' or 'file'. See https://github.com/nypublicradio/audiogram/blob/master/THEMES.md#a-note-about-fonts for details.");
  44. }
  45. font.file = normalize(font.file);
  46. try {
  47. fs.accessSync(font.file);
  48. } catch(e) {
  49. return console.warn("Font file " + font.file + " does not exist or is not readable.");
  50. }
  51. });
  52. }
  53. return settings;
  54. };
  55. function tryToCreate(dir, key) {
  56. try {
  57. mkdirp.sync(dir);
  58. fs.accessSync(dir);
  59. } catch(e) {
  60. throw new Error("Could not create/access settings." + key + " at " + dir + "");
  61. }
  62. }
  63. function normalize(p) {
  64. if (path.isAbsolute(p)) {
  65. return p;
  66. }
  67. return path.join(__dirname, "..", "..", p);
  68. }