Created
October 8, 2014 19:46
-
-
Save anonymous/b8cc664fa1b53578a4f4 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
share: function(req,res){ | |
if(!req.param("id") || !req.param("permission")){ | |
return ErrorService.sendError(412,'Missing parameters',req,res); | |
} | |
shareFolderRecursively(parseInt(req.param("id"),10), req.param("permission"), function(){ | |
return console.log('DONE'); | |
}); | |
function shareFolderRecursively(id, permission,cb){ | |
Folder | |
.findOne({ id: id }) | |
.populate('files') | |
.populate('folders') | |
.exec(function(err,folder){ | |
if(err){ | |
console.log(err); | |
} | |
folder.permission = permission; | |
folder.save(function(err){ | |
if(err){ | |
return ErrorService.sendError(404,err,req,res); | |
} | |
/** Loop trough all the current folder's files and update their permissions **/ | |
async.each( | |
folder.files, | |
function shareFile(file,callback){ | |
file.permission = permission; | |
file.save(function(err){ | |
if(err){ | |
callback(err); | |
} | |
else{ | |
callback(); | |
} | |
}); | |
}, | |
function(err){ | |
if(err){ | |
console.log(err); | |
} | |
else if(folder.folders.length > 0 ){ | |
async.each( | |
folder.folders, | |
function reLoop(folder,callback2){ | |
shareFolderRecursively(folder.id,permission,callback2); | |
}, | |
function(err){ | |
if(err){ | |
console.log(err); | |
} | |
else{ | |
cb(); | |
} | |
} | |
); | |
} | |
else{ | |
cb(); | |
} | |
} | |
); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment