media-helper.js
function toBuffer (media) {
  return new Promise((resolve, reject) => {
    if (isURL(media)) {
      toBase64(media)
      .then(data => {
        toBuffer(data)
        .then(data => resolve(data))
        .catch(error => reject(error))
      })
      .catch(error => reject(error))
    } else if (isBase64(media)) {
      try {
        resolve(Buffer.from(media))
      } catch (ex) {
        reject(ex)
      }
    } else if (isFile(media)) {
      fs.readFile(media, (err, data) => {
        err && reject(err)
        resolve(data)
      })
    } else {
      reject('Error: toBuffer(): argument must be file, url or file')
    }
  })
}
function toBuffer (media) {
  return new Promise((resolve, reject) => {
    if (isURL(media)) {
      toBase64(media)
      .then(data => {
        toBuffer(data)
        .then(data => resolve(data))
        .catch(error => reject(error))
      })
      .catch(error => reject(error))
    } else if (isBase64(media)) {
      try {
        resolve(Buffer.from(media))
      } catch (ex) {
        reject(ex)
      }
    } else if (isFile(media)) {
      fs.readFile(media, (err, data) => {
        err && reject(err)
        resolve(data)
      })
    } else {
      reject('Error: toBuffer(): argument must be file, url or file')
    }
  })
}
function toBuffer ( media ) { return new Promise ( ( resolve , reject ) => { if ( isURL ( media ) ) { toBase64 ( media ) . then ( data => { toBuffer ( data ) . then ( data => resolve ( data ) ) . catch ( error => reject ( error ) ) } ) . catch ( error => reject ( error ) ) } else if ( isBase64 ( media ) ) { try { resolve ( Buffer . from ( media ) ) } catch ( ex ) { reject ( ex ) } } else if ( isFile ( media ) ) { fs . readFile ( media , ( err , data ) => { err && reject ( err ) resolve ( data ) } ) } else { reject ( 'Error: toBuffer(): argument must be file, url or file' ) } } ) }
Reads an image from file or url and convert it to Buffer
@param {media} media - file, url or path
@returns {Promise}
Reads an image from file or url and convert it to Buffer