function retrieveLatestImage(req, res){
    if(latestImage[req.params.dronename] != undefined){
        try{
            var imageData = fs.readFileSync(latestImage[req.params.dronename]);
            res.set('Content-Type', 'image/jpeg');
            logger.info("Sending 1 ");
            res.send(imageData);
            return;
        } catch (e){
            logger.error("Image Cache Error.");
            logger.error(e.message);
            delete latestImage[req.params.dronename];
        }
    }

    var images = cloudant.use(req.params.dronename + '_images');
    images.list({"descending": true, "include_docs": true, "limit": 2}, function(err, body){
        if(err){
            logger.error(err);
            res.status(404).send("Internal error");
        }else{
            if(body.rows.length == 0){
                res.status(404).send("No images found");
                return;
            }
            
            if(body.rows[0].id.indexOf('_design') > -1){
                body.rows.splice(0, 1);
            }

            if(body.rows.length == 0){
                res.status(404).send("No images found in database");
                return;
            }

            var filename = body.rows[0].id;
            images.attachment.get(filename, "image", function(err, body){
                if(err){ // Would reflect data input error
                    logger.error(err);
                    res.status(404).send(err.message);
                }else{
                    res.set('Content-Type', 'image/jpeg');
                    res.send(body);
                    logger.info("Sending latest image from database ");
                    // Cache response - need to append original filename?
                    fs.writeFile(uploadDir+filename, body, function(err){
                        if(err){
                            logger.error(err);
                        }else{
                            latestImage[req.params.dronename] = uploadDir+filename;
                        }
                    });
                }
            });
        }
    });
}
/**
 * Retrieve latest image, from cache or Cloudant database
 * @param req {Object} The request from the client
 * @param res {Object} The response for the client
 * @param req.params.dronename {String} The drone database to use
 */
Retrieve latest image, from cache or Cloudant database
@param req {Object} The request from the client
@param res {Object} The response for the client
@param req.params.dronename {String} The drone database to use
Retrieve latest image from cache or Cloudant database @param req { Object } The request from the client @param res { Object } The response for the client @param req . params . dronename { String } The drone database to use
function retrieveLatestImage(req, res){
    if(latestImage[req.params.dronename] != undefined){
        try{
            var imageData = fs.readFileSync(latestImage[req.params.dronename]);
            res.set('Content-Type', 'image/jpeg');
            logger.info("Sending 1 ");
            res.send(imageData);
            return;
        } catch (e){
            logger.error("Image Cache Error.");
            logger.error(e.message);
            delete latestImage[req.params.dronename];
        }
    }
    var images = cloudant.use(req.params.dronename + '_images');
    images.list({"descending": true, "include_docs": true, "limit": 2}, function(err, body){
        if(err){
            logger.error(err);
            res.status(404).send("Internal error");
        }else{
            if(body.rows.length == 0){
                res.status(404).send("No images found");
                return;
            }
            if(body.rows[0].id.indexOf('_design') > -1){
                body.rows.splice(0, 1);
            }
            if(body.rows.length == 0){
                res.status(404).send("No images found in database");
                return;
            }
            var filename = body.rows[0].id;
            images.attachment.get(filename, "image", function(err, body){
                if(err){ 
                    logger.error(err);
                    res.status(404).send(err.message);
                }else{
                    res.set('Content-Type', 'image/jpeg');
                    res.send(body);
                    logger.info("Sending latest image from database ");
                    fs.writeFile(uploadDir+filename, body, function(err){
                        if(err){
                            logger.error(err);
                        }else{
                            latestImage[req.params.dronename] = uploadDir+filename;
                        }
                    });
                }
            });
        }
    });
}
function retrieveLatestImage ( req , res ) { if ( latestImage [ req . params . dronename ] != undefined ) { try { var imageData = fs . readFileSync ( latestImage [ req . params . dronename ] ) ; res . set ( 'Content-Type' , 'image/jpeg' ) ; logger . info ( "Sending 1 " ) ; res . send ( imageData ) ; return ; } catch ( e ) { logger . error ( "Image Cache Error." ) ; logger . error ( e . message ) ; delete latestImage [ req . params . dronename ] ; } } var images = cloudant . use ( req . params . dronename + '_images' ) ; images . list ( { "descending" : true , "include_docs" : true , "limit" : 2 } , function ( err , body ) { if ( err ) { logger . error ( err ) ; res . status ( 404 ) . send ( "Internal error" ) ; } else { if ( body . rows . length == 0 ) { res . status ( 404 ) . send ( "No images found" ) ; return ; } if ( body . rows [ 0 ] . id . indexOf ( '_design' ) > - 1 ) { body . rows . splice ( 0 , 1 ) ; } if ( body . rows . length == 0 ) { res . status ( 404 ) . send ( "No images found in database" ) ; return ; } var filename = body . rows [ 0 ] . id ; images . attachment . get ( filename , "image" , function ( err , body ) { if ( err ) { logger . error ( err ) ; res . status ( 404 ) . send ( err . message ) ; } else { res . set ( 'Content-Type' , 'image/jpeg' ) ; res . send ( body ) ; logger . info ( "Sending latest image from database " ) ; fs . writeFile ( uploadDir + filename , body , function ( err ) { if ( err ) { logger . error ( err ) ; } else { latestImage [ req . params . dronename ] = uploadDir + filename ; } } ) ; } } ) ; } } ) ; }
Retrieve latest image, from cache or Cloudant database
 @param req {Object} The request from the client
 @param res {Object} The response for the client
 @param req.params.dronename {String} The drone database to use
Retrieve latest image from cache or Cloudant database @param req { Object } The request from the client @param res { Object } The response for the client @param req . params . dronename { String } The drone database to use
// Would reflect data input error // Cache response - need to append original filename?