generated from bitprj/Intro-To-Serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.3.2.js
69 lines (55 loc) · 2.44 KB
/
test.3.2.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
let uri = undefined
const fetch = require('node-fetch');
const fs = require('fs');
const FormData = require('form-data');
const functions = require('./functions.js')
uri = process.env.BUNNIMAGE_ENDPOINT
const blob_url = process.env.blob_url
const containerName = process.env.container_name
const args = require('minimist')(process.argv.slice(2))
const user = args['user'];
const repo = args['repo'];
async function main() {
try {
functions.checkSecret(blob_url, "blob_url")
functions.checkSecret(containerName, "containerName")
fs.readFile(`${__dirname}/testimage.jpg`, async function (err, content) {
try {
let formData = new FormData()
formData.append('data', content, { filename: "testimage.jpeg", type: "image/jpeg", data: content })
const formHeaders = formData.getHeaders();
const resp1 = await fetch(uri, {
method: 'POST',
body: formData,
headers: {
...formHeaders,
},
});
var result1 = await resp1.text()
let test1 = JSON.stringify(result1)
functions.validateResponseStatus(resp1, uri)
var download = `${blob_url}/${containerName}/test.jpeg`;
let resp = await fetch(download, {
method: 'GET',
})
let data = await resp;
if (data.statusText == "The specified blob does not exist.") {
console.error("Hmm... We couldn't find our image. Try again?")
console.error(`We tried using "${download}" to find the image, but did not receive a response.`)
await functions.throwError(`Hmm... We couldn't find our image. Try again? We tried using '${download}' to find the image, but did not receive a response.`, user, repo)
process.exit(1)
} else {
console.info("Yay! 🎉 We got our picture!")
}
} catch (e) {
console.error("Try again! We got this error when trying to make a request: " + e)
await functions.throwError("Try again! We got this error when trying to make a request: " + e, user, repo)
process.exit(1)
}
})
}
catch (e) {
await functions.throwError(e, user, repo)
}
}
main();