Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     // removes all document metadata and also removes the file, in case
  2.     // no other documents has the same file attached to it
  3.     function removeDocumentMetadatas(docIDs, cb){
  4.         async.each(docIDs, (docID, callback) => {
  5.             DocumentMetadata.findAll({
  6.                 where:{
  7.                     document_id: docID
  8.                 },
  9.                 attributes: ['filehash', 'effective_path']
  10.             }).then((metas) => {
  11.                 // now go over each filehash and check if other
  12.                 // metadata records exist with the same file
  13.                 // if there are not such records, we can delete the document
  14.                 async.each(metas, (meta, filecb) => {
  15.                     var filehash = meta.filehash;
  16.                     var filepath = meta.effective_path;
  17.                     DocumentMetadata.findAll({
  18.                         where: {
  19.                             filehash: filehash
  20.                         },
  21.                         attributes: ['document_id']
  22.                     }).then((metasWithSameHash) => {
  23.                         // check all fetched meta records
  24.                         var a = true;
  25.                         _.each(metasWithSameHash, (meta) => {
  26.                             if(meta.document_id !== docID){
  27.                                 a = false;
  28.                             }
  29.                         });
  30.                         // if a false no other document has the same file
  31.                         if(!a){
  32.                             // delete file
  33.                             fs.unlink(filepath, (err) => {
  34.                                 if(err){
  35.                                     filecb(err);
  36.                                 }
  37.                                 filecb();
  38.                             });
  39.                         }else{
  40.                             filecb();
  41.                         }
  42.                     });
  43.                 }, (err) => {
  44.                     if(err){
  45.                         callback(err);
  46.                         return;
  47.                     }
  48.                     // finally delete all document metadatas
  49.                     DocumentMetadata.destroy({
  50.                         where: {
  51.                             document_id: docID
  52.                         }
  53.                     }).then(() => {
  54.                         callback();
  55.                     }).catch((err) => callback(err));
  56.                 });
  57.             });
  58.         }, (err) => {
  59.             if(err){
  60.                 cb(err, false);
  61.                 return;
  62.             }
  63.             cb(null, docIDs);
  64.         });
  65.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement