This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 257
/
changelistener.js
137 lines (124 loc) · 4.21 KB
/
changelistener.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
/**
* Copyright 2016 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Listens to Cloudant changes and trigger frame extractor and analysis actions.
*
* Changes look like:
* {
* "seq": "2-g1AAMNCzw5SiMA1Du_HQi8L_RQteAoMZf4GVgissQ",
* "id": "1documentation22d01513-c30f-417b-8c27-56b3c0de12ac",
* "changes": [{
* "rev": "1-967a00dff5e02add41819138abb3284d"
* }]
* }
* or
* {
* "seq": "3-g1AAAgb6CTrgCTAU2RdXOqyy",
* "id": "1documentation22d01513-c30f-417b-8c27-56b3c0de12ac",
* "changes": [{
* "rev": "2-eec205a9d413992850a6e32678485900"
* }],
* "deleted": true
* }
*/
function main(event) {
console.log('[', event.id, '] Document change detected');
// nothing to do on deletion event
if (event.deleted) {
console.log('[', event.id, '] OK - ignored, deleted');
return { ok: true };
}
return new Promise((resolve, reject) => {
onDocumentChange(
event.cloudantUrl, event.cloudantDbName,
event.id, event.changes[0].rev, (err, result) => {
if (err) {
reject({ ok: false });
} else {
resolve(result);
}
}
);
});
}
exports.main = main;
function onDocumentChange(url, dbName, documentId, documentRev, callback) {
const mediaStorage = require('./lib/cloudantstorage')({
cloudantUrl: url,
cloudantDbName: dbName
});
mediaStorage.get(documentId, (err, doc) => {
if (err) {
console.log('[', documentId, '] KO', err);
callback(err);
return;
}
// if the document has already changed in between,
// ignore this change event, another one should be coming
// or was processed already
if (doc._rev !== documentRev) {
console.log('[', doc._id, '] OK - ignored, document has changed - event rev:',
documentRev, 'database rev:', doc._rev);
callback(null, { ok: true, has_changed: true });
return;
}
// if it is a video, it has a 'video.mp4' attachment and it has no metadata,
if (doc.type === 'video' && mediaStorage.hasAttachment(doc, 'video.mp4') && !doc.metadata) {
// trigger the frame-extractor
asyncCallAction('vision/extractor', doc, callback);
return;
}
// if it is a audio, it has a 'audio.ogg' attachment and it has no metadata,
if (doc.type === 'audio' && mediaStorage.hasAttachment(doc, 'audio.ogg') && !doc.transcript) {
// trigger the speech to text
asyncCallAction('vision/speechtotext', doc, callback);
return;
}
// if it is a audio, it has a transcript and no analysis
if (doc.type === 'audio' && doc.transcript && !doc.analysis) {
// trigger the analysis of the text
asyncCallAction('vision/textanalysis', doc, callback);
return;
}
// if this is an image, with an attachment and no analysis
if (doc.type === 'image' && mediaStorage.hasAttachment(doc, 'image.jpg') && !doc.analysis) {
// trigger the analysis
asyncCallAction('vision/analysis', doc, callback);
return;
}
// nothing to do with this change
console.log('[', doc._id, '] OK - ignored event for doc type:', doc.type);
callback(null, { ok: true, ignored: true });
});
}
function asyncCallAction(anActionName, aDoc, callback) {
console.log('[', aDoc._id, '] Calling', anActionName);
const openwhisk = require('openwhisk');
const whisk = openwhisk({ ignore_certs: true });
whisk.actions.invoke({
actionName: anActionName,
params: {
doc: aDoc
},
blocking: false
}).then((result) => {
console.log('[', aDoc._id, ']', anActionName, '[OK]', result);
callback(null, { ok: true });
}).catch((error) => {
console.log('[', aDoc._id, ']', anActionName, '[KO]', error);
callback(error);
});
}