Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into fix/suppressed-config
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-vessey committed Jul 25, 2024
2 parents c9b25d9 + f0a0fc5 commit 4d0730c
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/Plugin/migrate/source/Spreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
use Drupal\migrate\Plugin\MigrationInterface;
use OpenSpout\Common\Exception\IOException;
use OpenSpout\Reader\Common\Creator\ReaderFactory;
use OpenSpout\Reader\CSV\Reader as CSVReader;
use OpenSpout\Reader\ReaderInterface;
Expand All @@ -21,8 +22,8 @@
* the same available configuration keys:
* - file: The path to the source file. The path can be either relative to
* Drupal root but it can be a also an absolute reference such as a stream
* wrapper; however, a .ods or .xlsx _must_ be able to be realpath'd to a
* real location, at present.
* wrapper. If an .ods or .xlsx can not be realpath'd, we will handle spooling
* to a temp file.
* - worksheet: The name of the worksheet to read from.
* - header_row: The row where the header is placed. If the table header is on
* the first row, this configuration should be 1. The header cell values will
Expand Down Expand Up @@ -81,6 +82,13 @@ class Spreadsheet extends SourcePluginBase implements ConfigurableInterface, Con
*/
protected FileSystemInterface $fileSystem;

/**
* File path under which non-realpath-able URIs might be spooled for use.
*
* @var string
*/
protected string $spoolFile;

/**
* Constructor.
*/
Expand Down Expand Up @@ -213,10 +221,24 @@ protected function openReader() {
$reader->open($realpath);
}
else {
// Real-path of stream wrappers does not quite make sense, so allow
// an opportunity for files from stream wrappers to be processed.
$reader = ReaderFactory::createFromFile($path);
$reader->open($path);
try {
// Real-path of stream wrappers does not quite make sense, so allow
// an opportunity for files from stream wrappers to be processed.
$reader = ReaderFactory::createFromFile($path);
$reader->open($path);
}
catch (IOException $e) {
// Possibly an exception such as:
// "OpenSpout\Common\Exception\IOException: Could not open
// {scheme}://{filename}.ods for reading! Stream wrapper used is not
// supported for this type of file."
// So let's try to spool to a temp file to remove the stream wrapper
// from the equation.
$spooled = $this->fileSystem->copy($path, sys_get_temp_dir());
$this->spoolFile = $this->fileSystem->realpath($spooled);
$reader = ReaderFactory::createFromFile($this->spoolFile);
$reader->open($this->spoolFile);
}
}
$this->reader = $reader;
}
Expand All @@ -231,6 +253,10 @@ protected function closeReader() {
if ($this->reader !== NULL) {
$this->reader->close();
unset($this->reader);
if (isset($this->spoolFile)) {
$this->fileSystem->unlink($this->spoolFile);
unset($this->spoolFile);
}
}
}

Expand Down

0 comments on commit 4d0730c

Please sign in to comment.