-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Listener for removing temporary files, which was created during uploa…
…ding (#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]>
- Loading branch information
1 parent
dde0cbd
commit f4907e7
Showing
4 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |