-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
56 lines (46 loc) · 1.06 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
'use strict';
var Dropbox = require('dropbox');
var options;
var queue = {};
function addToDropboxQueue(message) {
var id = message.fileName;
if (!message.attempts) {
message.attempts = 0;
}
if (message.attempts > options.maxAttempts) {
console.error('Max attempts reached for', id);
return;
}
var timeout = setTimeout(function saveToDropBoxWrapper() {
saveToDropBox(message);
delete queue[id];
}, options.delayTime);
if (queue[id]) {
clearTimeout(queue[id]);
}
queue[id] = timeout;
}
function saveToDropBox(message) {
var client = new Dropbox.Client({
key: options.id,
secret: options.secret,
token: message.token
});
client.writeFile(message.fileName, message.file, function (err) {
if (err) {
message.attempts++;
addToDropboxQueue(message);
console.error(err);
}
});
}
function handleMessages(message) {
if (message.file) {
addToDropboxQueue(message);
}
}
module.exports = function (config) {
options = config;
options.delayTime *= 1000;
return handleMessages;
};