add new function in service File/Upload

This commit is contained in:
Jianxuan Li 2014-12-28 23:37:16 +08:00
parent 862187d0a5
commit 6e2cfad2cb
3 changed files with 41 additions and 5 deletions

View File

@ -34,10 +34,32 @@ class DefaultFileUploadListener implements ListenerAggregateInterface
$file_data = $e->getParam('file_data');
return true;
$authService = $this->serviceManager->get('Auth');
return ['error' => '文件大小超出了限制'];
}, 80);
$data = [
'filename' => $file_data['db_path'],
'filetype' => $file_data['file_type'],
'filedesc' => $file_data['file_mime'],
'userid' => $authService->getIdentity('id'),
'filesize' => $file_data['file_size'],
'realname' => $file_data['realname'],
];
if(isset($file_data['language']))
{
$data['language'] = $file_data['language'];
}
$dbServices = $this->serviceManager->get('Db');
$dbh = $dbServices->getDbh();
$attid = $dbh->insert("attachments" , $data , true);
if(is_numeric($attid))
return ['attid' => $attid];
return false;
}, 100);
}

View File

@ -21,6 +21,7 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
private $relativePath = "";
private $fileName = "";
private $config;
private $params;
//日期路径模式
const DATETIME_MODEL_YMD = "Y/M/D/";
@ -47,7 +48,7 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
public function __invoke($files,$rootDir = "",$childDir = "",$fileName = "",$dateDirModel = self::DATETIME_MODEL_YMD)
{
$this->upload($files,$rootDir,$childDir,$fileName,$dateDirModel);
return $this->upload($files,$rootDir,$childDir,$fileName,$dateDirModel);
}
/**
@ -97,12 +98,18 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
}
$file_data = array();
$file_data['file_url'] = $this->getRelativePath() . $this->getFileName();
$file_data['file_size'] = $files['size'];
$file_data['db_path'] = $this->getRelativePath() . $this->getFileName();
$file_data['realname'] = $files['name'];
$file_data['file_ext'] = $fileService->getFileTextExt($files['name']);
$file_data['file_type'] = $fileService->getFileMime($file_path);
$file_data['file_mime'] = $fileService->getFileMime($file_path);
if(!empty($file_data) && is_array($file_data))
{
$file_data = array_merge($file_data,$this->params);
}
$results = $this->getEventManager()->trigger('upload.after', $this, compact('file_data'));
$cache_data = $results->last();
@ -210,6 +217,12 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
}
public function setParams($params)
{
$this->params = $params;
}
/**
* @return string
*/

View File

@ -112,6 +112,7 @@ class Reference extends AbstractEventManager implements ServiceManagerAwareInter
$appConfig = $configService->get('application.ini');
$fileUploadService = $this->serviceManager->get('File/Upload');
$fileUploadService->setParams(['file_type' => 'literature']);
$file_info = $fileUploadService($file,$appConfig['reference_save_path'],"","",$fileUploadService::DATETIME_MODEL_Y);
if(isset($file_info['error']) && !empty($file_info['error']))