src/managers/image-generator.js
/**
   * Downloads a file if it's content length is below a specified limit in bytes.
   * @private
   * @param  {string}  url           the URL of the remote file to be downloaded
   * @param  {string}  filePath      the local path to save the file into
   * @param  {Number}  fileSizeLimit the size limit in bytes
   * @return {Promise}               nothing
   */
Downloads a file if it's content length is below a specified limit in bytes.
Downloads a file if it ' s content length is below a specified limit in bytes .
async downloadFileWithSizeLimit(url, filePath, fileSizeLimit) {
    this.context.log.d(`Download file ${url} to ${filePath}`);
    const downloadFunc = (url, filePath, resolve, reject) => {
      request({
        url: url,
        method: 'HEAD'
      }, (err, headRes) => {
        if (err) {
          this.context.log.e(`HEAD request failed for ${url}`);
          reject(err.message);
          return;
        }
        if (headRes.headers['content-length'] && headRes.headers['content-length'] > fileSizeLimit) {
          this.context.log.e('File size exceeds limit (' + headRes.headers['content-length'] + ')');
          reject('File size exceeds limit (' + headRes.headers['content-length'] + ')');
        } else {
          if (headRes.headers['content-length']) {
            this.context.log.v(`File size ${url} is ${headRes.headers['content-length']}`);
          }
          const file = fs.createWriteStream(filePath);
          const res = request({ url: url });
          let size = 0;
          res.on('response', (response) => {
            if (response.statusCode !== 200) {
              reject('Response status was ' + response.statusCode);
            }
          });
          res.on('error', (err) => {
            this.deleteFile(filePath);
            reject(err.message);
          });
          res.on('data', (data) => {
            size += data.length;
            if (size > fileSizeLimit) {
              this.context.log.e('Resource stream exceeded limit (' + size + ')');
              res.abort(); 
              this.deleteFile(filePath);
              reject('File size  exceeds limit');
            }
          }).pipe(file);
          file.on('error', (err) => { 
            this.context.log.e(`ImageGenerator: File error ${err}`);
            this.deleteFile(filePath);
            reject(err.message);
          });
          file.on('finish', () => file.close(resolve));
        }
      });
    };
    return new Promise((resolve, reject) => downloadFunc(url, filePath, resolve, reject));
  }
async downloadFileWithSizeLimit ( url , filePath , fileSizeLimit ) { this . context . log . d ( ` ${ url } ${ filePath } ` ) ; const downloadFunc = ( url , filePath , resolve , reject ) => { request ( { url : url , method : 'HEAD' } , ( err , headRes ) => { if ( err ) { this . context . log . e ( ` ${ url } ` ) ; reject ( err . message ) ; return ; } if ( headRes . headers [ 'content-length' ] && headRes . headers [ 'content-length' ] > fileSizeLimit ) { this . context . log . e ( 'File size exceeds limit (' + headRes . headers [ 'content-length' ] + ')' ) ; reject ( 'File size exceeds limit (' + headRes . headers [ 'content-length' ] + ')' ) ; } else { if ( headRes . headers [ 'content-length' ] ) { this . context . log . v ( ` ${ url } ${ headRes . headers [ 'content-length' ] } ` ) ; } const file = fs . createWriteStream ( filePath ) ; const res = request ( { url : url } ) ; let size = 0 ; res . on ( 'response' , ( response ) => { if ( response . statusCode !== 200 ) { reject ( 'Response status was ' + response . statusCode ) ; } } ) ; res . on ( 'error' , ( err ) => { this . deleteFile ( filePath ) ; reject ( err . message ) ; } ) ; res . on ( 'data' , ( data ) => { size += data . length ; if ( size > fileSizeLimit ) { this . context . log . e ( 'Resource stream exceeded limit (' + size + ')' ) ; res . abort ( ) ; this . deleteFile ( filePath ) ; reject ( 'File size  exceeds limit' ) ; } } ) . pipe ( file ) ; file . on ( 'error' , ( err ) => { this . context . log . e ( ` ${ err } ` ) ; this . deleteFile ( filePath ) ; reject ( err . message ) ; } ) ; file . on ( 'finish' , ( ) => file . close ( resolve ) ) ; } } ) ; } ; return new Promise ( ( resolve , reject ) => downloadFunc ( url , filePath , resolve , reject ) ) ; }
Downloads a file if it's content length is below a specified limit in bytes.
Downloads a file if it ' s content length is below a specified limit in bytes .
// Abort the response (close and cleanup the stream) // Handle errors