Created
January 26, 2012 16:57
-
-
Save Pita/1683771 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//File uploads | |
app.post('/upload', function(req, res) | |
{ | |
new formidable.IncomingForm().parse(req, function(err, fields, files) | |
{ | |
//handle errors | |
if(err){ | |
res.send("Error"); | |
fileLogger.error(err); | |
return; | |
} | |
//check if file was sended correctly | |
if(!files["file"]){ | |
res.send("Error"); | |
fileLogger.error("File was not uploaded correctly"); | |
return; | |
} | |
//check if file was sended correctly | |
if(!fields["padid"]){ | |
res.send("Error"); | |
fileLogger.error("No padid given"); | |
return; | |
} | |
fileLogger.info("fileupload recieved for pad '"+ fields["padid"] + "', called '" + files["file"].name+ "'"); | |
var padid = fields["padid"]; | |
async.series([ | |
//create the files directory | |
function(callback){ | |
fs.mkdir("../var/files", function(err){ | |
if(err && err.code != 'EEXIST'){ | |
callback(err) | |
} | |
else { | |
callback(); | |
} | |
}); | |
}, | |
//create files directory for this pad | |
function(callback){ | |
fs.mkdir("../var/files/" + padid, function(err){ | |
if(err && err.code != 'EEXIST'){ | |
callback(err) | |
} | |
else { | |
callback(); | |
} | |
}); | |
}, | |
//move the file | |
function(callback){ | |
fs.rename(files["file"].path, "../var/files/" + padid + "/" + files["file"].name, callback); | |
}, | |
], function(err){ | |
//handle error | |
if(err){ | |
fileLogger.error(err); | |
res.end("Error"); | |
return; | |
} | |
res.end("ok"); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment