-
Notifications
You must be signed in to change notification settings - Fork 14
/
node_dropbox.js
206 lines (178 loc) · 5.26 KB
/
node_dropbox.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
var request = require('request');
var authUrl = "https://www.dropbox.com/1/oauth2/authorize?";
var apiRoot = "https://api.dropbox.com/1/";
var apiContentRoot = "https://api-content.dropbox.com/1/";
var apiLongPoll = "https://notify.dropboxapi.com/1/longpoll_delta";
var api = {
auth: {
disable_token: '/disable_access_token'
},
account: '/account/info',
metadata: '/metadata',
fileops: {
createFolder: '/fileops/create_folder',
remove: '/fileops/delete',
copy: '/fileops/copy',
move: '/fileops/move'
},
putFiles: '/files_put',
getFiles: '/files'
};
exports.Authenticate = function(ckey, csecret, redirect_uri, cb) {
var err = "";
var redirect_url = "";
if(ckey === "" || csecret === "" || redirect_uri === "") {
err = "Missing client key and/or client secret key.";
}else{
redirect_url = authUrl + "client_id=" + ckey + "&response_type=code&redirect_uri=" + redirect_uri;
}
cb(err, redirect_url);
}
exports.AccessToken = function(ckey, csecret, auth_code, redirect_url, cb) {
var url = apiRoot + '/oauth2/token';
var body = {
"code": auth_code,
"grant_type": "authorization_code",
"redirect_uri": redirect_url,
"client_id": ckey,
"client_secret": csecret
};
request.post(url, {form: body, json: true}, function(err, res, body) {
cb(err, body);
});
}
exports.api = function(access_token) {
var access_token = access_token;
return {
account: function(cb) {
options = optionsBuilder(apiRoot + api.account, access_token);
request.get(options, function(err, res, body) {
cb(err, res, body);
})
},
createDir: function(path, cb) {
options = postBuilder(apiRoot + api.fileops.createFolder,
{root:"auto", path:path}, access_token);
request.post(options, function(err, res, body) {
cb(err, res, body);
})
},
removeDir: function(path, cb) {
options = postBuilder(apiRoot + api.fileops.remove,
{root:"auto", path:path}, access_token);
request.post(options, function(err, res, body) {
cb(err, res, body);
});
},
moveSomething: function(from, to, cb) {
options = postBuilder(apiRoot + api.fileops.move,
{root:"auto", from_path:from, to_path:to}, access_token);
request.post(options, function(err, res, body) {
cb(err, res, body);
});
},
createFile: function(path, body, cb) {
options = {
method: "PUT",
url: apiContentRoot + api.putFiles + "/auto/" + path,
headers: {
"Content-Length": getByteLength(body),
"Authorization": "Bearer " + access_token
},
json: true,
body: body
}
request(options, function(err, res, body) {
cb(err, res, body);
})
},
removeFile: function(path, cb) {
options = postBuilder(apiRoot + api.fileops.remove,
{root: "auto", path:path}, access_token);
request.post(options, function(err, res, body) {
cb(err, res, body);
});
},
/**
* get the metadata of a file or folder
* the result body.contents can be used to list a folder's content
*/
getMetadata: function(path, cb) {
options = optionsBuilder(apiRoot + api.metadata + "/auto/" + path, access_token);
request.get(options, function(err, res, body) {
cb(err, res, body);
});
},
/**
* Downloads a file.
*/
getFile: function(path, cb) {
options = optionsBuilder(apiContentRoot + api.getFiles + '/auto' + path, access_token);
request(options, function(err, res, body) {
cb(err, res, body);
})
},
/**
* Let connect pipe to downloaded file.
*/
getFilePipe: function(path, cb) {
options = optionsBuilder(apiContentRoot + api.getFiles + '/auto' + path, access_token);
return request(options, cb)
},
/**
* Gets changes to files and folders in a user's Dropbox.
*/
getDelta: function(cursor, path, cb) {
options = postBuilder(apiRoot + '/delta',
{cursor:cursor, path_prefix:path}, access_token);
request.post(options, function(err, res, body) {
cb(err, res, body);
})
},
/**
* Gets notifications from server
*/
getNotifications: function(cursor, timeout, cb) {
var cursor = cursor || null,
timeout = timeout || 480,
url = apiLongPoll + '?cursor=' + cursor + '&timeout=' + timeout;
request.get(url, function(err, res, body) {
cb(err, res, body);
});
}
}
}
function optionsBuilder(url, access_token) {
return {
url: url,
json: true,
headers: {
"Authorization": "Bearer " + access_token
}
}
}
function postBuilder(url, data, access_token) {
return {
url: url,
json: true,
headers: {
"Authorization": "Bearer " + access_token
},
form: data
}
}
function getByteLength(normal_val) {
// Force string type
normal_val = String(normal_val);
var byteLen = 0;
for (var i = 0; i < normal_val.length; i++) {
var c = normal_val.charCodeAt(i);
byteLen += c < (1 << 7) ? 1 :
c < (1 << 11) ? 2 :
c < (1 << 16) ? 3 :
c < (1 << 21) ? 4 :
c < (1 << 26) ? 5 :
c < (1 << 31) ? 6 : Number.NaN;
}
return byteLen;
}