Fix output file deletion after exception thrown on write
For relative paths, it would not work as the FileSystemHelper would not allow deleting a file that's not part of its base folder.
This commit is contained in:
parent
23f8cc4f05
commit
6c9d4685bf
@ -13,15 +13,15 @@ use Box\Spout\Common\Exception\IOException;
|
|||||||
*/
|
*/
|
||||||
class FileSystemHelper
|
class FileSystemHelper
|
||||||
{
|
{
|
||||||
/** @var string Path of the base folder where all the I/O can occur */
|
/** @var string Real path of the base folder where all the I/O can occur */
|
||||||
protected $baseFolderPath;
|
protected $baseFolderRealPath;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $baseFolderPath The path of the base folder where all the I/O can occur
|
* @param string $baseFolderPath The path of the base folder where all the I/O can occur
|
||||||
*/
|
*/
|
||||||
public function __construct($baseFolderPath)
|
public function __construct($baseFolderPath)
|
||||||
{
|
{
|
||||||
$this->baseFolderPath = $baseFolderPath;
|
$this->baseFolderRealPath = realpath($baseFolderPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -124,9 +124,10 @@ class FileSystemHelper
|
|||||||
*/
|
*/
|
||||||
protected function throwIfOperationNotInBaseFolder($operationFolderPath)
|
protected function throwIfOperationNotInBaseFolder($operationFolderPath)
|
||||||
{
|
{
|
||||||
$isInBaseFolder = (strpos($operationFolderPath, $this->baseFolderPath) === 0);
|
$operationFolderRealPath = realpath($operationFolderPath);
|
||||||
|
$isInBaseFolder = (strpos($operationFolderRealPath, $this->baseFolderRealPath) === 0);
|
||||||
if (!$isInBaseFolder) {
|
if (!$isInBaseFolder) {
|
||||||
throw new IOException("Cannot perform I/O operation outside of the base folder: {$this->baseFolderPath}");
|
throw new IOException("Cannot perform I/O operation outside of the base folder: {$this->baseFolderRealPath}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -203,18 +203,6 @@ class GlobalFunctionsHelper
|
|||||||
return (strpos($path, 'zip://') === 0);
|
return (strpos($path, 'zip://') === 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Wrapper around global function dirname()
|
|
||||||
* @see dirname()
|
|
||||||
*
|
|
||||||
* @param string $filePath
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function dirname($filePath)
|
|
||||||
{
|
|
||||||
return dirname($filePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper around global function feof()
|
* Wrapper around global function feof()
|
||||||
* @see feof()
|
* @see feof()
|
||||||
|
@ -372,8 +372,8 @@ abstract class AbstractWriter implements WriterInterface
|
|||||||
|
|
||||||
// remove output file if it was created
|
// remove output file if it was created
|
||||||
if ($this->globalFunctionsHelper->file_exists($this->outputFilePath)) {
|
if ($this->globalFunctionsHelper->file_exists($this->outputFilePath)) {
|
||||||
$outputFolder = $this->globalFunctionsHelper->dirname($this->outputFilePath);
|
$outputFolderPath = dirname($this->outputFilePath);
|
||||||
$fileSystemHelper = new FileSystemHelper($outputFolder);
|
$fileSystemHelper = new FileSystemHelper($outputFolderPath);
|
||||||
$fileSystemHelper->deleteFile($this->outputFilePath);
|
$fileSystemHelper->deleteFile($this->outputFilePath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,7 +75,7 @@ class FileSystemHelper extends \Box\Spout\Common\Helper\FileSystemHelper
|
|||||||
*/
|
*/
|
||||||
protected function createRootFolder()
|
protected function createRootFolder()
|
||||||
{
|
{
|
||||||
$this->rootFolder = $this->createFolder($this->baseFolderPath, uniqid('ods'));
|
$this->rootFolder = $this->createFolder($this->baseFolderRealPath, uniqid('ods'));
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ class FileSystemHelper extends \Box\Spout\Common\Helper\FileSystemHelper
|
|||||||
*/
|
*/
|
||||||
protected function createRootFolder()
|
protected function createRootFolder()
|
||||||
{
|
{
|
||||||
$this->rootFolder = $this->createFolder($this->baseFolderPath, uniqid('xlsx', true));
|
$this->rootFolder = $this->createFolder($this->baseFolderRealPath, uniqid('xlsx', true));
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user