-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
62 lines (51 loc) · 1.66 KB
/
plugin.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
// This plugin on bubble.io https://bubble.io/plugin/convertapi-1690977977458x650984287975768000
async function(properties, context) {
let body = {
Parameters: [
{
Name: 'StoreFile',
Value: true
}
]
}
let fileParam = {}
if (properties.files) {
fileParam.Name = 'Files'
fileParam.FileValues = []
let files = await properties.files.get(0, 1000000)
files.forEach(f => {
if (!f.startsWith('http')) {
f = 'https:' + f
}
fileParam.FileValues.push({ Url: f })
})
} else {
let file = properties.file
if (!file.startsWith('http')) {
file = 'https:' + file
}
fileParam.Name = 'File'
fileParam.FileValue = { Url: file }
}
body.Parameters.push(fileParam)
properties.parameters.forEach(p => body.Parameters.push(
{
Name: p.key,
Value: p.value
}
))
const secret = context.keys['ConvertAPI Secret']
const converterPath = properties.converter ? `/converter/${properties.converter}` : ''
const response = await fetch(`https://v2.convertapi.com/convert/${properties.src}/to/${properties.dst}${converterPath}?secret=${secret}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
})
.then(r => r.ok ? r.json() : r.text().then( e => {throw new Error(e)}) )
.then(o => {
let files = []
o.Files.forEach(f => files.push(f.Url))
return files
})
return { files: response }
}