Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Cleanup uploaded files (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarampampam authored Feb 10, 2022
1 parent da2fb4a commit e260bef
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].

## UNRELEASED

### Added

- Listener `CleanupUploadedFilesListener` for removing temporary files, which was created during uploading _(should be enabled manually for the `AfterLoopIterationEvent` event)_ [#84]

[#84]:https://github.com/spiral/roadrunner-laravel/issues/84

## v5.7.0

### Added
Expand Down
1 change: 1 addition & 0 deletions config/roadrunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
Events\AfterLoopIterationEvent::class => [
...Defaults::afterLoopIteration(),
Listeners\RunGarbageCollectorListener::class, // keep the memory usage low
// Listeners\CleanupUploadedFilesListener::class, // remove temporary files
],

Events\AfterLoopStoppedEvent::class => [
Expand Down
33 changes: 33 additions & 0 deletions src/Listeners/CleanupUploadedFilesListener.php
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);
}
}
}
}
}
}
}
84 changes: 84 additions & 0 deletions tests/Unit/Listeners/CleanupUploadedFilesListenerTest.php
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();
}
}

0 comments on commit e260bef

Please sign in to comment.