prefection the FileUpload Service

This commit is contained in:
Jianxuan Li 2015-01-13 17:24:11 +08:00
parent 9d68652990
commit 9a7e8c9b1e
1 changed files with 32 additions and 8 deletions

View File

@ -16,6 +16,7 @@ use Westdc\File\Listener\DefaultFileUploadListener;
class Upload extends AbstractEventManager implements ServiceManagerAwareInterface{
protected $serviceManager;
protected $defaultListener = false;
private $uploadPath = "";
private $relativePath = "";
@ -55,6 +56,7 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
{
$Listener = new DefaultFileUploadListener;
$this->getEventManager()->attachAggregate($Listener);
$this->defaultListener = true;
}
/**
@ -79,12 +81,16 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
$file = $files;
$results = $this->getEventManager()->trigger('upload.pre', $this, compact('file'));
if($this->defaultListener === true)
{
$cache_data = $results->last();
if($cache_data !== true)
{
return $cache_data;
}
}
$fileService = $this->serviceManager->get('File');
@ -94,6 +100,10 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
if($dateDirModel !== false)
$this->makeDateDir($dateDirModel);
if(empty($this->fileName) || empty($fileName))
$this->setFileName(NULL , $fileService->getFileTextExt($files['name']));
if(!empty($fileName))
$this->setFileName($fileName , $fileService->getFileTextExt($files['name']));
//移动文件
@ -112,7 +122,7 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
$file_data['file_ext'] = $fileService->getFileTextExt($files['name']);
$file_data['file_mime'] = $fileService->getFileMime($file_path);
if(!empty($file_data) && is_array($file_data))
if(!empty($this->params) && is_array($this->params))
{
$file_data = array_merge($file_data,$this->params);
}
@ -209,18 +219,32 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
* @param $fileName
* @param $fileExt
*/
public function setFileName($fileName,$fileExt)
public function setFileName($fileName,$fileExt = "")
{
if(!empty($fileName)){
if(empty($fileExt))
{
$fileExt = pathinfo($fileName,PATHINFO_EXTENSION);
}
if(empty($fileExt))
$this->fileName = $fileName;
else
$this->fileName = $fileName . "." .$fileExt;
return;
}
$tools = $this->serviceManager->get('Tools');
$uuid = $tools->uuid();
if(empty($fileExt))
$this->fileName = $uuid;
else
$this->fileName = $uuid . "." . $fileExt;
return;
}