-
Notifications
You must be signed in to change notification settings - Fork 1
/
dropbox.js
45 lines (36 loc) · 995 Bytes
/
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
'use strict';
const request = require('https').request;
const fs = require('fs');
const Options = require('./options');
const Request = (options,data,callback) => {
if(typeof data === 'function') {
callback = data;
data = '';
}
const opt = new Options(options);
const req = request(opt,response);
function response(res) {
let body = '';
if (res.headers['content-type'] === 'application/json') {
res.on('data', chunck => {
body += chunck.toString();
});
} else {
const dropHeader = JSON.parse(res.headers['dropbox-api-result']);
const name = `./${dropHeader.name}`;
res.pipe(fs.createWriteStream(name));
body = name;
}
res.on('end',() => {
if (res.statusCode >= 300) {
return callback(body);
} else {
return callback(null, body);
}
});
res.on('error', err => callback(err));
}
req.write(data);
req.end();
};
module.exports = Request;