diff --git a/src/Plugin/migrate_plus/data_fetcher/File.php b/src/Plugin/migrate_plus/data_fetcher/File.php index 33e3cae..ca85619 100644 --- a/src/Plugin/migrate_plus/data_fetcher/File.php +++ b/src/Plugin/migrate_plus/data_fetcher/File.php @@ -4,6 +4,7 @@ namespace Drupal\migrate_plus\Plugin\migrate_plus\data_fetcher; use Drupal\migrate\MigrateException; use Drupal\migrate_plus\DataFetcherPluginBase; +use GuzzleHttp\Psr7\Response; /** * Retrieve data from a local path or general URL for migration. @@ -34,19 +35,39 @@ class File extends DataFetcherPluginBase { * {@inheritdoc} */ public function getResponse($url) { - $response = @file_get_contents($url); - if ($response === FALSE) { + // Try to start reading (only first 10 bytes) the file. + $content = $this->getFileContent($url, 0, 10); + if ($content === FALSE) { throw new MigrateException('file parser plugin: could not retrieve data from ' . $url); } - return $response; + return new Response(200, [], 'File is readable.'); } /** * {@inheritdoc} */ public function getResponseContent($url) { - $response = $this->getResponse($url); - return $response; + // Reads entire File into a string. + return $this->getFileContent($url); + } + + /** + * Gets the content of a file by given $url (path). + * + * @param string $url + * Name of the file to read. + * @param int $offset + * The offset where the reading starts on the original stream. + * Negative offsets count from the end of the stream. + * @param int|null $length + * Maximum length of data read. + * The default is to read until end of file is reached. + * + * @return string|false + * Returns the content of the file or false on failure. + */ + protected function getFileContent(string $url, int $offset = 0, int $length = NULL) { + return @file_get_contents($url, FALSE, NULL, $offset, $length); } }