Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cleanup temporary files #37

Merged
merged 3 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/HttpFoundation/File/Base64EncodedFile.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading