Procházet zdrojové kódy

Fallback to default template if none can be found

Brendan Abolivier před 7 roky
rodič
revize
6d640f2d19
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
1 změnil soubory, kde provedl 48 přidání a 15 odebrání
  1. 48
    15
      server.js

+ 48
- 15
server.js Zobrazit soubor

@@ -1,6 +1,7 @@
1 1
 var pug			= require('pug');
2 2
 var nodemailer  = require('nodemailer');
3 3
 var crypto		= require('crypto');
4
+var fs		= require('fs');
4 5
 var settings	= require('./settings');
5 6
 
6 7
 // Translation
@@ -28,6 +29,28 @@ var transporter = nodemailer.createTransport(settings.mailserver);
28 29
 // Verification tokens
29 30
 var tokens = {};
30 31
 
32
+// Default template
33
+// JavaScript has no native way to handle multi-line strings, so we put our template
34
+// in a comment inside a function fro which we generate a string.
35
+// cf: https://tomasz.janczuk.org/2013/05/multi-line-strings-in-javascript-and.html
36
+var defaultTemplate = (function() {/*
37
+html
38
+	body
39
+		p.subj
40
+			span(style="font-weight:bold") Subject:&nbsp;
41
+			span= subject
42
+		p.from
43
+			span(style="font-weight:bold") Sent from:&nbsp;
44
+			span= replyTo
45
+		each field in custom
46
+			p.custom
47
+				span(style="font-weight:bold")= field.label + ': '
48
+				span= field.value
49
+		p.message
50
+			span(style="font-weight:bold") Message:&nbsp;
51
+		
52
+		p= html
53
+*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
31 54
 
32 55
 // Serve static (JS + HTML) files
33 56
 app.use(express.static('front'));
@@ -104,23 +127,33 @@ app.post('/send', function(req, res, next) {
104 127
 	// Replacing the mail's content with HTML from the pug template
105 128
 	// Commenting the line below will bypass the generation and only user the
106 129
 	// text entered by the user
107
-	params.html = pug.renderFile('template.pug', params);
108
-
109
-	log.info(lang.log_sending, params.replyTo);
110
-
111
-	// Send the email to all users
112
-	sendMails(params, function(err, infos) {
130
+	fs.access('template.pug', function(err) {
131
+		// Checking if the template exists.
132
+		// If not, fallback to the default template.
133
+		// TODO: Parameterise the template file name.
113 134
 		if(err) {
114
-			log.error(err);
115
-		}
116
-		logStatus(infos);
117
-	}, function() {
118
-		if(status.failed === status.total) {
119
-			res.status(500).send();
135
+			params.html = pug.render(defaultTemplate, params);
120 136
 		} else {
121
-			res.status(200).send();
137
+			params.html = pug.renderFile('template.pug', params);
122 138
 		}
123
-	})
139
+
140
+		log.info(lang.log_sending, params.replyTo);
141
+
142
+		// Send the email to all users
143
+		sendMails(params, function(err, infos) {
144
+			if(err) {
145
+				log.error(err);
146
+			}
147
+			logStatus(infos);
148
+		}, function() {
149
+			if(status.failed === status.total) {
150
+				res.status(500).send();
151
+			} else {
152
+				res.status(200).send();
153
+			}
154
+		});
155
+	});
156
+
124 157
 });
125 158
 
126 159
 
@@ -334,4 +367,4 @@ function processCustom(custom) {
334 367
 	}
335 368
 
336 369
 	return fields;
337
-}
370
+}