-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.js
68 lines (60 loc) · 2.07 KB
/
share.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
const join = require('url-join')
const request = require('request')
const moment = require('moment')
const path = require('path')
const share = (context, filePath) => {
const target = join(context.config.get('path') + path.basename(filePath))
const headers = { 'OCS-APIRequest': 'true', Accept: 'application/json' }
const validUntil = moment().add(context.config.get('lifeTime'), 'days').format('YYYY-MM-DD')
const shareUrl = join(context.config.get('url'), '/ocs/v2.php/apps/files_sharing/api/v1/shares')
const auth = { username: context.config.get('username'), password: context.config.get('password') }
const shareOptions = {
path: target,
shareType: 3,
permissions: 1,
expireDate: validUntil
}
let additionalInfo = `\n\nValid until: ${validUntil}`
if (context.config.get('randomPassword') === true) {
const generator = require('generate-password')
const password = generator.generate({
length: context.config.get('pwLength'),
numbers: true
})
shareOptions.password = password
additionalInfo += `\nPassword: ${password}`
}
context.setProgress('Getting link…')
request({
method: 'POST',
uri: shareUrl,
headers: headers,
auth: auth,
json: shareOptions
},
function (error, response, body) {
if (error) {
console.error(error.message)
}
try {
if (response.statusCode === 403) {
const regex = /([0-9])\w+/g
const match = body.ocs.meta.message.match(regex)
if (match === null) {
context.notify(body.ocs.meta.message)
} else {
const passwordLength = match[match.length - 1]
context.config.set('pwLength', passwordLength)
share(context, filePath)
}
} else {
context.copyToClipboard(body.ocs.data.url + additionalInfo)
context.notify('Nextcloud uploaded, link copied to Clipboard')
context.setProgress('Upload done and share link created…', 'completed')
}
} catch (error) {
context.notify('Could not get share link: ' + error.message)
}
})
}
module.exports = share