-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
39 lines (35 loc) · 1.18 KB
/
upload.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
const fs = require('fs')
const path = require('path')
const join = require('url-join')
const request = require('request')
const share = require('./share')
const upload = (context, filePath) => {
const target = join(context.config.get('path') + path.basename(filePath))
const uploadUrl = join(context.config.get('url'), '/remote.php/webdav/', encodeURI(target))
const auth = { username: context.config.get('username'), password: context.config.get('password') }
const { size } = fs.statSync(filePath)
const headers = { 'OCS-APIRequest': 'true', 'Content-Length': size }
const readmeStream = fs.createReadStream(filePath)
readmeStream.on('error', console.log)
context.setProgress('Uploading…')
request({
method: 'PUT',
uri: uploadUrl,
headers: headers,
auth: auth,
body: readmeStream
},
function (error, response) {
if (response.statusCode !== 201) {
context.config.delete('username')
context.config.delete('password')
} else {
context.setProgress('Upload finished', 'completed')
share(context, filePath)
}
if (error) {
return console.error('Nextcloud upload failed:', error)
}
})
}
module.exports = upload