Skip to content

Commit f4907e7

Browse files
Listener for removing temporary files, which was created during uploading (#477)
* Listener for removing temporary files, which was created during uploading * cs fixes * Changelog updated * Changelog updated * Changelog updated * add files * simplify code * rename again Co-authored-by: Taylor Otwell <[email protected]>
1 parent dde0cbd commit f4907e7

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

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

5+
### Added
6+
7+
- 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))
8+
59
## [v1.2.1](https://github.com/laravel/octane/compare/v1.2.1...v1.2.1) - 2022-02-08
610

711
### Changed

config/octane.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Laravel\Octane\Listeners\EnsureUploadedFilesAreValid;
1717
use Laravel\Octane\Listeners\EnsureUploadedFilesCanBeMoved;
1818
use Laravel\Octane\Listeners\FlushTemporaryContainerInstances;
19+
use Laravel\Octane\Listeners\FlushUploadedFiles;
1920
use Laravel\Octane\Listeners\ReportException;
2021
use Laravel\Octane\Listeners\StopWorkerIfNecessary;
2122
use Laravel\Octane\Octane;
@@ -78,7 +79,7 @@
7879
],
7980

8081
RequestTerminated::class => [
81-
//
82+
// FlushUploadedFiles::class,
8283
],
8384

8485
TaskReceived::class => [
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Laravel\Octane\Listeners;
4+
5+
use SplFileInfo;
6+
7+
class FlushUploadedFiles
8+
{
9+
/**
10+
* Handle the event.
11+
*
12+
* @param mixed $event
13+
* @return void
14+
*/
15+
public function handle($event): void
16+
{
17+
foreach ($event->request->files->all() as $file) {
18+
if (! $file instanceof SplFileInfo ||
19+
! is_string($path = $file->getRealPath())) {
20+
continue;
21+
}
22+
23+
clearstatcache(true, $path);
24+
25+
if (is_file($path)) {
26+
unlink($path);
27+
}
28+
}
29+
}
30+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Laravel\Octane\Listeners;
4+
5+
use Illuminate\Support\Str;
6+
use Laravel\Octane\Listeners\FlushUploadedFiles;
7+
use Laravel\Octane\Tests\TestCase;
8+
use Symfony\Component\HttpFoundation\File\UploadedFile;
9+
use Symfony\Component\HttpFoundation\Request;
10+
11+
/**
12+
* @covers \Laravel\Octane\Listeners\FlushUploadedFiles
13+
*/
14+
class FlushUploadedFilesTest extends TestCase
15+
{
16+
public function test_files_removed()
17+
{
18+
try {
19+
[$file1path, $file2path, $file3path] = [
20+
\tempnam($tmpDir = \sys_get_temp_dir(), $prefix = 'unit-'),
21+
\tempnam($tmpDir, $prefix),
22+
\tempnam($tmpDir, $prefix),
23+
];
24+
25+
($request = Request::create('http://127.0.0.1:123/foo'))->files->add([
26+
new UploadedFile($file1path, Str::random()),
27+
new UploadedFile($file2path, Str::random()),
28+
new UploadedFile($file3path, Str::random()),
29+
]);
30+
31+
$this->assertTrue(\rename($file3path, $file3newPath = $file3path.Str::random()));
32+
33+
$this->assertFileExists($file1path);
34+
$this->assertFileExists($file2path);
35+
$this->assertFileExists($file3newPath);
36+
37+
$event = new \stdClass();
38+
$event->request = $request;
39+
40+
(new FlushUploadedFiles)->handle($event);
41+
42+
$this->assertFileDoesNotExist($file1path);
43+
$this->assertFileDoesNotExist($file2path);
44+
$this->assertFileExists($file3newPath); // still exists
45+
} finally {
46+
foreach ([$file1path, $file2path, $file3newPath] as $fileName) {
47+
if (\is_file($fileName)) {
48+
\unlink($fileName);
49+
}
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)