-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (117 loc) · 3.71 KB
/
index.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const { Requester } = require("@chainlink/external-adapter");
const vision = require("@google-cloud/vision");
const Stream = require("stream").Transform;
const isSsl = !Boolean(
process.env.IPFS_GATEWAY_SSL &&
process.env.IPFS_GATEWAY_SSL.toLowerCase() === "false"
);
const httpProtocol = isSsl ? require("https") : require("http");
const createRequest = async (input, callback) => {
const visionClient = new vision.ImageAnnotatorClient({
projectId: process.env.GOOGLE_CLOUD_PROJECT_ID,
keyFilename: process.env.GOOGLE_CLOUD_KEY_FILENAME
});
return performRequest({
input,
callback,
visionClient,
isSsl,
httpProtocol
});
};
const performRequest = ({ input, callback, visionClient, ssl = false, httpProtocol = require("http") }) => {
const { data, id: jobRunID } = input;
if (!data) {
callback(500, Requester.errored(jobRunID, "No data"));
return;
}
const { hash } = data;
if (jobRunID === undefined) {
callback(500, Requester.errored(jobRunID, "Job run ID required"));
return;
}
if (hash === undefined) {
callback(500, Requester.errored(jobRunID, "Content hash required"));
} else {
const protocol = ssl ? "https" : "http";
const url = `${protocol}://${process.env.IPFS_GATEWAY_URL}/ipfs/${hash}`;
try {
httpProtocol
.request(url, function(response) {
var imgBytesStream = new Stream();
response.on("data", function(chunk) {
imgBytesStream.push(chunk);
});
response.on("end", function() {
requestSafeSearchLabels(imgBytesStream.read());
});
})
.end();
const requestSafeSearchLabels = async imgBytes => {
try {
const [result] = await visionClient.safeSearchDetection(imgBytes);
const detections = result.safeSearchAnnotation;
const { adult, violence, racy } = detections;
const response = {
data: {
adult,
violence,
racy
}
};
const likelihoodToConfidenceMapping = {
UNKNOWN: "",
VERY_UNLIKELY: "0",
UNLIKELY: "20",
POSSIBLE: "50",
LIKELY: "80",
VERY_LIKELY: "100"
};
response.data.result = [
likelihoodToConfidenceMapping[adult],
likelihoodToConfidenceMapping[racy],
likelihoodToConfidenceMapping[violence],
"",
""
].join(",");
callback(200, Requester.success(jobRunID, response));
} catch (error) {
console.error(error);
callback(500, Requester.errored(jobRunID, error));
}
};
} catch (error) {
console.error(error);
callback(500, Requester.errored(jobRunID, error));
}
}
};
// This is a wrapper to allow the function to work with
// GCP Functions
exports.gcpservice = (req, res) => {
createRequest(req.body, (statusCode, data) => {
res.status(statusCode).send(data);
});
};
// This is a wrapper to allow the function to work with
// AWS Lambda
exports.handler = (event, context, callback) => {
createRequest(event, (statusCode, data) => {
callback(null, data);
});
};
// This is a wrapper to allow the function to work with
// newer AWS Lambda implementations
exports.handlerv2 = (event, context, callback) => {
createRequest(JSON.parse(event.body), (statusCode, data) => {
callback(null, {
statusCode: statusCode,
body: JSON.stringify(data),
isBase64Encoded: false
});
});
};
// This allows the function to be exported for testing
// or for running in express
module.exports.createRequest = createRequest;
module.exports.performRequest = performRequest;