lib/poster.js
function head(url, uploadOptions, redirectCount, callback) {
    var options = getRequestOptions('HEAD', url, uploadOptions.downloadHeaders, uploadOptions.downloadAgent);

    var downloadProtocol = getProtocol(url);

    var req = downloadProtocol.request(options, function(res) {
      try {
        if ((res.statusCode == 301) || (res.statusCode == 302)) {
          if (redirectCount >= uploadOptions.maxRedirects) {
            return callback('Redirect count reached. Aborting upload.');
          }
          
          var location = res.headers.location;
          if (location) {
            redirectCount++;
            var redirectUrl = parseUri(location);
            return head(redirectUrl, uploadOptions, redirectCount, callback);
          }
        }

        if ((res.statusCode < 200) || (res.statusCode >= 300)) {
          return callback('Invalid response from remote file server. statusCode: ' + res.statusCode);
        }
        
        var contentLength = parseInt(res.headers['content-length'], 10);
        if (isNaN(contentLength)) { //this shouldn't happen, but it does
          return callback('Remote web server returned an invalid content length');
        }

        if (!validateFileSize(uploadOptions.maxFileSize, contentLength)) {
          return callback('File is too large. maxFileSize: ' + uploadOptions.maxFileSize + ', content-length: ' + contentLength);
        }

        //can we bail out early?
        if (uploadOptions.downloadFileName) {
          return callback(null, url, contentLength, uploadOptions.downloadFileName);
        }

        //no download specified, attempt to parse one out
        var file, ext, mimeExt;
        
        var contentType = res.headers['content-type'].split(';')[0];

        //attempt to get the filename from the url
        file = sanitizeFileName(path.basename(url.pathname));
        if (file) {
          ext = path.extname(file);
          file = file.replace(ext, '');
          ext = ext.replace('.', '');
          if (ext) {
            mimeExt = mimetypes.extension(contentType);
            if (mimeExt) {
              if (ext.toLowerCase() !== '.' + mimeExt.toLowerCase()) {
                ext = mimeExt;
              }
            }
          }
        }

        //default file name if we couldn't parse one
        if (!file) { file = 'poster'; }

        //default file extension if we cannot find one (unlikely)
        if (!ext) {
          ext = 'unk';
          if (contentType) {
            mimeExt = mimetypes.extension(contentType);
            if (mimeExt) {
              ext = mimeExt;
            }
          }
        }

        return callback(null, url, contentLength, file + '.' + ext);
      }
      catch (e) {
        callback(e);
      }
    });
    req.on('error', function(e) {
      callback(e);
    });
    req.end();
  }
function head(url, uploadOptions, redirectCount, callback) {
    var options = getRequestOptions('HEAD', url, uploadOptions.downloadHeaders, uploadOptions.downloadAgent);

    var downloadProtocol = getProtocol(url);

    var req = downloadProtocol.request(options, function(res) {
      try {
        if ((res.statusCode == 301) || (res.statusCode == 302)) {
          if (redirectCount >= uploadOptions.maxRedirects) {
            return callback('Redirect count reached. Aborting upload.');
          }
          
          var location = res.headers.location;
          if (location) {
            redirectCount++;
            var redirectUrl = parseUri(location);
            return head(redirectUrl, uploadOptions, redirectCount, callback);
          }
        }

        if ((res.statusCode < 200) || (res.statusCode >= 300)) {
          return callback('Invalid response from remote file server. statusCode: ' + res.statusCode);
        }
        
        var contentLength = parseInt(res.headers['content-length'], 10);
        if (isNaN(contentLength)) { //this shouldn't happen, but it does
          return callback('Remote web server returned an invalid content length');
        }

        if (!validateFileSize(uploadOptions.maxFileSize, contentLength)) {
          return callback('File is too large. maxFileSize: ' + uploadOptions.maxFileSize + ', content-length: ' + contentLength);
        }

        //can we bail out early?
        if (uploadOptions.downloadFileName) {
          return callback(null, url, contentLength, uploadOptions.downloadFileName);
        }

        //no download specified, attempt to parse one out
        var file, ext, mimeExt;
        
        var contentType = res.headers['content-type'].split(';')[0];

        //attempt to get the filename from the url
        file = sanitizeFileName(path.basename(url.pathname));
        if (file) {
          ext = path.extname(file);
          file = file.replace(ext, '');
          ext = ext.replace('.', '');
          if (ext) {
            mimeExt = mimetypes.extension(contentType);
            if (mimeExt) {
              if (ext.toLowerCase() !== '.' + mimeExt.toLowerCase()) {
                ext = mimeExt;
              }
            }
          }
        }

        //default file name if we couldn't parse one
        if (!file) { file = 'poster'; }

        //default file extension if we cannot find one (unlikely)
        if (!ext) {
          ext = 'unk';
          if (contentType) {
            mimeExt = mimetypes.extension(contentType);
            if (mimeExt) {
              ext = mimeExt;
            }
          }
        }

        return callback(null, url, contentLength, file + '.' + ext);
      }
      catch (e) {
        callback(e);
      }
    });
    req.on('error', function(e) {
      callback(e);
    });
    req.end();
  }
function head ( url , uploadOptions , redirectCount , callback ) { var options = getRequestOptions ( 'HEAD' , url , uploadOptions . downloadHeaders , uploadOptions . downloadAgent ) ; var downloadProtocol = getProtocol ( url ) ; var req = downloadProtocol . request ( options , function ( res ) { try { if ( ( res . statusCode == 301 ) || ( res . statusCode == 302 ) ) { if ( redirectCount >= uploadOptions . maxRedirects ) { return callback ( 'Redirect count reached. Aborting upload.' ) ; } var location = res . headers . location ; if ( location ) { redirectCount ++ ; var redirectUrl = parseUri ( location ) ; return head ( redirectUrl , uploadOptions , redirectCount , callback ) ; } } if ( ( res . statusCode < 200 ) || ( res . statusCode >= 300 ) ) { return callback ( 'Invalid response from remote file server. statusCode: ' + res . statusCode ) ; } var contentLength = parseInt ( res . headers [ 'content-length' ] , 10 ) ; if ( isNaN ( contentLength ) ) { //this shouldn't happen, but it does return callback ( 'Remote web server returned an invalid content length' ) ; } if ( ! validateFileSize ( uploadOptions . maxFileSize , contentLength ) ) { return callback ( 'File is too large. maxFileSize: ' + uploadOptions . maxFileSize + ', content-length: ' + contentLength ) ; } //can we bail out early? if ( uploadOptions . downloadFileName ) { return callback ( null , url , contentLength , uploadOptions . downloadFileName ) ; } //no download specified, attempt to parse one out var file , ext , mimeExt ; var contentType = res . headers [ 'content-type' ] . split ( ';' ) [ 0 ] ; //attempt to get the filename from the url file = sanitizeFileName ( path . basename ( url . pathname ) ) ; if ( file ) { ext = path . extname ( file ) ; file = file . replace ( ext , '' ) ; ext = ext . replace ( '.' , '' ) ; if ( ext ) { mimeExt = mimetypes . extension ( contentType ) ; if ( mimeExt ) { if ( ext . toLowerCase ( ) !== '.' + mimeExt . toLowerCase ( ) ) { ext = mimeExt ; } } } } //default file name if we couldn't parse one if ( ! file ) { file = 'poster' ; } //default file extension if we cannot find one (unlikely) if ( ! ext ) { ext = 'unk' ; if ( contentType ) { mimeExt = mimetypes . extension ( contentType ) ; if ( mimeExt ) { ext = mimeExt ; } } } return callback ( null , url , contentLength , file + '.' + ext ) ; } catch ( e ) { callback ( e ) ; } } ) ; req . on ( 'error' , function ( e ) { callback ( e ) ; } ) ; req . end ( ) ; }
this function is kind of hacky, but since not all web servers support chunked
encoding and we DO NOT want to buffer any of the file we're downloading into memory,
this is how we get around this so we can calculate the content-length. while this
does indeed slow down the upload, it does save us some time if the file doesn't exist
on the remote web server of if maxFileSize is provided, we can bail out if it's too large
this function is kind of hacky but since not all web servers support chunked encoding and we DO NOT want to buffer any of the file we re downloading into memory this is how we get around this so we can calculate the content - length . while this does indeed slow down the upload it does save us some time if the file doesn t exist on the remote web server of if maxFileSize is provided we can bail out if it s too large