Updating the controller

Now that are done with defining the routes and updating the model, we will work on the logic discussed in the Solution design section earlier. We are going to add a new method to CloudAIAPI named uploadVideo. Open server/controllers/cloud-ai-api.ts, and we will first add the required imports and the VideoIntelligenceServiceClient configuration. Before the class definition, add the following code:

// SNIPP SNIPP
const video = require('@google-cloud/video-intelligence').v1;
const client = new video.VideoIntelligenceServiceClient({
credentials: JSON.parse(process.env.GCP_SK_CREDENTIALS)
});
// SNIPP SNIPP

Using the environment variable we have set in the .env file, we are using the GCP_SK_CREDENTIALS value to initialize a new VideoIntelligenceServiceClient. Next, we are going to create a new method named uploadVideo and get started with converting the video file uploaded by the user to a base64 string, like we did for the image upload:

// SNIPP SNIPP
uploadVideo = (req, res) => {
// console.log('req.file', req.file);
const filePath = req.file.path;
this.base64_encode(filePath).then((BASE64_CONTENT) => {})
}
// SNIPP SNIPP

Inside the base64_encode() callback, we get started with constructing a request to send to the Video Intelligence API:

// SNIPP SNIPP
const request = {
inputContent: BASE64_CONTENT,
features: ['EXPLICIT_CONTENT_DETECTION', 'LABEL_DETECTION']
};

client
.annotateVideo(request)
.then((results) => {
const operation = results[0];
return operation.promise();
}
.then(results => {
// CODE BELOW
}).catch(err => {
console.error('ERROR:', err);
return res.status(500).json(err);
});
// SNIPP SNIPP

Using annotateVideo() on the instance of VideoIntelligenceServiceClient, we make a request submitting the video and the features we want to detect. In the second promise, we will have the actual response to the video analysis. The code present inside this section will be as follows:

// SNIPP SNIPP
// Gets annotations for video
const annotations = results[0].annotationResults[0];
const explicitContentResults = annotations.explicitAnnotation;
const segmentLabelAnnotations = annotations.segmentLabelAnnotations;
// console.log(JSON.stringify(annotations, null, 4));
let isExplicit = false;
let explictLabels = [];
if (explicitContentResults) {
explicitContentResults.frames.forEach((result) => {
var o: any = {};
// console.log('result', JSON.stringify(result, null, 4));
o.timeOffset = result.timeOffset;
o.pornographyLikelihood = result.pornographyLikelihood;
explictLabels.push(JSON.parse(JSON.stringify(o)));
if (result.pornographyLikelihood > 2) isExplicit = true;
});
}
let segmentLabels = [];
if (segmentLabelAnnotations) {
segmentLabelAnnotations.forEach((label) => {
let o: any = {};
// console.log('label', JSON.stringify(label, null, 4));
o.entity = label.entity;
o.categoryEntities = label.categoryEntities;
o.segments = label.segments; // array
segmentLabels.push(JSON.parse(JSON.stringify(o)));
});
}
if (isExplicit) {
this.deleteFile(filePath);
return res.status(400).json({
message: 'Adult Content is not allowed'
})
}
// Upload our video to cloudinary for external file hosting
// This is optional & you can use any service for the same
cloudinary.v2.uploader.upload(filePath, {
resource_type: 'video'
}, (error, result) => {
// console.log('result: ', result);
// console.log('error', error);
if (error) {
return res.status(400).json({
message: error.message
});
}
// CLOUDINARY CODE BELOW
});
// SNIPP SNIPP

In the previous code, we fetch annotationResults and extract explicitAnnotation and segmentLabelAnnotations. From explicitContentResults, we extract timeOffset and pornographyLikelihood. And then, from segmentLabelAnnotations, we extract entitycategoryEntities, and segments. If any one of the explicitContentResults returns a possibility of pornographyLikelihood, we respond with a bad request. Now that we have a valid video, we will upload it to Cloudinary:

// SNIPP SNIPP
let msg: any = {};
msg.thread = req.params.threadId;
msg.createdBy = req.user;
msg.lastUpdatedBy = req.user;
msg.explicitVideoAnnotation = explictLabels;
msg.segmentLabelAnnotations = segmentLabels;
msg.cloudinaryProps = result;

msg.description = `<div align="center" class="embed-responsive embed-responsive-16by9">
<video loop class="embed-responsive-item" controls>
<source src="${result.secure_url}">
Your browser does not support the video tag.
</video>
</div>`;

let message = new Message(msg);
message.save((err, msg) => {
if (err) {
console.log(err);
return this.respondErrorMessage(res, err);
}
res.status(200).json(msg);
});

// Delete the local file so we don't clutter
this.deleteFile(filePath);
// SNIPP SNIPP

Once the upload is completed, we will extract the video URL and build a message description that can display the video and save the message to database. This wraps up our controller logic as well as our service side logic. In the next section, we are going work on the client-side logic.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.41.205