lib/gridFileManager.js
function getFileDetails(db, fileSelectionCriteria, fileOptions, cb) {

  defaultLogger.debug("In getFileDetails ");
  var selectionQuery = undefined;
  if (fileSelectionCriteria.groupId) {

    selectionQuery= {"metadata.groupId": fileSelectionCriteria.groupId};


  } else if (fileSelectionCriteria.hash) {
    selectionQuery= {"md5": fileSelectionCriteria.hash};
  }

  var gridStore = new GridStore(db, null, "r", {"root": constants.ROOT_COLLECTION});
  var fileOfInterest = undefined;

  gridStore.collection(function(err, collection) {
    collection.find(selectionQuery, { "sort": {"metadata.version":-1}}, function(err, files) {
      if (err) {
        defaultLogger.error(err); return cb(err);
      }

      files.toArray(function(err, filesArray) {
        if (err) {
          defaultLogger.error(err); return cb(err);
        }
        if (!(filesArray && Array.isArray(filesArray) && filesArray.length > 0)) {
          return cb(new Error("No files exist for groupId " + fileSelectionCriteria.groupId));
        }

        //If the file details are needed for all files in the groupId, then an array containing all of the file details is returned.
        if (fileOptions.allMatchingFiles == true) { // eslint-disable-line eqeqeq
          fileOfInterest = filesArray;
        } else {// Just want the details of a single file.
          //If there is no version, get the latest version

          //If no version is found or we have a hash query, just want the first entry of the array.
          if (!fileSelectionCriteria.version || fileSelectionCriteria.hash) {
            fileOfInterest = filesArray[0];
          } else {
            fileOfInterest = filesArray.filter(function(file) {
              return file.metadata.version === fileSelectionCriteria.version;
            });

            if (fileOfInterest.length === 1) {
              fileOfInterest = fileOfInterest[0];
            } else {
              return cb(new Error("Unexpected number of files returned for groupId " + fileSelectionCriteria.groupId, fileOfInterest.length));
            }
          }
        }

        if (!Array.isArray(fileOfInterest)) {
          //Actually want the thumbnail for the file, return the details for that file instead.
          if (fileOptions.thumbnail) {
            if (fileOfInterest.metadata.thumbnail) {
              getThumbnailFileDetails(db, fileOfInterest.metadata.thumbnail, function(err, thumbFileDetails) {
                if (err) {
                  defaultLogger.error(err); return cb(err);
                }

                fileOfInterest = thumbFileDetails;

                gridStore.close(function(err) {
                  return cb(err, fileOfInterest);
                });
              });
            } else {
              return cb(new Error("Thumbnail for file " + JSON.stringify(fileSelectionCriteria) + " does not exist."));
            }
          } else {
            gridStore.close(function(err) {
              return cb(err, fileOfInterest);
            });
          }
        } else {
          gridStore.close(function(err) {
            return cb(err, fileOfInterest);
          });
        }
      });
    });
  });
}
function getFileDetails(db, fileSelectionCriteria, fileOptions, cb) {

  defaultLogger.debug("In getFileDetails ");
  var selectionQuery = undefined;
  if (fileSelectionCriteria.groupId) {

    selectionQuery= {"metadata.groupId": fileSelectionCriteria.groupId};


  } else if (fileSelectionCriteria.hash) {
    selectionQuery= {"md5": fileSelectionCriteria.hash};
  }

  var gridStore = new GridStore(db, null, "r", {"root": constants.ROOT_COLLECTION});
  var fileOfInterest = undefined;

  gridStore.collection(function(err, collection) {
    collection.find(selectionQuery, { "sort": {"metadata.version":-1}}, function(err, files) {
      if (err) {
        defaultLogger.error(err); return cb(err);
      }

      files.toArray(function(err, filesArray) {
        if (err) {
          defaultLogger.error(err); return cb(err);
        }
        if (!(filesArray && Array.isArray(filesArray) && filesArray.length > 0)) {
          return cb(new Error("No files exist for groupId " + fileSelectionCriteria.groupId));
        }

        //If the file details are needed for all files in the groupId, then an array containing all of the file details is returned.
        if (fileOptions.allMatchingFiles == true) { // eslint-disable-line eqeqeq
          fileOfInterest = filesArray;
        } else {// Just want the details of a single file.
          //If there is no version, get the latest version

          //If no version is found or we have a hash query, just want the first entry of the array.
          if (!fileSelectionCriteria.version || fileSelectionCriteria.hash) {
            fileOfInterest = filesArray[0];
          } else {
            fileOfInterest = filesArray.filter(function(file) {
              return file.metadata.version === fileSelectionCriteria.version;
            });

            if (fileOfInterest.length === 1) {
              fileOfInterest = fileOfInterest[0];
            } else {
              return cb(new Error("Unexpected number of files returned for groupId " + fileSelectionCriteria.groupId, fileOfInterest.length));
            }
          }
        }

        if (!Array.isArray(fileOfInterest)) {
          //Actually want the thumbnail for the file, return the details for that file instead.
          if (fileOptions.thumbnail) {
            if (fileOfInterest.metadata.thumbnail) {
              getThumbnailFileDetails(db, fileOfInterest.metadata.thumbnail, function(err, thumbFileDetails) {
                if (err) {
                  defaultLogger.error(err); return cb(err);
                }

                fileOfInterest = thumbFileDetails;

                gridStore.close(function(err) {
                  return cb(err, fileOfInterest);
                });
              });
            } else {
              return cb(new Error("Thumbnail for file " + JSON.stringify(fileSelectionCriteria) + " does not exist."));
            }
          } else {
            gridStore.close(function(err) {
              return cb(err, fileOfInterest);
            });
          }
        } else {
          gridStore.close(function(err) {
            return cb(err, fileOfInterest);
          });
        }
      });
    });
  });
}
function getFileDetails ( db , fileSelectionCriteria , fileOptions , cb ) { defaultLogger . debug ( "In getFileDetails " ) ; var selectionQuery = undefined ; if ( fileSelectionCriteria . groupId ) { selectionQuery = { "metadata.groupId" : fileSelectionCriteria . groupId } ; } else if ( fileSelectionCriteria . hash ) { selectionQuery = { "md5" : fileSelectionCriteria . hash } ; } var gridStore = new GridStore ( db , null , "r" , { "root" : constants . ROOT_COLLECTION } ) ; var fileOfInterest = undefined ; gridStore . collection ( function ( err , collection ) { collection . find ( selectionQuery , { "sort" : { "metadata.version" : - 1 } } , function ( err , files ) { if ( err ) { defaultLogger . error ( err ) ; return cb ( err ) ; } files . toArray ( function ( err , filesArray ) { if ( err ) { defaultLogger . error ( err ) ; return cb ( err ) ; } if ( ! ( filesArray && Array . isArray ( filesArray ) && filesArray . length > 0 ) ) { return cb ( new Error ( "No files exist for groupId " + fileSelectionCriteria . groupId ) ) ; } //If the file details are needed for all files in the groupId, then an array containing all of the file details is returned. if ( fileOptions . allMatchingFiles == true ) { // eslint-disable-line eqeqeq fileOfInterest = filesArray ; } else { // Just want the details of a single file. //If there is no version, get the latest version //If no version is found or we have a hash query, just want the first entry of the array. if ( ! fileSelectionCriteria . version || fileSelectionCriteria . hash ) { fileOfInterest = filesArray [ 0 ] ; } else { fileOfInterest = filesArray . filter ( function ( file ) { return file . metadata . version === fileSelectionCriteria . version ; } ) ; if ( fileOfInterest . length === 1 ) { fileOfInterest = fileOfInterest [ 0 ] ; } else { return cb ( new Error ( "Unexpected number of files returned for groupId " + fileSelectionCriteria . groupId , fileOfInterest . length ) ) ; } } } if ( ! Array . isArray ( fileOfInterest ) ) { //Actually want the thumbnail for the file, return the details for that file instead. if ( fileOptions . thumbnail ) { if ( fileOfInterest . metadata . thumbnail ) { getThumbnailFileDetails ( db , fileOfInterest . metadata . thumbnail , function ( err , thumbFileDetails ) { if ( err ) { defaultLogger . error ( err ) ; return cb ( err ) ; } fileOfInterest = thumbFileDetails ; gridStore . close ( function ( err ) { return cb ( err , fileOfInterest ) ; } ) ; } ) ; } else { return cb ( new Error ( "Thumbnail for file " + JSON . stringify ( fileSelectionCriteria ) + " does not exist." ) ) ; } } else { gridStore . close ( function ( err ) { return cb ( err , fileOfInterest ) ; } ) ; } } else { gridStore . close ( function ( err ) { return cb ( err , fileOfInterest ) ; } ) ; } } ) ; } ) ; } ) ; }
Utility function to search for files in the database.
Utility function to search for files in the database .