-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
81 lines (64 loc) · 1.9 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
'use strict';
const request = require('./dropbox');
function Dropbox() {}
Dropbox.prototype.createFolder = options => {
const header = {
path : '/2/files/create_folder',
contentType : 'application/json',
Authorization: options.token
};
let data = JSON.stringify({'path': options.folder});
return new Promise((fulfilled, reject) => {
request(header,data, (err, res) => {
err? reject(err) : fulfilled(res);
});
});
};
Dropbox.prototype.getFolders = options => {
const header = {
path : '/2/files/list_folder',
contentType : 'application/json',
Authorization: options.token
};
let data = JSON.stringify(
{'path': options.folder, 'recursive': options.isRecurseve }
);
return new Promise((fulfilled, reject) => {
request(header,data, (err, res) => {
err? reject(err) : fulfilled(res);
});
});
};
Dropbox.prototype.download = options => {
const imgName = options.fileName;
let header = {
hostname : 'content.dropboxapi.com',
path : '/2/files/download',
Authorization: options.token,
headerPath : `{\"path\":\"/photos/sample_imgs/${imgName}\"}`
};
return new Promise((fulfilled, reject) => {
request(header,(err, res) => {
err? reject(err) : fulfilled(res);
});
});
};
Dropbox.prototype.upload = options => {
const data = options.buffer;
const bufferLen = Buffer.byteLength(data);
const path = `${options.folder}/${options.fileName}`;
let header = {
hostname : 'content.dropboxapi.com',
path : '/2/files/upload',
Authorization: options.token,
contentType : 'application/octet-stream',
contentLength : bufferLen,
headerPath : `{\"path\": \"${path}\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}`
};
return new Promise((fulfilled, reject) => {
request(header,data,(err, res) => {
err? reject(err) : fulfilled(res);
});
});
};
module.exports = Dropbox;