You are here

protected static function Remote::parseHeader in FileField Sources 8

Parse cURL header and record the filename specified in Content-Disposition.

File

src/Plugin/FilefieldSource/Remote.php, line 240

Class

Remote
A FileField source plugin to allow downloading a file from a remote server.

Namespace

Drupal\filefield_sources\Plugin\FilefieldSource

Code

protected static function parseHeader(&$ch, $header) {
  if (preg_match('/Content-Disposition:.*?filename="(.+?)"/', $header, $matches)) {

    // Content-Disposition: attachment; filename="FILE NAME HERE".
    static::filename($matches[1]);
  }
  elseif (preg_match('/Content-Disposition:.*?filename=([^; ]+)/', $header, $matches)) {

    // Content-Disposition: attachment; filename=file.ext.
    $uri = trim($matches[1]);
    static::filename($uri);
  }
  elseif (preg_match('/Content-Type:[ ]*([a-z0-9_\\-]+\\/[a-z0-9_\\-]+)/i', $header, $matches)) {
    $mime_type = $matches[1];
    static::mimeExtension($mime_type);
  }

  // This is required by cURL.
  return strlen($header);
}