-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.js
142 lines (114 loc) · 3.62 KB
/
publish.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
function publish(req, res) {
// Start by testing authentication
// TODO: if is not authenticated, redirect to /login
// Login page
if (req.path === '/login') {
// New Article form
} else if (req.path === '/new') {
res.render('layout', {
title: "New Article",
adj: "pages",
partials: {
header: "header",
main: "publish-new",
script: "publish-new-script"
}
});
// Publishing Center - process new articles and show existing articles
} else if (req.path === '/') {
var db = require('./dbauth.js')();
console.log('Database connection established.');
// Returns a callback containing the next unique id
function getNextSequence(name, callback) {
db.collection('counters').findAndModify({
query: { _id: name },
update: { $inc: { seq: 1 } },
new: true,
}, function(err, doc) {
console.log(doc);
if (err) {
console.log('Error: ', err);
callback(err);
return;
}
callback(doc.seq);
});
}
// Submit the new article to the database
if (req.method === 'POST' ) {
var data = '';
req.on('data', function(datum) {
data += datum;
});
// Article data received, now process
req.on('end', function() {
var ent = require('ent');
var sanitize = require('sanitize-html');
var S = require('string');
var querystring = require('querystring');
var input = querystring.parse(data);
/****************************************
* For now, a rudimentary way to prevent randos from posting on my blog.
* *************************************/
if (input["password"] != process.env.SENDGRID_KEY) {
res.writeHead(403, {"Content-Type" : "text/plain"});
res.end("Sorry, you don't have permission to publish.");
return;
}
// Strip bad HTML while keeping good HTML with tags
var safeArticle = ent.encode(sanitize(input["article"]));
// Calculate article reading time, where 1 min = 250 words
var wordCount = S(safeArticle).stripTags().s.split(" ").length;
var readTime = Math.round(wordCount / 250);
readTime = readTime > 1 ? readTime : 1; // readTime at least 1
readTime += readTime === 1 ? " minute" : " minutes";// minute vs minutes
var months = ["January", "February", "March", "April", "May", "June",
"July", "August","September", "October", "November", "December"];
var date = new Date();
var dateStr = months[date.getMonth()] + ' ' + date.getDate() + ', '
+ date.getFullYear();
// Insert the article after getting the unique id
getNextSequence("postid", function(seq) {
var newArticle = {
_id: seq,
title: input["title"],
article: safeArticle,
path: input["path"],
read: readTime,
date: dateStr,
visible: parseInt(input["visible"]) || 0
};
db.collection('articles').insert(newArticle, function(err, records) {
console.log("Article inserted into database: ", records);
if (err) {
console.log("Error: ", err);
}
});
});
});
}
// Blog control panel - Show existing articles
db.collection('articles').find(function(err, listOfArticles) {
if (err) {
console.log("Database Error: ", err);
res.writeHead(500, {"Content-Type" : "text/plain"});
res.end("500 error: " + err);
return;
}
res.render('layout', {
title: "Publishing Center",
adj: "pages",
articles: listOfArticles,
partials: {
header: "header",
main: "publish",
script: "publish-script"
}
});
});
} else {
res.writeHead(404, {"Content-Type" : "text/plain"});
res.end("404 error");
}
}
module.exports = publish