forked from MrSwitch/hello.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropbox.js
219 lines (174 loc) · 4.95 KB
/
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
207
208
209
210
211
212
213
214
215
216
217
218
219
(function(hello) {
hello.init({
dropbox: {
name: 'Dropbox',
oauth: {
version: '1.0',
auth: 'https://www.dropbox.com/1/oauth/authorize',
request: 'https://api.dropbox.com/1/oauth/request_token',
token: 'https://api.dropbox.com/1/oauth/access_token'
},
login: function(p) {
// The dropbox login window is a different size
p.options.window_width = 1000;
p.options.window_height = 1000;
},
/*
Dropbox does not allow insecure HTTP URI's in the redirect_uri field
...otherwise I'd love to use OAuth2
Follow request https://forums.dropbox.com/topic.php?id=106505
p.qs.response_type = 'code';
oauth: {
version: 2,
auth: 'https://www.dropbox.com/1/oauth2/authorize',
grant: 'https://api.dropbox.com/1/oauth2/token'
}
*/
// API Base URL
base: 'https://api.dropbox.com/1/',
// Bespoke setting: this is states whether to use the custom environment of Dropbox or to use their own environment
// Because it's notoriously difficult for Dropbox too provide access from other webservices, this defaults to Sandbox
root: 'sandbox',
// Map GET requests
get: {
me: 'account/info',
// Https://www.dropbox.com/developers/core/docs#metadata
'me/files': req('metadata/@{root|sandbox}/@{parent}'),
'me/folder': req('metadata/@{root|sandbox}/@{id}'),
'me/folders': req('metadata/@{root|sandbox}/'),
'default': function(p, callback) {
if (p.path.match('https://api-content.dropbox.com/1/files/')) {
// This is a file, return binary data
p.method = 'blob';
}
callback(p.path);
}
},
post: {
'me/files': function(p, callback) {
var path = p.data.parent;
var fileName = p.data.name;
p.data = {
file: p.data.file
};
// Does this have a data-uri to upload as a file?
if (typeof (p.data.file) === 'string') {
p.data.file = hello.utils.toBlob(p.data.file);
}
callback('https://api-content.dropbox.com/1/files_put/@{root|sandbox}/' + path + '/' + fileName);
},
'me/folders': function(p, callback) {
var name = p.data.name;
p.data = {};
callback('fileops/create_folder?root=@{root|sandbox}&' + hello.utils.param({
path: name
}));
}
},
// Map DELETE requests
del: {
'me/files': 'fileops/delete?root=@{root|sandbox}&path=@{id}',
'me/folder': 'fileops/delete?root=@{root|sandbox}&path=@{id}'
},
wrap: {
me: function(o) {
formatError(o);
if (!o.uid) {
return o;
}
o.name = o.display_name;
o.first_name = o.name.split(' ')[0];
o.last_name = o.name.split(' ')[1];
o.id = o.uid;
delete o.uid;
delete o.display_name;
return o;
},
'default': function(o, headers, req) {
formatError(o);
if (o.is_dir && o.contents) {
o.data = o.contents;
delete o.contents;
o.data.forEach(function(item) {
item.root = o.root;
formatFile(item, headers, req);
});
}
formatFile(o, headers, req);
if (o.is_deleted) {
o.success = true;
}
return o;
}
},
// Doesn't return the CORS headers
xhr: function(p) {
// The proxy supports allow-cross-origin-resource
// Alas that's the only thing we're using.
if (p.data && p.data.file) {
var file = p.data.file;
if (file) {
if (file.files) {
p.data = file.files[0];
}
else {
p.data = file;
}
}
}
if (p.method === 'delete') {
p.method = 'post';
}
return true;
},
form: function(p, qs) {
delete qs.state;
delete qs.redirect_uri;
}
}
});
function formatError(o) {
if (o && 'error' in o) {
o.error = {
code: 'server_error',
message: o.error.message || o.error
};
}
}
function formatFile(o, headers, req) {
if (typeof o !== 'object' ||
(typeof Blob !== 'undefined' && o instanceof Blob) ||
(typeof ArrayBuffer !== 'undefined' && o instanceof ArrayBuffer)) {
// This is a file, let it through unformatted
return;
}
if ('error' in o) {
return;
}
var path = o.root + o.path.replace(/\&/g, '%26');
if (o.thumb_exists) {
o.thumbnail = hello.settings.oauth_proxy + '?path=' +
encodeURIComponent('https://api-content.dropbox.com/1/thumbnails/' + path + '?format=jpeg&size=m') + '&access_token=' + req.query.access_token;
}
o.type = (o.is_dir ? 'folder' : o.mime_type);
o.name = o.path.replace(/.*\//g, '');
if (o.is_dir) {
o.files = 'metadata/' + path;
}
else {
o.downloadLink = hello.settings.oauth_proxy + '?path=' +
encodeURIComponent('https://api-content.dropbox.com/1/files/' + path) + '&access_token=' + req.query.access_token;
o.file = 'https://api-content.dropbox.com/1/files/' + path;
}
if (!o.id) {
o.id = o.path.replace(/^\//, '');
}
// O.media = 'https://api-content.dropbox.com/1/files/' + path;
}
function req(str) {
return function(p, cb) {
delete p.query.limit;
cb(str);
};
}
})(hello);