Skip to content

Commit

Permalink
fix: cleanup temporary files (#37)
Browse files Browse the repository at this point in the history
* fix: temporary filesystem clogging

* refactor: remove sf4 compatibility and deprecate obsolete parameter

* fix: Keep underlying file instance in UploadedBase64EncodedFile to avoid its phyisical file will be cleaned up
  • Loading branch information
hshn committed Dec 24, 2023
1 parent 7bfa5bf commit 54fa814
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/HttpFoundation/File/Base64EncodedFile.php
Expand Up @@ -90,4 +90,11 @@ private function restoreToTemporary(string $encoded, $strict = true): string

return $path;
}

public function __destruct()
{
if (file_exists($this->getPathname())) {
unlink($this->getPathname());
}
}
}
15 changes: 8 additions & 7 deletions src/HttpFoundation/File/UploadedBase64EncodedFile.php
Expand Up @@ -10,6 +10,8 @@
*/
class UploadedBase64EncodedFile extends UploadedFile
{
private Base64EncodedFile $underlying;

/**
* @param Base64EncodedFile $file
* @param string $originalName
Expand All @@ -18,13 +20,12 @@ class UploadedBase64EncodedFile extends UploadedFile
*/
public function __construct(Base64EncodedFile $file, $originalName = '', $mimeType = null, $size = null)
{
$method = new \ReflectionMethod(parent::class, '__construct');
$num = $method->getNumberOfParameters();
if (5 === $num) {
parent::__construct($file->getPathname(), $originalName ?: $file->getFilename(), $mimeType, null, true);
} else {
// Symfony 4 compatible
parent::__construct($file->getPathname(), $originalName ?: $file->getFilename(), $mimeType, $size, null, true);
parent::__construct($file->getPathname(), $originalName ?: $file->getFilename(), $mimeType, null, true);

if ($size !== null) {
trigger_error('The $size argument is removed since Symfony 5.0 and we will removed it in 6.0.', E_USER_DEPRECATED);
}

$this->underlying = $file;
}
}
17 changes: 17 additions & 0 deletions tests/HttpFoundation/File/Base64EncodedFileTest.php
Expand Up @@ -40,4 +40,21 @@ public function testCreateInstanceWithData()
$this->assertEquals($rawStrings, file_get_contents($file->getPathname()));
$this->assertEquals('txt', pathinfo($file->getPathname(), PATHINFO_EXTENSION));
}

/**
* @test
*/
public function testCleanupTemporaryFilesOnDestruction()
{
$rawStrings = 'symfony2';
$file = new Base64EncodedFile(base64_encode($rawStrings));

$pathname = $file->getPathname();

$this->assertTrue(file_exists($pathname));

unset($file);

$this->assertFalse(file_exists($pathname));
}
}
16 changes: 16 additions & 0 deletions tests/HttpFoundation/File/UploadedBase64EncodedFileTest.php
Expand Up @@ -67,6 +67,22 @@ public function testGetError()
$this->assertEquals(UPLOAD_ERR_OK, $file->getError());
}

/**
* @test
*/
public function testCleanupTemporaryFilesOnDestruction()
{
$file = $this->getFile('symfony');

$pathname = $file->getPathname();

$this->assertTrue(file_exists($pathname));

unset($file);

$this->assertFalse(file_exists($pathname));
}

/**
* @param string $content
* @param string $filename
Expand Down

0 comments on commit 54fa814

Please sign in to comment.