lib/utils.js
/**
 * Encodes the given local file to base64 and returns the resulting string
 * or uploads it to a remote server using http/https or ftp protocols
 * if `remotePath` is set
 *
 * @param {string} localFile - The path to an existing local file
 * @param {?string} remotePath - The path to the remote location, where
 *                               this file should be uploaded
 * @param {?UploadOptions} uploadOptions - Set of upload options
 * @returns {string} Either an empty string if the upload was successful or
 * base64-encoded file representation if `remotePath` is falsy
 */
Encodes the given local file to base64 and returns the resulting string
or uploads it to a remote server using http/https or ftp protocols
if `remotePath` is set

@param {string} localFile - The path to an existing local file
@param {?string} remotePath - The path to the remote location, where
this file should be uploaded
@param {?UploadOptions} uploadOptions - Set of upload options
@returns {string} Either an empty string if the upload was successful or
base64-encoded file representation if `remotePath` is falsy
Encodes the given local file to base64 and returns the resulting string or uploads it to a remote server using http / https or ftp protocols if ` remotePath ` is set @param { string } localFile - The path to an existing local file @param { ?string } remotePath - The path to the remote location where this file should be uploaded @param { ?UploadOptions } uploadOptions - Set of upload options @returns { string } Either an empty string if the upload was successful or base64 - encoded file representation if ` remotePath ` is falsy
async function encodeBase64OrUpload (localFile, remotePath = null, uploadOptions = {}) {
  if (!await fs.exists(localFile)) {
    log.errorAndThrow(`The file at '${localFile}' does not exist or is not accessible`);
  }
  const {size} = await fs.stat(localFile);
  log.debug(`The size of the file is ${util.toReadableSizeString(size)}`);
  if (_.isEmpty(remotePath)) {
    const maxMemoryLimit = v8.getHeapStatistics().total_available_size / 2;
    if (size >= maxMemoryLimit) {
      log.info(`The file might be too large to fit into the process memory ` +
        `(${util.toReadableSizeString(size)} >= ${util.toReadableSizeString(maxMemoryLimit)}). ` +
        `Provide a link to a remote writable location for video upload ` +
        `(http(s) and ftp protocols are supported) if you experience Out Of Memory errors`);
    }
    const content = await fs.readFile(localFile);
    return content.toString('base64');
  }
  const remoteUrl = url.parse(remotePath);
  let options = {};
  const {user, pass, method} = uploadOptions;
  if (remoteUrl.protocol.startsWith('http')) {
    options = {
      url: remoteUrl.href,
      method: method || 'PUT',
      multipart: [{ body: _fs.createReadStream(localFile) }],
    };
    if (user && pass) {
      options.auth = {user, pass};
    }
  } else if (remoteUrl.protocol === 'ftp:') {
    options = {
      host: remoteUrl.hostname,
      port: remoteUrl.port || 21,
    };
    if (user && pass) {
      options.user = user;
      options.pass = pass;
    }
  }
  await net.uploadFile(localFile, remotePath, options);
  return '';
}
async function encodeBase64OrUpload ( localFile , remotePath = null , uploadOptions = { } ) { if ( ! await fs . exists ( localFile ) ) { log . errorAndThrow ( ` ${ localFile } ` ) ; } const { size } = await fs . stat ( localFile ) ; log . debug ( ` ${ util . toReadableSizeString ( size ) } ` ) ; if ( _ . isEmpty ( remotePath ) ) { const maxMemoryLimit = v8 . getHeapStatistics ( ) . total_available_size / 2 ; if ( size >= maxMemoryLimit ) { log . info ( ` ` + ` ${ util . toReadableSizeString ( size ) } ${ util . toReadableSizeString ( maxMemoryLimit ) } ` + ` ` + ` ` ) ; } const content = await fs . readFile ( localFile ) ; return content . toString ( 'base64' ) ; } const remoteUrl = url . parse ( remotePath ) ; let options = { } ; const { user , pass , method } = uploadOptions ; if ( remoteUrl . protocol . startsWith ( 'http' ) ) { options = { url : remoteUrl . href , method : method || 'PUT' , multipart : [ { body : _fs . createReadStream ( localFile ) } ] , } ; if ( user && pass ) { options . auth = { user , pass } ; } } else if ( remoteUrl . protocol === 'ftp:' ) { options = { host : remoteUrl . hostname , port : remoteUrl . port || 21 , } ; if ( user && pass ) { options . user = user ; options . pass = pass ; } } await net . uploadFile ( localFile , remotePath , options ) ; return '' ; }
Encodes the given local file to base64 and returns the resulting string
 or uploads it to a remote server using http/https or ftp protocols
 if `remotePath` is set
Encodes the given local file to base64 and returns the resulting string or uploads it to a remote server using http / https or ftp protocols if ` remotePath ` is set