Skip to content

Instantly share code, notes, and snippets.

@Pita
Created January 26, 2012 16:57
Show Gist options
  • Save Pita/1683771 to your computer and use it in GitHub Desktop.
Save Pita/1683771 to your computer and use it in GitHub Desktop.
//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