function apiUploadHandler(req, res) {
  return fileFilterError => {

    // Status code will be used for known errors from the app, not for errors
    // from multer or unknown errors
    const abortUpload = (errors = []) => {
      cleanupFiles(req);
      const errorMessages = errors.map(error => {
        const rv = {};
        rv.internalMessage = error.message;
        if (error instanceof ReportedError)
          rv.displayMessage = req.__(...error.getEscapedUserMessageArray());
        return rv;
      });
      api.error(req, res, errorMessages);
    };

    if (fileFilterError)
      return abortUpload([fileFilterError]);

    if (!req.files.length)
      return abortUpload([new Error('No files received.')]);

    const validationErrors = validateAllMetadata(req.files, req.body);
    if (validationErrors.length)
      return abortUpload(validationErrors);

    validateFiles(req.files)
      .then(fileTypes => getFileRevs(req.files, fileTypes, req.user, ['upload', 'upload-via-api']))
      .then(fileRevs => addMetadata(req.files, fileRevs, req.body))
      .then(completeUploads)
      .then(fileRevs => Promise.all(fileRevs.map(fileRev => fileRev.save())))
      .then(fileRevs => reportUploadSuccess(req, res, fileRevs))
      .catch(error => abortUpload([error]));
  };
}
/**
 * The main handler for processing upload attempts via the API. Kicks in after
 * the basic MIME check within the Multer middleware. Handles metadata
 * validation & creation of "File" revisions.
 *
 * @param {IncomingMessage} req
 *  Express request
 * @param {ServerResponse} res
 *  Express response
 * @returns {Function}
 *  callback invoked by the Multer middleware
 * @memberof APIUploadHandler
 */
The main handler for processing upload attempts via the API. Kicks in after
the basic MIME check within the Multer middleware. Handles metadata
validation & creation of "File" revisions.

@param {IncomingMessage} req
Express request
@param {ServerResponse} res
Express response
@returns {Function}
callback invoked by the Multer middleware
@memberof APIUploadHandler
The main handler for processing upload attempts via the API . Kicks in after the basic MIME check within the Multer middleware . Handles metadata validation & creation of " File " revisions . @param { IncomingMessage } req Express request @param { ServerResponse } res Express response @returns { Function } callback invoked by the Multer middleware @memberof APIUploadHandler
function apiUploadHandler(req, res) {
  return fileFilterError => {
    const abortUpload = (errors = []) => {
      cleanupFiles(req);
      const errorMessages = errors.map(error => {
        const rv = {};
        rv.internalMessage = error.message;
        if (error instanceof ReportedError)
          rv.displayMessage = req.__(...error.getEscapedUserMessageArray());
        return rv;
      });
      api.error(req, res, errorMessages);
    };
    if (fileFilterError)
      return abortUpload([fileFilterError]);
    if (!req.files.length)
      return abortUpload([new Error('No files received.')]);
    const validationErrors = validateAllMetadata(req.files, req.body);
    if (validationErrors.length)
      return abortUpload(validationErrors);
    validateFiles(req.files)
      .then(fileTypes => getFileRevs(req.files, fileTypes, req.user, ['upload', 'upload-via-api']))
      .then(fileRevs => addMetadata(req.files, fileRevs, req.body))
      .then(completeUploads)
      .then(fileRevs => Promise.all(fileRevs.map(fileRev => fileRev.save())))
      .then(fileRevs => reportUploadSuccess(req, res, fileRevs))
      .catch(error => abortUpload([error]));
  };
}
function apiUploadHandler ( req , res ) { return fileFilterError => { const abortUpload = ( errors = [ ] ) => { cleanupFiles ( req ) ; const errorMessages = errors . map ( error => { const rv = { } ; rv . internalMessage = error . message ; if ( error instanceof ReportedError ) rv . displayMessage = req . __ ( ... error . getEscapedUserMessageArray ( ) ) ; return rv ; } ) ; api . error ( req , res , errorMessages ) ; } ; if ( fileFilterError ) return abortUpload ( [ fileFilterError ] ) ; if ( ! req . files . length ) return abortUpload ( [ new Error ( 'No files received.' ) ] ) ; const validationErrors = validateAllMetadata ( req . files , req . body ) ; if ( validationErrors . length ) return abortUpload ( validationErrors ) ; validateFiles ( req . files ) . then ( fileTypes => getFileRevs ( req . files , fileTypes , req . user , [ 'upload' , 'upload-via-api' ] ) ) . then ( fileRevs => addMetadata ( req . files , fileRevs , req . body ) ) . then ( completeUploads ) . then ( fileRevs => Promise . all ( fileRevs . map ( fileRev => fileRev . save ( ) ) ) ) . then ( fileRevs => reportUploadSuccess ( req , res , fileRevs ) ) . catch ( error => abortUpload ( [ error ] ) ) ; } ; }
The main handler for processing upload attempts via the API.
The main handler for processing upload attempts via the API .
// Status code will be used for known errors from the app, not for errors // from multer or unknown errors