diff --git a/src/Feeds/Target/File.php b/src/Feeds/Target/File.php index 3d03b479..e433a17d 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\Exception\RequestException; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -179,7 +180,14 @@ class File extends EntityReference { throw new EmptyFeedException('The given file url is empty.'); } - // Perform a lookup agains the value using the configured reference method. + // Filename from redirect. + if($this->configuration['use_redirected_filename']) { + $this->client->get($value,['on_stats' => function ($stats) use (&$value) { + $value = $stats->getEffectiveUri()->__toString(); + }]); + } + + // Perform a lookup against the value using the configured reference method. if (FALSE !== ($fid = $this->findEntity($value, $this->configuration['reference_by']))) { return $fid; } @@ -236,7 +244,9 @@ class File extends EntityReference { * In case the file extension is not valid. */ protected function getFileName($url) { + $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)) { @@ -262,14 +272,17 @@ class File extends EntityReference { * In case the file could not be downloaded. */ protected function getContent($url) { - $response = $this->client->request('GET', $url); - if ($response->getStatusCode() >= 400) { - $args = [ - '%url' => $url, - '@code' => $response->getStatusCode(), - ]; - throw new TargetValidationException($this->t('Download of %url failed with code @code.', $args)); + try { + $response = $this->client->request('GET', $url); + }catch (RequestException $e) { + if ($e->hasResponse()) { + $args = [ + '%url' => $url, + '@code' => $e->getResponse()->getStatusCode(), + ]; + throw new TargetValidationException($this->t('Download of %url failed with code @code.', $args)); + } } return (string) $response->getBody(); @@ -279,7 +292,10 @@ class File extends EntityReference { * {@inheritdoc} */ public function defaultConfiguration() { - return ['existing' => FileSystemInterface::EXISTS_ERROR] + parent::defaultConfiguration(); + return [ + 'existing' => FileSystemInterface::EXISTS_ERROR, + 'use_redirected_filename' => '', + ] + parent::defaultConfiguration(); } /** @@ -302,6 +318,13 @@ class File extends EntityReference { '#default_value' => $this->configuration['existing'], ]; + $form['use_redirected_filename'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Use Redirected Filename'), + '#description' => 'Following redirects to retrieve filenames will slow down import.', + '#default_value' => $this->configuration['use_redirected_filename'], + ]; + return $form; }