lib/impl/pdfGeneration/cacheFiles.js
function mergeSubmissionFiles(params, cb) {
  logger.debug("cacheFiles: mergeSubmissionFiles", params);
  var submission = params.submission;

  //No submission, cannot continue.
  if (!_.isObject(submission)) {
    return cb(buildErrorResponse({
      err: new Error("Expected Submission Object To Cache Files For"),
      msg: "Error Exporting Submission As PDF",
      httpCode: 500
    }));
  }

  var form = submission.formSubmittedAgainst;


  var fileFieldDetails = _.filter(submission.formFields, function(field) {
    return isFileFieldType(field.fieldId.type);
  });
  fileFieldDetails = _.flatten(fileFieldDetails);


  var fieldTypes = _.groupBy(fileFieldDetails, function(field) {
    return field.fieldId.type;
  });

  //Files That Have To Be Loaded From Mbaas
  var mbaasTypes = _.union(fieldTypes.photo, fieldTypes.signature);

  //Only Interested In The File Ids To Download
  var filesToDownload = _.map(mbaasTypes, function(field) {
    return _.map(field.fieldValues, function(fieldValue) {
      return fieldValue.groupId;
    });
  });

  filesToDownload = _.flatten(filesToDownload);

  logger.debug("cacheFiles: mergeSubmissionFiles filesToDownload", filesToDownload);


  //Now download all of the files..
  async.mapSeries(filesToDownload, function(fileIdOrUrl, cb) {
    //Only want the callback to be called once.
    cb = _.once(cb);

    var fileUri = path.join(params.options.pdfExportDir, 'image_binary_' + fileIdOrUrl);
    var localFile = fs.createWriteStream(fileUri);

    //Loading The Submission File From THe Database / MbaaS
    cacheSubmissionFile({
      fileId: fileIdOrUrl,
      connections: params.connections,
      options: params.options,
      submission: submission
    }, localFile, function(err, fileDetails) {
      if (err) {
        logger.error("cacheFiles: cacheSubmissionFile", {error: err});
      }
      fileDetails = fileDetails || {};
      fileDetails.url = "file://" + fileUri;
      fileDetails.fileId = fileIdOrUrl;

      logger.debug("cacheFiles: mergeSubmissionFiles fileDetails", fileDetails);

      return cb(err, _.omit(fileDetails, "stream"));
    });
  }, function(err, cachedFiles) {
    if (err) {
      return cb(buildErrorResponse({
        error: err,
        msg: "Error Cacheing Files For Submission Export",
        code: constants.ERROR_CODES.FH_FORMS_ERR_CODE_PDF_GENERATION,
        httpCode: 500
      }));
    }

    //No Errors, all files are now cached for the submission

    submission.formSubmittedAgainst =  populateSubmissionFileData({
      form: form,
      submissionFiles: cachedFiles,
      downloadUrl: params.options.downloadUrl,
      fileUriPath: params.options.fileUriPath,
      submission: submission
    });

    logger.debug("cacheFiles: mergeSubmissionFiles submission", submission);

    cb(undefined, submission);
  });
}
function mergeSubmissionFiles(params, cb) {
  logger.debug("cacheFiles: mergeSubmissionFiles", params);
  var submission = params.submission;

  //No submission, cannot continue.
  if (!_.isObject(submission)) {
    return cb(buildErrorResponse({
      err: new Error("Expected Submission Object To Cache Files For"),
      msg: "Error Exporting Submission As PDF",
      httpCode: 500
    }));
  }

  var form = submission.formSubmittedAgainst;


  var fileFieldDetails = _.filter(submission.formFields, function(field) {
    return isFileFieldType(field.fieldId.type);
  });
  fileFieldDetails = _.flatten(fileFieldDetails);


  var fieldTypes = _.groupBy(fileFieldDetails, function(field) {
    return field.fieldId.type;
  });

  //Files That Have To Be Loaded From Mbaas
  var mbaasTypes = _.union(fieldTypes.photo, fieldTypes.signature);

  //Only Interested In The File Ids To Download
  var filesToDownload = _.map(mbaasTypes, function(field) {
    return _.map(field.fieldValues, function(fieldValue) {
      return fieldValue.groupId;
    });
  });

  filesToDownload = _.flatten(filesToDownload);

  logger.debug("cacheFiles: mergeSubmissionFiles filesToDownload", filesToDownload);


  //Now download all of the files..
  async.mapSeries(filesToDownload, function(fileIdOrUrl, cb) {
    //Only want the callback to be called once.
    cb = _.once(cb);

    var fileUri = path.join(params.options.pdfExportDir, 'image_binary_' + fileIdOrUrl);
    var localFile = fs.createWriteStream(fileUri);

    //Loading The Submission File From THe Database / MbaaS
    cacheSubmissionFile({
      fileId: fileIdOrUrl,
      connections: params.connections,
      options: params.options,
      submission: submission
    }, localFile, function(err, fileDetails) {
      if (err) {
        logger.error("cacheFiles: cacheSubmissionFile", {error: err});
      }
      fileDetails = fileDetails || {};
      fileDetails.url = "file://" + fileUri;
      fileDetails.fileId = fileIdOrUrl;

      logger.debug("cacheFiles: mergeSubmissionFiles fileDetails", fileDetails);

      return cb(err, _.omit(fileDetails, "stream"));
    });
  }, function(err, cachedFiles) {
    if (err) {
      return cb(buildErrorResponse({
        error: err,
        msg: "Error Cacheing Files For Submission Export",
        code: constants.ERROR_CODES.FH_FORMS_ERR_CODE_PDF_GENERATION,
        httpCode: 500
      }));
    }

    //No Errors, all files are now cached for the submission

    submission.formSubmittedAgainst =  populateSubmissionFileData({
      form: form,
      submissionFiles: cachedFiles,
      downloadUrl: params.options.downloadUrl,
      fileUriPath: params.options.fileUriPath,
      submission: submission
    });

    logger.debug("cacheFiles: mergeSubmissionFiles submission", submission);

    cb(undefined, submission);
  });
}
function mergeSubmissionFiles ( params , cb ) { logger . debug ( "cacheFiles: mergeSubmissionFiles" , params ) ; var submission = params . submission ; //No submission, cannot continue. if ( ! _ . isObject ( submission ) ) { return cb ( buildErrorResponse ( { err : new Error ( "Expected Submission Object To Cache Files For" ) , msg : "Error Exporting Submission As PDF" , httpCode : 500 } ) ) ; } var form = submission . formSubmittedAgainst ; var fileFieldDetails = _ . filter ( submission . formFields , function ( field ) { return isFileFieldType ( field . fieldId . type ) ; } ) ; fileFieldDetails = _ . flatten ( fileFieldDetails ) ; var fieldTypes = _ . groupBy ( fileFieldDetails , function ( field ) { return field . fieldId . type ; } ) ; //Files That Have To Be Loaded From Mbaas var mbaasTypes = _ . union ( fieldTypes . photo , fieldTypes . signature ) ; //Only Interested In The File Ids To Download var filesToDownload = _ . map ( mbaasTypes , function ( field ) { return _ . map ( field . fieldValues , function ( fieldValue ) { return fieldValue . groupId ; } ) ; } ) ; filesToDownload = _ . flatten ( filesToDownload ) ; logger . debug ( "cacheFiles: mergeSubmissionFiles filesToDownload" , filesToDownload ) ; //Now download all of the files.. async . mapSeries ( filesToDownload , function ( fileIdOrUrl , cb ) { //Only want the callback to be called once. cb = _ . once ( cb ) ; var fileUri = path . join ( params . options . pdfExportDir , 'image_binary_' + fileIdOrUrl ) ; var localFile = fs . createWriteStream ( fileUri ) ; //Loading The Submission File From THe Database / MbaaS cacheSubmissionFile ( { fileId : fileIdOrUrl , connections : params . connections , options : params . options , submission : submission } , localFile , function ( err , fileDetails ) { if ( err ) { logger . error ( "cacheFiles: cacheSubmissionFile" , { error : err } ) ; } fileDetails = fileDetails || { } ; fileDetails . url = "file://" + fileUri ; fileDetails . fileId = fileIdOrUrl ; logger . debug ( "cacheFiles: mergeSubmissionFiles fileDetails" , fileDetails ) ; return cb ( err , _ . omit ( fileDetails , "stream" ) ) ; } ) ; } , function ( err , cachedFiles ) { if ( err ) { return cb ( buildErrorResponse ( { error : err , msg : "Error Cacheing Files For Submission Export" , code : constants . ERROR_CODES . FH_FORMS_ERR_CODE_PDF_GENERATION , httpCode : 500 } ) ) ; } //No Errors, all files are now cached for the submission submission . formSubmittedAgainst = populateSubmissionFileData ( { form : form , submissionFiles : cachedFiles , downloadUrl : params . options . downloadUrl , fileUriPath : params . options . fileUriPath , submission : submission } ) ; logger . debug ( "cacheFiles: mergeSubmissionFiles submission" , submission ) ; cb ( undefined , submission ) ; } ) ; }
Caching any files needed for a submission to the file system.

@param params
- submission
- connections
- options
@param cb
@returns {*}
@private
Caching any files needed for a submission to the file system .