-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (132 loc) · 3.8 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
const axios = require('axios')
const fs = require('fs')
const FormData = require('form-data')
require('dotenv').config()
const fileDir = './public'
const filePaths = []
const {
DSTOR_DOMAIN,
DSTOR_ACCESS_TOKEN_ENDPOINT,
DSTOR_EMAIL,
DSTOR_PASSWORD,
DSTOR_UPLOAD_TOKEN_ENDPOINT,
DSTOR_UPLOAD_ENDPOINT
} = process.env
const getAccessTokenWithCredentials = async () => {
try {
if (!DSTOR_ACCESS_TOKEN_ENDPOINT) throw new Error('token endpoint not found in env file')
console.log(DSTOR_ACCESS_TOKEN_ENDPOINT, DSTOR_EMAIL, DSTOR_PASSWORD)
const {
data: { access_token },
} = await axios(`${DSTOR_DOMAIN}/${DSTOR_ACCESS_TOKEN_ENDPOINT}`, {
method: 'post',
headers: {
'Content-Type': 'application/json',
},
data: {
email: DSTOR_EMAIL,
password: DSTOR_PASSWORD,
},
})
if (!access_token) throw new Error('Error getting token')
console.log('access_token from credentials: ', access_token)
return access_token
} catch (error) {
console.log('login error: ', error)
return undefined
}
}
const getUploadTokenWithAccessToken = async (accessToken) => {
try {
if (!accessToken) throw new Error('Error getting access token')
const uploadTokenUrl = `${DSTOR_DOMAIN}/${DSTOR_UPLOAD_TOKEN_ENDPOINT}`
console.log('uploadTokenUrl: ', uploadTokenUrl)
const { data } = await axios(uploadTokenUrl, {
method: 'post',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`,
},
data: {
chunks_number: 1,
// folder_to_upload_to: 0,
folder_path: '/',
},
})
console.log('getUploadTokenWithAccessToken data: ', data)
if (!data.token) throw new Error('Error getting upload token')
console.log('data.token: ', data.token)
return data.token
} catch (err) {
console.log('getUploadToken error: ', err)
}
}
const uploadFile = async (
uploadToken,
accessToken,
file,
comment
) => {
try {
console.log('uploading file: ')
if (!DSTOR_UPLOAD_ENDPOINT) throw new Error('add file endpoint not found.')
const fd = new FormData()
fd.append('', file)
// console.log('uploadFile token: ', token)
// console.log('DSTOR_UPLOAD_ENDPOINT', DSTOR_UPLOAD_ENDPOINT)
console.log('headers: ', fd.getHeaders())
const { data } = await axios(`${DSTOR_DOMAIN}/${DSTOR_UPLOAD_ENDPOINT}`, {
method: 'post',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Access-Control-Allow-Origin': '*',
'Content-Type': 'multipart/form-data',
'x-dstor-upload-token': uploadToken,
// 'x-dstor-parent-id': 0, // root folder,
'x-dstor-comment': 'From DecideVoter',
...fd.getHeaders(),
},
data: fd,
})
console.log('upload data: ', data)
let hash
if (data && data.upload_token && !data.errorAtStage) {
while (!hash) {
const { data: uploadStatus } = await axios(`${DSTOR_DOMAIN}/${DSTOR_UPLOAD_STATUS_ENDPOINT}`, {
headers: {
'Authorization': `Bearer ${accessToken}`,
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'x-dstor-upload-token': uploadToken,
},
})
console.log('uploadFile uploadStatus: ', uploadStatus)
if (uploadStatus.status === 'DONE') {
hash = uploadStatus.data[0].Hash
}
}
}
return {
hash
}
} catch (err) {
console.log('uploadFile error: ', err.message)
return undefined
}
}
const main = async () => {
const accessToken = await getAccessTokenWithCredentials()
console.log('accessToken: ', accessToken)
const uploadToken = await getUploadTokenWithAccessToken(accessToken)
console.log('uploadToken: ', uploadToken)
for (const file of filePaths) {
const fileData = fs.createReadStream(file)
// console.log('fileData: ', fileData)
await uploadFile(uploadToken, accessToken, fileData)
}
}
fs.readdirSync(fileDir).forEach(filePath => {
console.log(filePath);
filePaths.push(`${fileDir}/${filePath}`)
})
main()