-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
330 lines (272 loc) · 9.29 KB
/
index.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
const fs = require('fs');
const path = require('path');
const express = require('express');
const serveStatic = require('serve-static');
const passport = require('passport');
const Strategy = require('passport-local').Strategy;
const ensureLogin = require('connect-ensure-login').ensureLoggedIn;
const multer = require('multer')();
const sharp = require('sharp');
const users = require('./lib/users.js');
const builder = require('./lib/builder.js');
const utils = require('./lib/utils.js');
const mailer = require('./lib/mailer.js');
// Build the static site first
builder.build();
var config = builder.getConfig();
// Configure the local strategy for Passport
passport.use(new Strategy(function(username, password, cb) {
users.findByUsername(username, function(err, user) {
if (err) { return cb(err); }
if (!user) { return cb(null, false); }
if (!users.validate(user, password)) { return cb(null, false); }
return cb(null, user);
});
}));
passport.serializeUser(function(user, cb) {
cb(null, user.id);
});
passport.deserializeUser(function(id, cb) {
users.findById(id, function (err, user) {
if (err) { return cb(err); }
cb(null, user);
});
});
// Express app setup
var app = express();
app.set('views', `themes/${config.adminTheme}/views`);
app.set('view engine', 'ejs');
app.use(serveStatic(builder.dirs.out, {
setHeaders: (res, reqpath) => {
if (utils.isTextFile(reqpath)) {
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Disposition', `inline; filename="${path.parse(reqpath).base}"`);
}
}
}));
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(require('express-session')({
secret: process.env['SMOLPRESS_SESSION_SECRET'] || utils.randomString(16),
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
// Routes
app.get('/admin', ensureLogin(), (req, res) => {
res.redirect('/content');
})
app.get('/content', ensureLogin(), (req, res) => {
res.render('content', { user: req.user, pages: builder.getPages(), config: builder.getConfig() });
});
app.get('/login', (req, res) => {
if (users.empty())
res.redirect('signup');
else
res.render('login', {config: builder.getConfig()});
});
app.get('/signup', (req, res) => {
if (users.empty())
res.render('signup', {config: builder.getConfig()});
else
res.redirect('/');
});
app.post('/signup', (req, res) => {
if (req.body.password == req.body.passconfirm) {
if (req.body.username.length == 0) {
res.send("Username cannot be blank");
}
else {
users.create(req.body.username, req.body.password);
res.redirect('/login');
}
}
else {
res.send("Passwords don't match. Hit back and try again");
}
});
app.post('/login',
passport.authenticate('local', { successReturnToOrRedirect: '/', failureRedirect: '/login' }),
(req, res) => { res.redirect('/content'); }
);
app.get('/logout', (req, res) => {
req.logout(() => {
res.redirect('/');
});
});
app.get('/settings', ensureLogin(), (req, res) => {
res.render('settings', Object.assign({user: req.user}, {config: builder.getConfig()}));
});
app.post('/settings', ensureLogin(), (req, res) => {
var status = builder.setConfig(req.body);
builder.build();
config = builder.getConfig();
app.set('views', `themes/${config.adminTheme}/views`);
var viewData = Object.assign({user: req.user}, {config: builder.getConfig()});
viewData.message = status;
res.render('settings', viewData);
});
app.post('/posts', ensureLogin(), (req, res) => {
var slug = utils.slugify(req.body.title);
var filename = `${builder.dirs.src}/pages/${slug}.md`;
var draft = '';
if (req.body.draft) {
draft = 'draft: true\n';
}
var metadata = `---\ntitle: ${req.body.title}\n${draft}---\n# ${req.body.title}`;
fs.writeFileSync(filename, metadata);
builder.buildPage(`${slug}.md`);
builder.buildIndex();
res.redirect("/" + slug + "/edit");
});
app.get('/:slug/edit', ensureLogin(), (req, res) => {
var filename = `${builder.dirs.src}/pages/${req.params.slug}.md`;
if (fs.existsSync(filename)) {
res.render('editor', {
config: builder.getConfig(),
content: fs.readFileSync(filename, 'utf8'),
saveurl: `/${req.params.slug}/save`
});
}
else {
res.redirect("/" + req.params.slug);
}
});
app.get('/:slug/delete', ensureLogin(), (req, res) => {
var filename = `${builder.dirs.src}/pages/${req.params.slug}.md`;
if (fs.existsSync(filename)) {
fs.unlinkSync(filename);
}
var datadir = `${builder.dirs.data}/${req.params.slug}`;
if (fs.existsSync(datadir)) {
fs.rmSync(datadir, {recursive: true});
}
builder.build();
res.redirect("/content");
});
app.post('/:slug/save', ensureLogin(), (req, res) => {
var filename = `${builder.dirs.src}/pages/${req.params.slug}.md`;
fs.writeFileSync(filename, req.body.content);
var page = builder.buildPage(`${req.params.slug}.md`);
builder.buildIndex();
if (page.draft) {
res.redirect("/" + req.params.slug + "/edit");
}
else {
res.redirect("/" + req.params.slug);
}
})
app.post('/:slug/comments', (req, res) => {
var filename = `${builder.dirs.data}/${req.params.slug}/comments.json`;
var comments = [];
if (fs.existsSync(filename)) {
comments = JSON.parse(fs.readFileSync(filename));
}
else {
fs.mkdirSync(`${builder.dirs.data}/${req.params.slug}`);
fs.writeFileSync(filename, "[]");
}
if (req.body.name && req.body.text &&
comments.length < builder.getConfig().maxComments &&
req.body.text.length >= 2 && req.body.name.length <= 256 &&
req.body.text.length <= builder.getConfig().maxCommentLength) {
const pages = builder.getPages();
const page = pages.find(el => el.name.indexOf(req.params.slug) == 0)
if (page && page.comments == false) {
return res.redirect("/" + req.params.slug);
}
const comment = {name: req.body.name, text: req.body.text, id: Date.now(), slug: req.params.slug};
comments.push(comment);
fs.writeFileSync(filename, JSON.stringify(comments));
config = builder.getConfig();
if (config.emailNotifications) {
mailer.mail(comment, page, config);
}
builder.buildPage(`${req.params.slug}.md`);
}
res.redirect("/" + req.params.slug + "#comments");
})
app.get('/media', ensureLogin(), (req, res) => {
var images = [];
fs.readdirSync(`${builder.dirs.src}/media/`).forEach(file=> {
images.push({name: file, url: `/media/${file}`})
})
res.render('media', { user: req.user, images, config: builder.getConfig() });
})
app.post('/cleanup', ensureLogin(), (req, res) => {
Object.keys(req.body).forEach(file => {
if (fs.existsSync(`${builder.dirs.src}/media/` + file)) {
fs.unlinkSync(`${builder.dirs.src}/media/` + file)
}
})
res.redirect('/media');
})
app.get('/comments', ensureLogin(), (req, res) => {
var comments = [];
fs.readdirSync(`${builder.dirs.data}`).forEach(entry => {
if (fs.lstatSync(`${builder.dirs.data}/` + entry).isDirectory()) {
const commentsFilePath = `${builder.dirs.data}/` + entry + '/comments.json';
if (fs.existsSync(commentsFilePath)) {
JSON.parse(fs.readFileSync(commentsFilePath)).forEach(c => comments.push(c))
comments.sort((a, b) => (b.id - a.id))
}
}
})
res.render('comments', { user: req.user, comments, config: builder.getConfig() })
})
app.get('/moderate/:slug/:cid', ensureLogin(), (req, res) => {
var filename = `${builder.dirs.data}/${req.params.slug}/comments.json`;
var comments = [];
if (fs.existsSync(filename)) {
comments = JSON.parse(fs.readFileSync(filename));
}
else {
res.redirect('/comments');
}
fs.writeFileSync(filename, JSON.stringify(comments.filter(c => c.id != req.params.cid)));
builder.buildPage(`${req.params.slug}.md`);
res.redirect('/comments');
});
app.get('/approve/:slug/:cid', ensureLogin(), (req, res) => {
var filename = `${builder.dirs.data}/${req.params.slug}/comments.json`;
var comments = [];
if (fs.existsSync(filename)) {
comments = JSON.parse(fs.readFileSync(filename));
}
else {
res.redirect('/comments');
}
for (const c of comments) {
if (c.id == req.params.cid)
c.approved = true;
}
fs.writeFileSync(filename, JSON.stringify(comments));
builder.buildPage(`${req.params.slug}.md`);
res.redirect('/comments');
});
app.post('/upload', ensureLogin(), multer.single('image'), function (req, res) {
var filename = req.body.rename ? req.body.rename : req.file.originalname;
var imagePath = `${builder.dirs.src}/media/${filename}`;
var w = req.body.width ? parseInt(req.body.width) : null;
var h = req.body.height ? parseInt(req.body.height) : null;
if (req.body.resize) {
sharp(req.file.buffer).rotate().resize(w, h, {
fit: sharp.fit.inside,
withoutEnlargement: true
}).toFile(imagePath, (err, info) => {
fs.copyFile(imagePath, `${builder.dirs.out}/media/${filename}`, (err) => {
if (err) throw err;
});
res.redirect('/media');
});
} else {
fs.writeFileSync(imagePath, req.file.buffer);
fs.copyFile(imagePath, `${builder.dirs.out}/media/${filename}`, (err) => {
if (err) throw err;
});
res.redirect('/media');
}
});
var listener = app.listen(process.env['SMOLPRESS_PORT'] || 3939, () => {
console.log(`Server started on port ${listener.address().port}`)
});