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