diff --git a/src/Feeds/Target/File.php b/src/Feeds/Target/File.php index 3d03b479..e19fa428 100644 --- a/src/Feeds/Target/File.php +++ b/src/Feeds/Target/File.php @@ -14,6 +14,7 @@ use Drupal\feeds\Exception\EmptyFeedException; use Drupal\feeds\Exception\TargetValidationException; use Drupal\feeds\FieldTargetDefinition; use GuzzleHttp\ClientInterface; +use GuzzleHttp\TransferStats; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -236,7 +237,17 @@ class File extends EntityReference { * In case the file extension is not valid. */ protected function getFileName($url) { + // Some APIs return URLs that don't represent a file but redirect the user + // to a file. We make an HTTP request here and update the value of $url + // whenever a redirect takes place. + $this->client->get($url, [ + 'on_stats' => function (TransferStats $stats) use (&$url) { + $url = (string) $stats->getEffectiveUri(); + }, + ]); + $filename = trim(\Drupal::service('file_system')->basename($url), " \t\n\r\0\x0B."); + list($filename) = explode('?', $filename); $extension = substr($filename, strrpos($filename, '.') + 1); if (!preg_grep('/' . $extension . '/i', $this->fileExtensions)) {