Skip to content
Snippets Groups Projects

Allow a remote file as the Source path

2 files
+ 52
0
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -197,6 +197,10 @@ class CSV extends SourcePluginBase implements ConfigurableInterface {
* @throws \League\Csv\Exception
*/
public function initializeIterator() {
if (preg_match('/^https?:\/\//', $this->configuration['path'])) {
$this->configuration['path'] = $this->downloadRemoteFileToTmp($this->configuration['path']);
}
$header = $this->getReader()->getHeader();
if ($this->configuration['fields']) {
// If there is no header record, we need to flip description and name so
@@ -292,4 +296,51 @@ class CSV extends SourcePluginBase implements ConfigurableInterface {
return Reader::createFromStream($csv);
}
/**
* Download a remote file to a temporary file to be streamed by the Reader.
*
* @param $remote_path
* The remote path to download.
*
* @return string
* The path to the temporary file.
*/
protected function downloadRemoteFileToTmp($remote_path): string {
// Get the last modified time of the remote file.
$ch = curl_init($remote_path);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FILETIME, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_exec($ch);
$remote_last_modified = curl_getinfo($ch, CURLINFO_FILETIME);
curl_close($ch);
if ($remote_last_modified === -1) {
throw new \RuntimeException(sprintf('Could not retrieve the last modified time for "%s".', $remote_path));
}
$file_name = pathinfo($remote_path, PATHINFO_FILENAME);
$tmp_csv_file = sys_get_temp_dir() . '/csv_' . $file_name . '_' . $remote_last_modified;
if (!file_exists($tmp_csv_file)) {
$fp = fopen($tmp_csv_file, 'w+');
$ch = curl_init($remote_path);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
$result = curl_exec($ch);
if ($result === FALSE) {
throw new \RuntimeException(sprintf('File "%s" could not be downloaded. cURL error: %s', $remote_path, curl_error($ch)));
}
curl_close($ch);
fclose($fp);
}
return $tmp_csv_file;
}
}
Loading