Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // removes all document metadata and also removes the file, in case
- // no other documents has the same file attached to it
- function removeDocumentMetadatas(docIDs, cb){
- async.each(docIDs, (docID, callback) => {
- DocumentMetadata.findAll({
- where:{
- document_id: docID
- },
- attributes: ['filehash', 'effective_path']
- }).then((metas) => {
- // now go over each filehash and check if other
- // metadata records exist with the same file
- // if there are not such records, we can delete the document
- async.each(metas, (meta, filecb) => {
- var filehash = meta.filehash;
- var filepath = meta.effective_path;
- DocumentMetadata.findAll({
- where: {
- filehash: filehash
- },
- attributes: ['document_id']
- }).then((metasWithSameHash) => {
- // check all fetched meta records
- var a = true;
- _.each(metasWithSameHash, (meta) => {
- if(meta.document_id !== docID){
- a = false;
- }
- });
- // if a false no other document has the same file
- if(!a){
- // delete file
- fs.unlink(filepath, (err) => {
- if(err){
- filecb(err);
- }
- filecb();
- });
- }else{
- filecb();
- }
- });
- }, (err) => {
- if(err){
- callback(err);
- return;
- }
- // finally delete all document metadatas
- DocumentMetadata.destroy({
- where: {
- document_id: docID
- }
- }).then(() => {
- callback();
- }).catch((err) => callback(err));
- });
- });
- }, (err) => {
- if(err){
- cb(err, false);
- return;
- }
- cb(null, docIDs);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement