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

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 9 deletions.
52 changes: 43 additions & 9 deletions src/File/RenameUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ class RenameUpload extends AbstractFilter
* @var array
*/
protected $options = array(
'target' => null,
'use_upload_name' => false,
'overwrite' => false,
'randomize' => false,
'target' => null,
'use_upload_name' => false,
'use_upload_extension' => false,
'overwrite' => false,
'randomize' => false,
);

/**
Expand Down Expand Up @@ -91,6 +92,25 @@ public function getUseUploadName()
return $this->options['use_upload_name'];
}

/**
* @param boolean $flag When true, this filter will use the original file
* extension for the target filename
* @return RenameUpload
*/
public function setUseUploadExtension($flag = true)
{
$this->options['use_upload_extension'] = (boolean) $flag;
return $this;
}

/**
* @return boolean
*/
public function getUseUploadExtension()
{
return $this->options['use_upload_extension'];
}

/**
* @param boolean $flag Shall existing files be overwritten?
* @return RenameUpload
Expand Down Expand Up @@ -242,12 +262,19 @@ protected function getFinalTarget($uploadData)
$targetFile = basename($uploadData['name']);
} elseif (!is_dir($target)) {
$targetFile = basename($target);
if ($this->getUseUploadExtension() && !$this->getRandomize()) {
$targetInfo = pathinfo($targetFile);
$sourceinfo = pathinfo($uploadData['name']);
if (isset($sourceinfo['extension'])) {
$targetFile = $targetInfo['filename'] . '.' . $sourceinfo['extension'];
}
}
} else {
$targetFile = basename($source);
}

if ($this->getRandomize()) {
$targetFile = $this->applyRandomToFilename($targetFile);
$targetFile = $this->applyRandomToFilename($uploadData['name'], $targetFile);
}

return $targetDir . $targetFile;
Expand All @@ -257,13 +284,20 @@ protected function getFinalTarget($uploadData)
* @param string $filename
* @return string
*/
protected function applyRandomToFilename($filename)
protected function applyRandomToFilename($source,$filename)
{
$info = pathinfo($filename);
$filename = $info['filename'] . uniqid('_');
if (isset($info['extension'])) {
$filename .= '.' . $info['extension'];

$sourceinfo = pathinfo($source);

$extension = '';
if ($this->getUseUploadExtension() === true && isset($sourceinfo['extension'])) {
$extension .= '.' . $sourceinfo['extension'];
} elseif (isset($info['extension'])) {
$extension .= '.' . $info['extension'];
}
return $filename;

return $filename . $extension;
}
}
34 changes: 34 additions & 0 deletions test/File/RenameUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,40 @@ public function testGetRandomizedFile()
$this->assertRegExp('#' . $fileNoExt . '_.{13}\.xml#', $filter($this->_oldFile));
}

public function testGetFileWithOriginalExtension()
{
$fileNoExt = $this->_filesPath . '/newfile';
$filter = new RenameUploadMock(array(
'target' => $this->_newFile,
'use_upload_extension' => true,
'randomize' => false,
));

$oldFilePathInfo = pathinfo($this->_oldFile);

$this->assertRegExp(
'#' . $fileNoExt . '.'.$oldFilePathInfo['extension'].'#',
$filter($this->_oldFile)
);
}

public function testGetRandomizedFileWithOriginalExtension()
{
$fileNoExt = $this->_filesPath . '/newfile';
$filter = new RenameUploadMock(array(
'target' => $this->_newFile,
'use_upload_extension' => true,
'randomize' => true,
));

$oldFilePathInfo = pathinfo($this->_oldFile);

$this->assertRegExp(
'#' . $fileNoExt . '_.{13}\.'.$oldFilePathInfo['extension'].'#',
$filter($this->_oldFile)
);
}

/**
* @return void
*/
Expand Down

0 comments on commit 78201e3

Please sign in to comment.