Skip to content
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
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)

### Added

- Listener for removing temporary files which were created during uploading _(must be enabled manually for existing applications)_ ([#477](https://github.com/laravel/octane/pull/477))

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

### Changed
Expand Down
3 changes: 2 additions & 1 deletion config/octane.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Laravel\Octane\Listeners\EnsureUploadedFilesAreValid;
use Laravel\Octane\Listeners\EnsureUploadedFilesCanBeMoved;
use Laravel\Octane\Listeners\FlushTemporaryContainerInstances;
use Laravel\Octane\Listeners\FlushUploadedFiles;
use Laravel\Octane\Listeners\ReportException;
use Laravel\Octane\Listeners\StopWorkerIfNecessary;
use Laravel\Octane\Octane;
Expand Down Expand Up @@ -78,7 +79,7 @@
],

RequestTerminated::class => [
//
// FlushUploadedFiles::class,
],

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

namespace Laravel\Octane\Listeners;

use SplFileInfo;

class FlushUploadedFiles
{
/**
* Handle the event.
*
* @param mixed $event
* @return void
*/
public function handle($event): void
{
foreach ($event->request->files->all() as $file) {
if (! $file instanceof SplFileInfo ||
! is_string($path = $file->getRealPath())) {
continue;
}

clearstatcache(true, $path);

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

namespace Laravel\Octane\Listeners;

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

/**
* @covers \Laravel\Octane\Listeners\FlushUploadedFiles
*/
class FlushUploadedFilesTest 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 FlushUploadedFiles)->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);
}
}
}
}
}