Skip to content

Commit

Permalink
Merge branch 'hoftix/multiple-muf-renameupload' of git://github.com/S…
Browse files Browse the repository at this point in the history
…lamdunk/zf2 into Slamdunk-hoftix/multiple-muf-renameupload
  • Loading branch information
Ralph Schindler committed Feb 15, 2013
2 parents 4fe0377 + a6c8187 commit ce355fb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
23 changes: 20 additions & 3 deletions library/Zend/Filter/File/RenameUpload.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ class RenameUpload extends AbstractFilter
'randomize' => false, 'randomize' => false,
); );


/**
* Store already filtered values, so we can filter multiple
* times the same file without being block by move_uploaded_file
* internal checks
*
* @var array
*/
protected $alreadyFiltered = array();

/** /**
* Constructor * Constructor
* *
Expand Down Expand Up @@ -143,6 +152,10 @@ public function filter($value)
$sourceFile = $value; $sourceFile = $value;
} }


if (isset($this->alreadyFiltered[$sourceFile])) {
return $this->alreadyFiltered[$sourceFile];
}

$targetFile = $this->getFinalTarget($uploadData); $targetFile = $this->getFinalTarget($uploadData);
if (!file_exists($sourceFile) || $sourceFile == $targetFile) { if (!file_exists($sourceFile) || $sourceFile == $targetFile) {
return $value; return $value;
Expand All @@ -151,11 +164,15 @@ public function filter($value)
$this->checkFileExists($targetFile); $this->checkFileExists($targetFile);
$this->moveUploadedFile($sourceFile, $targetFile); $this->moveUploadedFile($sourceFile, $targetFile);


$return = $targetFile;
if ($isFileUpload) { if ($isFileUpload) {
$uploadData['tmp_name'] = $targetFile; $return = $uploadData;
return $uploadData; $return['tmp_name'] = $targetFile;
} }
return $targetFile;
$this->alreadyFiltered[$sourceFile] = $return;

return $return;
} }


/** /**
Expand Down
25 changes: 20 additions & 5 deletions tests/ZendTest/Filter/File/RenameUploadTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -249,14 +249,12 @@ public function testCannotOverwriteExistingFile()
*/ */
public function testGetRandomizedFile() public function testGetRandomizedFile()
{ {
$fileNoExt = $this->_filesPath . '/newfile';
$filter = new RenameUploadMock(array( $filter = new RenameUploadMock(array(
'target' => $this->_newFile, 'target' => $this->_newFile,
'randomize' => true, 'randomize' => true,
)); ));


$this->assertEquals($this->_newFile, $filter->getTarget());
$this->assertTrue($filter->getRandomize());
$fileNoExt = $this->_filesPath . '/newfile';
$this->assertRegExp('#' . $fileNoExt . '_.{13}\.xml#', $filter($this->_oldFile)); $this->assertRegExp('#' . $fileNoExt . '_.{13}\.xml#', $filter($this->_oldFile));
} }


Expand All @@ -271,8 +269,6 @@ public function testGetRandomizedFileWithoutExtension()
'randomize' => true, 'randomize' => true,
)); ));


$this->assertEquals($fileNoExt, $filter->getTarget());
$this->assertTrue($filter->getRandomize());
$this->assertRegExp('#' . $fileNoExt . '_.{13}#', $filter($this->_oldFile)); $this->assertRegExp('#' . $fileNoExt . '_.{13}#', $filter($this->_oldFile));
} }


Expand All @@ -284,4 +280,23 @@ public function testInvalidConstruction()
$this->setExpectedException('\Zend\Filter\Exception\InvalidArgumentException', 'Invalid target'); $this->setExpectedException('\Zend\Filter\Exception\InvalidArgumentException', 'Invalid target');
$filter = new FileRenameUpload(1234); $filter = new FileRenameUpload(1234);
} }

/**
* @return void
*/
public function testCanFilterMultipleTimesWithSameResult()
{
$filter = new RenameUploadMock(array(
'target' => $this->_newFile,
'randomize' => true,
));

$firstResult = $filter($this->_oldFile);

$this->assertContains('newfile', $firstResult);

$secondResult = $filter($this->_oldFile);

$this->assertSame($firstResult, $secondResult);
}
} }

0 comments on commit ce355fb

Please sign in to comment.