forked from kyma-incubator/kyma-showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
labels-logo.yaml
108 lines (101 loc) · 3.28 KB
/
labels-logo.yaml
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
apiVersion: serverless.kyma-project.io/v1alpha1
kind: Function
metadata:
name: labels-logo
spec:
deps: |-
{
"name": "labels-logo",
"version": "1.0.0",
"dependencies": {
"node-fetch": "2.6.1",
"@google-cloud/vision":"2.3.8",
"nanoid": "3.1.25"
}
}
env:
- name: API_CONFIG
valueFrom:
configMapKeyRef:
key: config.json
name: showcase-functions-config
- name: GCP_API_KEY
valueFrom:
secretKeyRef:
key: GCP_API_KEY
name: kyma-showcase-secret
- name: GCP_EMAIL
valueFrom:
secretKeyRef:
key: GCP_EMAIL
name: kyma-showcase-secret
runtime: nodejs14
source: |-
module.exports = {
main: async function (event, context) {
async function getBase64(id) {
console.log("Getting base64 from DB");
const fetch = require('node-fetch');
const URL = JSON.parse(process.env.API_CONFIG).API_URL + '/' + id;
const response = await fetch(URL);
const data = await response.json();
const content = data.content;
const base64 = content.replace(/data:.*?base64,/, '');
console.log("Base64 loaded");
return base64;
}
async function getLogoDetails(base64){
console.log("Getting logo details from GCP");
const vision = require('@google-cloud/vision');
const options = {
credentials: {
client_email: process.env.GCP_EMAIL,
private_key: process.env.GCP_API_KEY.replace(/\\n/gm, '\n'),
},
};
const client = new vision.ImageAnnotatorClient(options);
const request = {
image: {
content: Buffer.from(base64, 'base64'),
},
};
const [result] = await client.logoDetection(request);
const resultLogos = result.logoAnnotations;
const logoDetails = resultLogos.map(logo => logo.description);
if (logoDetails.length === 0)
return null;
const logoJson = JSON.stringify({logo:logoDetails});
console.log("Logo details loaded");
return logoJson;
}
async function putDetailsToDB(id, data){
console.log("Putting logo details to DB");
const fetch = require('node-fetch');
const URL = JSON.parse(process.env.API_CONFIG).API_URL + '/' + id;
const response = await fetch(URL,{
method:'PUT',
body: data,
headers: {
'Content-type': 'application/json',
},
});
await console.log("Logo details put to DB with status: " + response.status);
return response.json();
}
try{
// Needed to remove unwanted apostrophes from our ID
const imgID = event.data.slice(1, -1);
console.log("Processing image: " + imgID);
const base64 = await getBase64(imgID);
const logoDetails = await getLogoDetails(base64);
if(logoDetails){
await putDetailsToDB(imgID, logoDetails);
}else {
console.log("Logo array is empty");
}
}catch(err){
console.error(err);
return null;
}
}
}