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,11 +81,15 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
$file = $files;
$results = $this->getEventManager()->trigger('upload.pre', $this, compact('file'));
$cache_data = $results->last();
if($cache_data !== true)
if($this->defaultListener === true)
{
return $cache_data;
$cache_data = $results->last();
if($cache_data !== true)
{
return $cache_data;
}
}
$fileService = $this->serviceManager->get('File');
@ -94,7 +100,11 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
if($dateDirModel !== false)
$this->makeDateDir($dateDirModel);
$this->setFileName($fileName , $fileService->getFileTextExt($files['name']));
if(empty($this->fileName) || empty($fileName))
$this->setFileName(NULL , $fileService->getFileTextExt($files['name']));
if(!empty($fileName))
$this->setFileName($fileName , $fileService->getFileTextExt($files['name']));
//移动文件
$file_path = $this->getUploadPath() . $this->getFileName();
@ -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)){
$this->fileName = $fileName . "." .$fileExt;
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();
$this->fileName = $uuid . "." . $fileExt;
if(empty($fileExt))
$this->fileName = $uuid;
else
$this->fileName = $uuid . "." . $fileExt;
return;
}