-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathfile-stream.js
115 lines (100 loc) · 2.53 KB
/
file-stream.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
var purest = require('../')
var fs = require('fs')
var path = require('path')
// ----------------------------------------------------------------------------
var auth = {
box: {
// root_readwrite
token: '',
},
drive: {
// drive.file
token: '',
},
dropbox: {
// files.content.write - on the app
token: '',
},
}
var file = {
cat: path.resolve(__dirname, '../test/fixtures/cat.png'),
}
// ----------------------------------------------------------------------------
var config = {
"box": {
"upload": {
"method": "POST",
"url": "https://upload.box.com/api/2.0/files/content",
"headers": {
"authorization": "Bearer {auth}"
}
}
},
"drive": {
"upload": {
"method": "POST",
"url": "https://www.googleapis.com/upload/drive/v3/files",
"headers": {
"authorization": "Bearer {auth}"
}
}
},
"dropbox": {
"upload": {
"method": "POST",
"url": "https://content.dropboxapi.com/2/files/upload",
"headers": {
"authorization": "Bearer {auth}",
"content-type": "application/octet-stream"
}
}
}
}
// ----------------------------------------------------------------------------
;({
// https://developer.box.com/reference/post-files-content/
'box upload': async () => {
var box = purest({provider: 'box', config, defaults: {
auth: auth.box.token
}})
await box('upload')
.multipart({
attributes: JSON.stringify({
name: 'cat.png',
parent: {id: 0},
}),
file: fs.createReadStream(file.cat)
})
.request()
},
// https://developers.google.com/drive/api/v3/manage-uploads#http_1
'drive upload': async () => {
var drive = purest({provider: 'drive', config, defaults: {
auth: auth.drive.token
}})
await drive('upload')
.multipart([
{
'Content-Type': 'application/json',
body: JSON.stringify({name: 'cat.png'})
},
{
'Content-Type': 'image/png',
body: fs.createReadStream(file.cat)
}
])
.request()
},
// https://www.dropbox.com/developers/documentation/http/documentation#files-upload
'dropbox upload': async () => {
var dropbox = purest({provider: 'dropbox', config, defaults: {
auth: auth.dropbox.token
}})
await dropbox('upload')
.headers({
'Dropbox-API-Arg': JSON.stringify({path: '/cat.png'}),
})
.body(fs.createReadStream(file.cat))
.request()
},
})[process.argv[2]]()