/**
 * Autor: John P. Masseria (john@masseria.org)
 */

// Limit concurrent camera access 
var sem = require('semaphore')(1);
var uuid = require('uuid');

var port = 8080;
var 
    app = require('http').createServer(handler).listen(port, "0.0.0.0"),
    path = require('path'),
    fs = require('fs'),
    sys = require('util'),
    exec = require('child_process').exec,
    child, child1;

function handler(req, res) {
    var
        fileName = path.basename(req.url) || 'index.html',
        ext = path.extname(fileName),
        localFolder = __dirname + '/',
        //localFolder = __dirname + '/public/',
        page404 = localFolder + '404.html';
 
    //do we support the requested file type?
    if(!extensions[ext]){
        //for now just send a 404 and a short message
        res.writeHead(404, {'Content-Type': 'text/html'});
        res.end("<html><head></head><body>The requested file type is not supported</body></html>");
    };

    if( fileName.toLowerCase() == 'snap.jpg' ) {
        sem.take(function() {
            var fname = uuid.v4(); // Get a random filename
            child = exec('../uvccapture/uvccapture -o/tmp/' + fname + ' -x640 -y480 -j5 -m -c./decorate.sh', function (error, stdout, stderr) {
                if (error !== null) {
                    console.log('kernel exec error: ' + error);
                } else {
                    getFile(('/tmp/' + fname + '.jpg'), res, page404, extensions[ext]);
                }; 
                child = exec('rm /tmp/' + fname + '*', function (error, stdout, stderr) {
                    if (error !== null)
                        console.log('rm error: ' + error);
                    sem.leave();
                });
            });
        });
    } else { 
        //call our helper function
        //pass in the path to the file we want,
        //the response object, and the 404 page path
        //in case the requestd file is not found
        getFile((localFolder + fileName), res, page404, extensions[ext]);
    };
};

//these are the only file types we will support
extensions = {
    ".html": "text/html",
    ".css" : "text/css",
    ".js"  : "application/javascript",
    ".png" : "image/png",
    ".gif" : "image/gif",
    ".jpg" : "image/jpeg",
    ".ico" : "image/x-icon"
};
 
//helper function handles file verification
function getFile(filePath, res, page404, mimeType){
    //does the requested file exist?
    fs.exists(filePath, function(exists){
        if(exists){
            //read the fiule, run the anonymous function
            fs.readFile(filePath, function(err,contents){
                if(!err){
                    //send the contents with the default 200/ok header
                    res.writeHead(200,{
                        "Content-type" : mimeType,
                        "Content-Length" : contents.length
                    });
                    res.end(contents);
                } else
                    console.dir('getFile: ' + err);  //for our own troubleshooting
            });
        } else {
            //if the requested file was not found serve-up our custom 404 page
            fs.readFile(page404, function(err,contents){
                if(!err){
                    //send the contents with a 404/not found header 
                    res.writeHead(404, {'Content-Type': 'text/html'});
                    res.end(contents);
                } else
                    console.dir('getFile: ' + err);  //for our own troubleshooting
            });
        };
    });
};
 
// listen for an HTTP request on port specified 
app.listen(port);

@@ -82,18 +82,17 @@
 a access
- 
 %0A
+
 var sem 
@@ -167,17 +167,16 @@
 080;%0Avar
- 
 %0A    app
@@ -647,18 +647,17 @@
 .html';%0A
- 
 %0A
+
     //do
@@ -1342,32 +1342,89 @@
        %7D else %7B%0A
+                    // Use synchronous I/O to read image%0A
                 
@@ -1434,16 +1434,20 @@
  getFile
+Sync
 (('/tmp/
@@ -1467,33 +1467,24 @@
 .jpg'), res,
- page404,
  extensions%5B
@@ -1475,35 +1475,37 @@
 res, extensions%5B
-ext
+'jpg'
 %5D);%0A            
@@ -1510,17 +1510,16 @@
       %7D;
- 
 %0A       
@@ -1793,17 +1793,16 @@
 %7D else %7B
- 
 %0A       
@@ -2327,17 +2327,357 @@
 con%22%0A%7D;%0A
- 
+%0A//helper function handles file verification%0Afunction getFileSync(filePath, res, mimeType)%7B%0A    //read the file%0A    contents = fs.readFile(filePath);%0A    //send the contents with the default 200/ok header%0A    res.writeHead(200,%7B%0A        %22Content-type%22 : mimeType,%0A        %22Content-Length%22 : contents.length%0A    %7D);%0A    res.end(contents);%0A%7D;%0A
 %0A//helpe
@@ -2889,17 +2889,16 @@
 d the fi
-u
 le, run 
@@ -3656,17 +3656,16 @@
 d header
- 
 %0A       
@@ -3911,17 +3911,16 @@
  %7D);%0A%7D;%0A
- 
 %0A// list
@@ -3959,17 +3959,16 @@
 pecified
- 
 %0Aapp.lis

Use readFileSync to read jpeg image file - need synchronous I/O inside semaphore