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

Listener for removing temporary files, which was created during uploading #477

Merged
merged 8 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Listener for removing temporary files, which was created during uploa…
…ding
  • Loading branch information
tarampampam committed Feb 10, 2022
commit d31d112e8591a642464cd369618bcce92b093576
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased](https://github.com/laravel/octane/compare/v1.2.1...1.x)

### Changed

- Removes temporary files, which was created during uploading _(should be enabled manually)_

## [v1.2.1](https://github.com/laravel/octane/compare/v1.2.1...v1.2.1) - 2022-02-08

### Changed
Expand Down
2 changes: 1 addition & 1 deletion config/octane.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
],

RequestTerminated::class => [
//
// \Laravel\Octane\Listeners\CleanupUploadedFiles::class,
],

TaskReceived::class => [
Expand Down
32 changes: 32 additions & 0 deletions src/Listeners/CleanupUploadedFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Laravel\Octane\Listeners;

/**
* @link https://github.com/spiral/roadrunner-laravel/issues/84
*/
class CleanupUploadedFiles
{
/**
* Handle the event.
*
* @param mixed $event
* @return void
*/
public function handle($event): void
{
foreach ($event->request->files->all() as $file) {
if ($file instanceof \SplFileInfo) {
if (\is_string($path = $file->getRealPath())) {
\clearstatcache(true, $path);

if (\is_file($path)) {
\unlink($path);
}
}
}
}
}
}
52 changes: 52 additions & 0 deletions tests/Listeners/CleanupUploadedFilesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Laravel\Octane\Listeners;

use Illuminate\Support\Str;
use Laravel\Octane\Tests\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* @covers \Laravel\Octane\Listeners\CleanupUploadedFiles
*/
class CleanupUploadedFilesTest extends TestCase
{
public function test_files_removed()
{
try {
[$file1path, $file2path, $file3path] = [
\tempnam($tmpDir = \sys_get_temp_dir(), $prefix = 'unit-'),
\tempnam($tmpDir, $prefix),
\tempnam($tmpDir, $prefix),
];

($request = Request::create('http://127.0.0.1:123/foo'))->files->add([
new UploadedFile($file1path, Str::random()),
new UploadedFile($file2path, Str::random()),
new UploadedFile($file3path, Str::random()),
]);

$this->assertTrue(\rename($file3path, $file3newPath = $file3path . Str::random()));

$this->assertFileExists($file1path);
$this->assertFileExists($file2path);
$this->assertFileExists($file3newPath);

$event = new \stdClass();
$event->request = $request;

(new CleanupUploadedFiles)->handle($event);

$this->assertFileDoesNotExist($file1path);
$this->assertFileDoesNotExist($file2path);
$this->assertFileExists($file3newPath); // still exists
} finally {
foreach ([$file1path, $file2path, $file3newPath] as $fileName) {
if (\is_file($fileName)) {
\unlink($fileName);
}
}
}
}
}