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

validate-settings.js 2.9KB

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