function combineAudio()

in functions/process_contact.js [171:235]


function combineAudio(bucketName, contactIdent, lambdaFunc, combinedBucket)
{
        const lambda = new aws.Lambda();
        var bucketParams = {
        	Bucket: bucketName,
        	Delimiter: '/',
        	Prefix: 'recordings/' + contactIdent
        };

    
        
        // List files in starting with a specific Contact ID
        s3bucket.listObjects(bucketParams, function (err, data){
            if (err) {console.log(err,err.stack);}
            else {
                // Loop thru to find the To and From files
                for (let index = 0; index < data['Contents'].length; index++)
                {
            		//console.log(data['Contents'][index]['Key']);
                    var filename = data['Contents'][index]['Key'];
                    if (filename.includes('TO')) {
                        var fileTo = filename;
                    }
                    if (filename.includes('FROM')){
                            var fileFrom = filename;
                    }
            	}
            	// if both files are found proceed to call the merge audio streams
                if ( fileTo && fileFrom) {
                    console.log("Got both files invoke Lambda");
                    var dates = new Date();
                    var datesString = dates.toISOString();
                    var lambdaParams = {
                        FunctionName: lambdaFunc,
                        InvocationType: "Event",
                        Payload:JSON.stringify({
                            "sources": [{
                                "Bucket": bucketName,
                                "Key": fileTo
                            },
                            {
                                "Bucket": bucketName,
                                "Key": fileFrom
                            }],
                            "target": {
                                "Bucket": combinedBucket,
                                "Key": contactIdent + "_" + datesString + "_COMBINED_AUDIO.wav"
                            }
                        })
                    };
                    console.log(lambdaParams);
                    // Invoke merge audio files
                    lambda.invoke(lambdaParams, function(error, data){
                            console.log("error value" + error);
                            if (error) {console.log("Error invoking Lambda");}
                                else {console.log(data);}
                    });
                }
                else {
                    // If just one file is found, wait for the other file.
                    console.log("Waiting for the other file");
                }
            }
    });
}