-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.js
231 lines (207 loc) · 6.19 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
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
220
221
222
223
224
225
226
227
228
229
230
231
const request = require('request')
const purest = require('purest')({ request })
const { promisify } = require('node:util')
const Provider = require('../Provider')
const logger = require('../../logger')
const adapter = require('./adapter')
const { ProviderApiError, ProviderAuthError } = require('../error')
const { requestStream } = require('../../helpers/utils')
// From https://www.dropbox.com/developers/reference/json-encoding:
//
// This function is simple and has OK performance compared to more
// complicated ones: http://jsperf.com/json-escape-unicode/4
const charsToEncode = /[\u007f-\uffff]/g
function httpHeaderSafeJson (v) {
return JSON.stringify(v).replace(charsToEncode,
(c) => {
return `\\u${(`000${c.charCodeAt(0).toString(16)}`).slice(-4)}`
})
}
/**
* Adapter for API https://www.dropbox.com/developers/documentation/http/documentation
*/
class DropBox extends Provider {
constructor (options) {
super(options)
this.authProvider = DropBox.authProvider
this.client = purest({
...options,
provider: DropBox.authProvider,
})
// needed for the thumbnails fetched via companion
this.needsCookieAuth = true
}
static get authProvider () {
return 'dropbox'
}
_userInfo ({ token }, done) {
this.client
.post('users/get_current_account')
.options({ version: '2' })
.auth(token)
.request(done)
}
/**
* Makes 2 requests in parallel - 1. to get files, 2. to get user email
* it then waits till both requests are done before proceeding with the callback
*
* @param {object} options
* @param {Function} done
*/
_list (options, done) {
let userInfoDone = false
let statsDone = false
let userInfo
let stats
let reqErr
const finishReq = () => {
if (reqErr) {
logger.error(reqErr, 'provider.dropbox.list.error')
done(reqErr)
} else {
stats.body.user_email = userInfo.body.email
done(null, this.adaptData(stats.body, options.companion))
}
}
this.stats(options, (err, resp) => {
statsDone = true
stats = resp
if (err || resp.statusCode !== 200) {
err = this._error(err, resp)
}
reqErr = reqErr || err
if (userInfoDone) {
finishReq()
}
})
this._userInfo(options, (err, resp) => {
userInfoDone = true
userInfo = resp
if (err || resp.statusCode !== 200) {
err = this._error(err, resp)
}
reqErr = reqErr || err
if (statsDone) {
finishReq()
}
})
}
stats ({ directory, query, token }, done) {
if (query.cursor) {
this.client
.post('files/list_folder/continue')
.options({ version: '2' })
.auth(token)
.json({
cursor: query.cursor,
})
.request(done)
return
}
this.client
.post('files/list_folder')
.options({ version: '2' })
.qs(query)
.auth(token)
.json({
path: `${directory || ''}`,
include_non_downloadable_files: false,
})
.request(done)
}
async download ({ id, token }) {
try {
const req = this.client
.post('https://content.dropboxapi.com/2/files/download')
.options({
version: '2',
headers: {
'Dropbox-API-Arg': httpHeaderSafeJson({ path: `${id}` }),
},
})
.auth(token)
.request()
return await requestStream(req, async (res) => this._error(null, res))
} catch (err) {
logger.error(err, 'provider.dropbox.download.error')
throw err
}
}
async thumbnail ({ id, token }) {
try {
const req = this.client
.post('https://content.dropboxapi.com/2/files/get_thumbnail_v2')
.options({
headers: {
'Dropbox-API-Arg': httpHeaderSafeJson({ resource: { '.tag': 'path', path: `${id}` }, size: 'w256h256' }),
},
})
.auth(token)
.request()
return await requestStream(req, (resp) => this._error(null, resp))
} catch (err) {
logger.error(err, 'provider.dropbox.thumbnail.error')
throw err
}
}
_size ({ id, token }, done) {
return this.client
.post('files/get_metadata')
.options({ version: '2' })
.auth(token)
.json({ path: id })
.request((err, resp, body) => {
if (err || resp.statusCode !== 200) {
err = this._error(err, resp)
logger.error(err, 'provider.dropbox.size.error')
return done(err)
}
done(null, parseInt(body.size, 10))
})
}
_logout ({ token }, done) {
return this.client
.post('auth/token/revoke')
.options({ version: '2' })
.auth(token)
.request((err, resp) => {
if (err || resp.statusCode !== 200) {
logger.error(err, 'provider.dropbox.logout.error')
done(this._error(err, resp))
return
}
done(null, { revoked: true })
})
}
adaptData (res, companion) {
const data = { username: adapter.getUsername(res), items: [] }
const items = adapter.getItemSubList(res)
items.forEach((item) => {
data.items.push({
isFolder: adapter.isFolder(item),
icon: adapter.getItemIcon(item),
name: adapter.getItemName(item),
mimeType: adapter.getMimeType(item),
id: adapter.getItemId(item),
thumbnail: companion.buildURL(adapter.getItemThumbnailUrl(item), true),
requestPath: adapter.getItemRequestPath(item),
modifiedDate: adapter.getItemModifiedDate(item),
size: adapter.getItemSize(item),
})
})
data.nextPagePath = adapter.getNextPagePath(res)
return data
}
_error (err, resp) {
if (resp) {
const fallbackMessage = `request to ${this.authProvider} returned ${resp.statusCode}`
const errMsg = (resp.body || {}).error_summary ? resp.body.error_summary : fallbackMessage
return resp.statusCode === 401 ? new ProviderAuthError() : new ProviderApiError(errMsg, resp.statusCode)
}
return err
}
}
DropBox.prototype.list = promisify(DropBox.prototype._list)
DropBox.prototype.size = promisify(DropBox.prototype._size)
DropBox.prototype.logout = promisify(DropBox.prototype._logout)
module.exports = DropBox