-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
132 lines (117 loc) · 3.66 KB
/
routes.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
'use strict';
const async = require('async');
const auth = require('http-auth');
const path = require('path');
const fs = require('fs');
const dropbox = require('./lib/dropbox');
const database = require('./lib/database');
const image = require('./lib/image');
const basicAuth = auth.basic({ realm: process.env.REALM },
(user, pw, callback) => {
callback(user === process.env.USERNAME && pw === process.env.PASSWORD)
}
);
const handleMarkdown = (token, entry, filename) => {
const out = fs.createWriteStream(path.join(process.env.ARTICLE_FOLDER, filename));
dropbox.downloadFile(token, entry.path_lower).pipe(out);
};
const handleGif = (token, entry, filename) => {
// download gif as is
const out = fs.createWriteStream(path.join(process.env.IMAGE_FOLDER, filename));
dropbox.downloadFile(token, entry.path_lower).pipe(out);
// extract first frame as placeholder
image.gifPlaceholder(token, entry, filename);
};
const handleImage = (token, entry, filename) => {
image.resize(token, entry, filename);
};
const processEntries = (token, entries) => {
entries.forEach((entry) => {
if (entry['.tag'] === 'file') {
console.log(`\t${entry.path_lower} added or modified.`);
const filename = entry.path_lower.split('/').reverse()[0].split(' ').join('-');
const extension = filename.split('.').reverse()[0];
switch (extension.toLowerCase()) {
case 'md':
handleMarkdown(token, entry, filename);
break;
case 'jpg':
case 'jpeg':
case 'png':
handleImage(token, entry, filename);
break;
case 'gif':
handleGif(token, entry, filename);
break;
default:
console.log(`\t\t${extension} files are not handled.`);
}
} else if (entry['.tag'] === 'deleted') {
console.log(`\t${entry.path_lower} deleted.`);
}
});
};
const processUser = (user) => {
console.log('User:', user);
database.getTokenAndCursor(user, (token, cursor) => {
let hasMore = true;
let entries = [];
async.whilst(
() => hasMore,
(cb) => {
if (cursor) {
dropbox.listFolderContinue(token, cursor, (err, res) => {
if (err) throw err;
entries = entries.concat(res.body.entries);
database.setCursor(user, res.body.cursor);
hasMore = res.body.has_more;
cb(err);
});
} else {
dropbox.listFolder(token, (err, res) => {
if (err) throw err;
entries = entries.concat(res.body.entries);
database.setCursor(user, res.body.cursor);
hasMore = res.body.has_more;
cb(err);
});
}
},
(err) => {
if (err) throw err;
processEntries(token, entries);
}
);
});
};
module.exports = (app) => {
app.get('/', auth.connect(basicAuth), (req, res) => {
res.render('signup', {
appId: process.env.APP_ID,
domain: process.env.DOMAIN,
redirectPath: process.env.REDIRECT_PATH,
});
});
app.get('/redirect', auth.connect(basicAuth), (req, res) => {
// get token for user and save in redis
dropbox.requestToken(req.query.code, (errGet, uid, token) => {
if (errGet) throw errGet;
database.setToken(uid, token, (errSet) => {
if (errSet) throw errSet;
console.log(`${uid} registered.`);
res.render('redirect');
});
});
});
app.get('/webhook', (req, res) => {
// echo challenge
res.send(req.query.challenge);
});
app.post('/webhook', (req, res) => {
res.sendStatus(200);
const users = req.body.delta.users;
users.forEach((user) => {
processUser(user);
});
});
};