-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (57 loc) · 1.76 KB
/
Copy pathindex.js
File metadata and controls
63 lines (57 loc) · 1.76 KB
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
'use strict';
const API = require('ep_etherpad-lite/node/db/API.js');
const randomString = require('ep_etherpad-lite/static/js/pad_utils').randomString;
const MAX_BODY_SIZE = 1024 * 1024; // 1 MB
exports.registerRoute = (hookName, args, callback) => {
args.app.post('/post', (req, res) => {
let padId = req.headers['x-pad-id'];
if (padId === undefined) {
padId = randomString(8);
}
let content = '';
let aborted = false;
req.on('data', (data) => {
content += data;
if (content.length > MAX_BODY_SIZE) {
aborted = true;
res.status(413).send('Request body too large');
req.destroy();
}
});
req.on('end', async () => {
if (aborted) return;
let padExists = true;
try {
padExists = await API.getText(padId, 0);
if (padExists && padExists.text) {
padExists = true;
}
} catch (e) {
padExists = false;
}
// Pad does not exist so creating a new pad.
if (!padExists) {
try {
console.debug('ep_post_data: Creating new pad', padId);
await API.createPad(padId, content);
res.send('Success creating new pad');
} catch (e) {
console.error('ep_post_data: Error creating pad', padId, e);
res.send('Error creating pad');
}
}
// Pad already exists so updating an existing pad.
if (padExists) {
try {
console.debug('ep_post_data: Setting text!', padId, content);
await API.setText(padId, content);
res.send('Success updating pad');
} catch (e) {
console.error('ep_post_data: Error updating pad', padId, e);
res.send('Error updating pad');
}
}
});
});
callback();
};