Skip to content

Commit

Permalink
fix: auto remove temp files
Browse files Browse the repository at this point in the history
  • Loading branch information
aVadim483 committed Jun 7, 2024
1 parent 296487e commit 8cd5b9d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 67 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"xls",
"xlsx",
"export",
"spreadsheet",
"ms office",
"office 2007"
"spreadsheet",
"ms office",
"office 2007"
],
"type": "library",
"homepage": "https://github.com/aVadim483/fast-excel-writer",
Expand Down
7 changes: 4 additions & 3 deletions src/FastExcelWriter/Excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1490,10 +1490,10 @@ public function save(?string $fileName = null, ?bool $overWrite = true): bool

if ($this->writer->saveToFile($fileName, $overWrite, $this->getMetadata())) {
$this->saved = true;
return true;
}
$this->writer->removeFiles();

return false;
return $this->saved;
}

/**
Expand All @@ -1503,7 +1503,7 @@ public function save(?string $fileName = null, ?bool $overWrite = true): bool
*/
public function download(string $name = null)
{
$tmpFile = $this->writer->makeTempFile();
$tmpFile = $this->writer->makeTempFileName(uniqid('xlsx_writer_'));
$this->save($tmpFile);
if (!$name) {
$name = basename($tmpFile) . '.xlsx';
Expand All @@ -1520,6 +1520,7 @@ public function download(string $name = null)
header('Content-Disposition: attachment; filename="' . $name . '"');

readfile($tmpFile);
unlink($tmpFile);
}

/**
Expand Down
20 changes: 10 additions & 10 deletions src/FastExcelWriter/Writer/FileWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public function __construct(string $filename, ?string $openFlags = 'wb', ?bool $
$this->checkUtf8 = $checkUtf8;
}

/**
*
*/
public function __destruct()
{
if ($this->buffer || $this->fd) {
$this->close();
}
}

/**
* @return string
*/
Expand Down Expand Up @@ -219,16 +229,6 @@ public function close()
}
}

/**
*
*/
public function __destruct()
{
if ($this->buffer || $this->fd) {
$this->close();
}
}


public function getFileResource()
{
Expand Down
90 changes: 39 additions & 51 deletions src/FastExcelWriter/Writer/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,15 @@ public function __construct(?array $options = [])
if (!empty($options['temp_dir'])) {
$this->setTempDir($options['temp_dir']);
}

register_shutdown_function([$this, 'removeFiles']);
}

/**
* Writer destructor
*/
public function __destruct()
{
/* moved to saveToFile()
foreach ($this->buffers as $name => $buffer) {
if ($buffer) {
$buffer->close();
$this->buffers[$name] = null;
}
}
*/
$this->removeFiles();
}

Expand Down Expand Up @@ -297,28 +291,32 @@ public function setTempDir(?string $tempDir = '')
}

/**
* @return bool|string
* @param string $name
*
* @return false|string
*/
public function _x_makeTempFile($localName = null)
public function makeTempFileName(string $name)
{
if (!$this->tempDir) {
$tempDir = sys_get_temp_dir();
$filename = tempnam($tempDir, $this->tempFilePrefix);
if (!$filename) {
$filename = tempnam(getcwd(), $this->tempFilePrefix);
if (!is_writable($tempDir)) {
$tempDir = getcwd();
}
}
else {
$filename = tempnam($this->tempDir, $this->tempFilePrefix);
$tempDir = $this->tempDir;
}
$filename = $tempDir . '/' . $name . '.tmp';
if (touch($filename, time(), time()) && is_writable($filename)) {
return realpath($filename);
}
if ($filename) {
if ($localName) {
$this->tempFiles['zip'][$localName] = $filename;
else {
$error = 'Warning: tempdir ' . $tempDir . ' is not writeable';
if (!$this->tempDir) {
$error .= ', use ->setTempDir()';
}
$this->tempFiles['tmp'][] = $filename;
throw new Exception($error);
}

return $filename;
}

/**
Expand All @@ -329,40 +327,22 @@ public function _x_makeTempFile($localName = null)
*/
public function makeTempFile(?string $key = null, ?string $zipName = null)
{
if (!$this->tempDir) {
$tempDir = sys_get_temp_dir();
if (!is_writable($tempDir)) {
$tempDir = getcwd();
}
}
else {
$tempDir = $this->tempDir;
$filename = $this->makeTempFileName(uniqid($this->tempFilePrefix, true));
if (!is_file($filename)) {
throw new Exception('Cannot create temp file "' . $filename . '"');
}
$filename = $tempDir . '/' . uniqid($this->tempFilePrefix, true) . '.tmp';
if (touch($filename, time(), time()) && is_writable($filename)) {
$filename = realpath($filename);
if ($zipName) {
$this->tempFiles['zip'][$zipName] = $filename;
if (!$key) {
$key = $zipName;
}
}
if ($key) {
$this->tempFiles['tmp'][$key] = $filename;
}
else {
$this->tempFiles['tmp'][] = $filename;

if ($zipName) {
$this->tempFiles['zip'][$zipName] = $filename;
if (!$key) {
$key = $zipName;
}
}
else {
$error = 'Warning: tempdir ' . $tempDir . ' is not writeable';
if (!$this->tempDir) {
$error .= ', use ->setTempDir()';
}
throw new Exception($error);
if ($key) {
$this->tempFiles['tmp'][$key] = $filename;
}
if (!is_file($filename)) {
throw new Exception('Cannot create temp file "' . $filename . '"');
else {
$this->tempFiles['tmp'][] = $filename;
}

return $filename;
Expand Down Expand Up @@ -391,8 +371,15 @@ protected function removeTempFile(string $fileName)
/**
* @return void
*/
protected function removeFiles()
public function removeFiles()
{
foreach ($this->buffers as $name => $buffer) {
if ($buffer) {
$buffer->close();
$this->buffers[$name] = null;
}
}

if (!empty($this->tempFiles['tmp'])) {
foreach ($this->tempFiles['tmp'] as $tempFile) {
if (is_file($tempFile)) {
Expand Down Expand Up @@ -874,6 +861,7 @@ protected function _writeChartFile(string $entry, Chart $chart, array &$relation
{
$chartWriter = $this->makeChartWriter($this->makeTempFile(null, $entry));
$chartWriter->writeChartXml($chart);
$chartWriter->close();
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/FastExcelWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public function testExcelWriter0()
unlink($testFileName);
}

$tempDir = __DIR__ . '/tmp';
Excel::setTempDir($tempDir);
$excel = Excel::create();
$sheet = $excel->sheet();

Expand Down Expand Up @@ -143,6 +145,7 @@ public function testExcelWriter0()
$sheet->writeTo('E9', 'E9');

$this->excelReader = $this->saveCheckRead($excel, $testFileName);
$this->assertCount(0, glob($tempDir . '/*.*'));
$this->cells = $this->excelReader->readCells();

$this->assertEquals('A1', $this->cells['A1']);
Expand Down

0 comments on commit 8cd5b9d

Please sign in to comment.