Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
RenameUpload returns a new instance of UploadedFile so that
Browse files Browse the repository at this point in the history
 - can still be used in request handlers or other validators in the chain
 - reading off what new filename was generated by the filter options.
  • Loading branch information
alextech committed Aug 17, 2018
1 parent b30e325 commit 32228d5
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 69 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
"require": {
"php": "^5.6 || ^7.0",
"psr/http-message": "^1.0",
"zendframework/zend-diactoros": "^1.8",
"zendframework/zend-stdlib": "^2.7.7 || ^3.1"
},
"require-dev": {
"pear/archive_tar": "^1.4.3",
"phpunit/phpunit": "^5.7.23 || ^6.4.3",
"zendframework/zend-coding-standard": "~1.0.0",
"zendframework/zend-crypt": "^3.2.1",
"zendframework/zend-diactoros": "^1.8",
"zendframework/zend-servicemanager": "^2.7.8 || ^3.3",
"zendframework/zend-uri": "^2.6"
},
Expand Down
128 changes: 64 additions & 64 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions src/File/RenameUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Filter\File;

use Psr\Http\Message\UploadedFileInterface;
use Zend\Diactoros\UploadedFile;
use Zend\Filter\AbstractFilter;
use Zend\Filter\Exception;
use Zend\Stdlib\ErrorHandler;
Expand Down Expand Up @@ -202,7 +203,13 @@ public function filter($value)
$return = $targetFile;
if ($isFileUpload) {
if ($value instanceof UploadedFileInterface) {
$return = $value;
$return = new UploadedFile(
$targetFile,
filesize($targetFile),
UPLOAD_ERR_OK,
$value->getClientFilename(),
$value->getClientMediaType()
);
} else {
$return = [
'tmp_name' => $clientFilename,
Expand Down Expand Up @@ -256,7 +263,9 @@ protected function checkFileExists($targetFile)
}

/**
* @param array $uploadData $_FILES array
* @param $source
* @param $clientFileName
*
* @return string
*/
protected function getFinalTarget($source, $clientFileName)
Expand Down
16 changes: 14 additions & 2 deletions test/File/RenameUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,26 @@ public function testStringConstructorWithPsrFile()
$filter = new RenameUploadMock($this->targetFile);
$this->assertEquals($this->targetFile, $filter->getTarget());

/** @var UploadedFile $moved */
$moved = $filter(new UploadedFile(
$this->sourceFile,
1,
0,
$this->targetFile
));
$this->expectExceptionMessage('Cannot retrieve stream after it has already been moved');

/** @var UploadedFile $moved */
$this->assertEquals(
new UploadedFile(
$this->targetFile,
0,
0,
$this->targetFile
),
$moved
);

// exception should NOT be thrown.
// Moved file in real application will be used by request handlers.
$moved->getStream();
$this->assertEquals('falsefile', $filter('falsefile'));
}
Expand Down

0 comments on commit 32228d5

Please sign in to comment.