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

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
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
23 changes: 20 additions & 3 deletions src/File/RenameUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ class RenameUpload extends AbstractFilter
'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
*
Expand Down Expand Up @@ -143,6 +152,10 @@ public function filter($value)
$sourceFile = $value;
}

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

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

$return = $targetFile;
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 test/File/RenameUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,12 @@ public function testCannotOverwriteExistingFile()
*/
public function testGetRandomizedFile()
{
$fileNoExt = $this->_filesPath . '/newfile';
$filter = new RenameUploadMock(array(
'target' => $this->_newFile,
'randomize' => true,
));

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

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

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

Expand All @@ -284,4 +280,23 @@ public function testInvalidConstruction()
$this->setExpectedException('\Zend\Filter\Exception\InvalidArgumentException', 'Invalid target');
$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 3f667bd

Please sign in to comment.