-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
123 lines (113 loc) · 4.33 KB
/
init.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
var fs = require('fs');
var box_sdk = require('box-sdk');
var async = require('async');
var options = {}; // See parseArgs()
var fileTree = {};
var gsms;
try {
gsms = require('./gsms');
} catch (e) {
gsms = {
"client_id": "tul5x43dwghbz7dque6h0z04py06b4oc",
"client_secret": "IWQeQ09keAHchQlGGllzOoAJlIXvtF4g",
};
}
function parseArgs() {
var args = process.argv;
if (!gsms.email && args.length < 3)
return false;
gsms.email = args[2];
gsms.host = args[3]?args[3]:gsms.host?gsms.host:'localhost';
gsms.port = args[4]?args[4]:gsms.port?gsms.port:5000;
if (!gsms.log)
gsms.log = 10;
return true;
}
function usage() {
console.log('Usage: node init.js email [host] [port]');
console.log('Examp: node init.js someone@email.com');
}
function walkFileTree(id, connection, fileTree, cb) {
connection.getFolderItems(id, {'fields': ['name', 'id'].join(',')}, function (error, body) {
body=eval(body);
async.each(body.entries, function (node, callback) {
fileTree.children[node.name] = {"parent":fileTree.id,"children":{}, "id": parseInt(node.id), "name": node.name};
if (node.type === 'folder') {
fileTree.children[node.name].type = "folder";
walkFileTree(fileTree.children[node.name].id, connection, fileTree.children[node.name], function (e, ft) {
fileTree.children[node.name] = ft;
callback(null, fileTree);
});
} else {
fileTree.children[node.name].type = "file";
connection.log.debug('getFileInfo '+node.id);
connection.getFileInfo(parseInt(node.id), function(e, res) {
res = eval(res);
fileTree.children[node.name].etag = parseInt(res.etag);
delete fileTree.children[node.name].children;
callback(null, fileTree);
});
}
}, function (err, result) {
cb(null, fileTree);
});
});
}
(function main() {
if (!parseArgs()) {
usage();
return -1;
}
var box = box_sdk.Box({
client_id: gsms.client_id,
client_secret: gsms.client_secret,
host: gsms.host,
port: gsms.port,
timeout: 0,
}, gsms.log);
connection = box.getConnection(gsms.email);
// Restore access_token and refresh_token if possible
connection._setTokens({access_token: gsms.access_token, refresh_token: gsms.refresh_token, expires_in: 1});
//Navigate user to the auth URL if neccessary
if (!connection.isAuthenticated()) {
console.log('Open '+connection.getAuthURL()+' in a browser');
}
connection.ready(function() {
// Save gsms
gsms.access_token = connection.access_token;
gsms.refresh_token = connection.refresh_token;
// Dummy call to refresh tokens if neccessary
connection.getFolderInfo(0, function (body) {
var wait = 0;
if (!connection.isAuthenticated()) {
console.log('Navigate to the auth URL in the next 60 seconds and try again.');
wait = 60*1000;
}
setTimeout(function() {
gsms.access_token = connection.access_token;
gsms.refresh_token = connection.refresh_token;
gsms.fileTree = {
"/": {
"type": "folder",
"parent": fileTree,
"children": {},
"id": 0
}};
// walkFileTree(0, connection, gsms.fileTree["/"], function (err, root) {
// gsms.fileTree["/"] = root;
console.log("Writing gsms.json...");
fs.writeFile('gsms.json', JSON.stringify(gsms), function(err) {
if (err) {
console.log('Error: '+err);
box.stopServer(function(){});
return false;
}
console.log('gsms.json initialized');
box.stopServer(function(){});
return true;
});
// });
}, wait);
});
});
})();