Skip to content

Instantly share code, notes, and snippets.

@ondrek
Created September 1, 2013 18:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ondrek/6406457 to your computer and use it in GitHub Desktop.
Save ondrek/6406457 to your computer and use it in GitHub Desktop.
remove photo from mongodb and from amazon s3
/* from photo mongo database and s3 */
var removePhoto = function(){
mongoclient.connect(database, function(err, db) {
if (err) throw err;
/* url../remove?98c582c7d329/3b1fa423d917 - album:98c582c7d329 photo:3b1fa423d917 */
var url = require('url').parse(req.url).query;
var album = underscore.words(url, '/')[0];
var photo = underscore.words(url, '/')[1];
findAlbumByUrlHashInDB();
function findAlbumByUrlHashInDB(){
var collection = db.collection('albums');
collection.findOne({hash:album}, function(err, album) {
if (err) throw err;
findPhotoInAlbum(album);
});
}
function findPhotoInAlbum(album) {
var realNames = {};
var photos = album.files;
photos.forEach(function(element){
if (element.hash===photo) {
realNames.photo = element.name;
realNames.album = album.name;
removePhotoFromS3(realNames, removePhotoFromDB);
}
});
}
function removePhotoFromS3(realNames, callback){
s3.client.deleteObject({
Bucket : settings.bucket,
Key : settings.home + '/Photos/' + realNames.album + '/' + realNames.photo
}, function(err){
if(err) throw err;
callback(realNames.photo);
});
}
function removePhotoFromDB(photoName){
/* remove item in array files where name=title parameter */
var collection = db.collection('albums');
collection.update({hash:album}, {$pull:{files:{name:photoName}}}, function(err) {
if (err) throw err;
res.redirect('/photos/album?'+album);
db.close();
});
}
});
}; // removePhoto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment