This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da2fb4a
commit e260bef
Showing
4 changed files
with
126 additions
and
0 deletions.
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\RoadRunnerLaravel\Listeners; | ||
|
||
use Spiral\RoadRunnerLaravel\Events\Contracts\WithHttpRequest; | ||
|
||
/** | ||
* @link https://github.com/spiral/roadrunner-laravel/issues/84 | ||
*/ | ||
class CleanupUploadedFilesListener implements ListenerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handle($event): void | ||
{ | ||
if ($event instanceof WithHttpRequest) { | ||
foreach ($event->httpRequest()->files->all() as $file) { | ||
if ($file instanceof \SplFileInfo) { | ||
if (\is_string($path = $file->getRealPath())) { | ||
\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,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners; | ||
|
||
use Mockery as m; | ||
use Illuminate\Support\Str; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
use Spiral\RoadRunnerLaravel\Events\Contracts\WithHttpRequest; | ||
use Spiral\RoadRunnerLaravel\Listeners\CleanupUploadedFilesListener; | ||
|
||
/** | ||
* @covers \Spiral\RoadRunnerLaravel\Listeners\CleanupUploadedFilesListener | ||
*/ | ||
class CleanupUploadedFilesListenerTest extends AbstractListenerTestCase | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function testHandle(): void | ||
{ | ||
$tmp_dir = $this->createTemporaryDirectory(); | ||
|
||
$this->assertNotFalse( | ||
\file_put_contents( | ||
$file_1_path = $tmp_dir . DIRECTORY_SEPARATOR . ($file_1_name = Str::random()), | ||
Str::random(), | ||
) | ||
); | ||
|
||
$this->assertNotFalse( | ||
\file_put_contents( | ||
$file_2_path = $tmp_dir . DIRECTORY_SEPARATOR . ($file_2_name = Str::random()), | ||
Str::random(), | ||
) | ||
); | ||
|
||
$this->assertNotFalse( | ||
\file_put_contents( | ||
$file_3_path = $tmp_dir . DIRECTORY_SEPARATOR . ($file_3_name = Str::random()), | ||
Str::random(), | ||
) | ||
); | ||
|
||
$request = Request::create('http://127.0.0.1:123/foo'); | ||
|
||
$request->files->add([ | ||
new UploadedFile($file_1_path, $file_1_name), | ||
new UploadedFile($file_2_path, $file_2_name), | ||
new UploadedFile($file_3_path, $file_3_name), | ||
]); | ||
|
||
\rename($file_3_path, $file_3_new_path = $file_3_path . Str::random()); | ||
|
||
/** @var m\MockInterface|WithHttpRequest $event */ | ||
$event = m::mock(WithHttpRequest::class) | ||
->makePartial() | ||
->expects('httpRequest') | ||
->atLeast() | ||
->once() | ||
->andReturn($request) | ||
->getMock(); | ||
|
||
$this->assertFileExists($file_1_path); | ||
$this->assertFileExists($file_2_path); | ||
$this->assertFileExists($file_3_new_path); | ||
|
||
$this->listenerFactory()->handle($event); | ||
|
||
$this->assertFileDoesNotExist($file_1_path); | ||
$this->assertFileDoesNotExist($file_2_path); | ||
$this->assertFileExists($file_3_new_path); // still exists | ||
} | ||
|
||
/** | ||
* @return CleanupUploadedFilesListener | ||
*/ | ||
protected function listenerFactory(): CleanupUploadedFilesListener | ||
{ | ||
return new CleanupUploadedFilesListener(); | ||
} | ||
} |