add new service File/Upload,add reference upload test function,rename file upload listener

This commit is contained in:
Jianxuan Li 2014-12-28 00:03:41 +08:00
parent 08d0192a60
commit d21de7cc11
4 changed files with 71 additions and 64 deletions

View File

@ -4,14 +4,18 @@ namespace Westdc\File\Listener;
use Zend\EventManager\EventCollection; use Zend\EventManager\EventCollection;
use Zend\EventManager\ListenerAggregateInterface; use Zend\EventManager\ListenerAggregateInterface;
use Zend\EventManager\EventManagerInterface; use Zend\EventManager\EventManagerInterface;
use Westdc\Service\ServiceManager;
class DefaultFileListener implements ListenerAggregateInterface class DefaultFileUploadListener implements ListenerAggregateInterface
{ {
protected $listeners = array(); protected $listeners = array();
protected $serviceManager;
function __construct() function __construct()
{ {
$this->serviceManager = new ServiceManager();
$this->serviceManager = $this->serviceManager->getServiceManager();
} }
public function attach(EventManagerInterface $events) public function attach(EventManagerInterface $events)
@ -26,6 +30,16 @@ class DefaultFileListener implements ListenerAggregateInterface
return ['error' => '文件大小超出了限制']; return ['error' => '文件大小超出了限制'];
}, 80); }, 80);
$this->listeners[] = $events->attach('upload.after', function($e){
$file_data = $e->getParam('file_data');
return true;
return ['error' => '文件大小超出了限制'];
}, 80);
} }
public function detach(EventManagerInterface $events) public function detach(EventManagerInterface $events)

View File

@ -11,7 +11,7 @@ namespace Westdc\File;
use Zend\ServiceManager\ServiceManager; use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface; use Zend\ServiceManager\ServiceManagerAwareInterface;
use Westdc\EventModel\AbstractEventManager; use Westdc\EventModel\AbstractEventManager;
use Westdc\File\Listener\DefaultFileListener; use Westdc\File\Listener\DefaultFileUploadListener;
class Upload extends AbstractEventManager implements ServiceManagerAwareInterface{ class Upload extends AbstractEventManager implements ServiceManagerAwareInterface{
@ -22,6 +22,10 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
private $fileName = ""; private $fileName = "";
private $config; private $config;
const DATETIME_MODEL_YMD = "Y/M/D/";
const DATETIME_MODEL_YM = "Y/M/";
const DATETIME_MODEL_Y = "Y/";
public function setServiceManager(ServiceManager $serviceManager) public function setServiceManager(ServiceManager $serviceManager)
{ {
$this->serviceManager = $serviceManager; $this->serviceManager = $serviceManager;
@ -33,23 +37,15 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
public function init() public function init()
{ {
$Listener = new DefaultFileListener; $Listener = new DefaultFileUploadListener;
$this->getEventManager()->attachAggregate($Listener); $this->getEventManager()->attachAggregate($Listener);
$configService = $this->serviceManager->get('ConfigService'); $configService = $this->serviceManager->get('ConfigService');
$this->config = $configService->get('file.php'); $this->config = $configService->get('file.php');
} }
/**
* upload public function upload($files,$rootDir = "",$childDir = "",$fileName = "",$dateDirModel = self::DATETIME_MODEL_YMD)
*
* 文件上传
*
* @param Array $files e.g. $_FILES['Filedata']
*
* @return Array $msg e.g. if($msg['error'])
*/
public function upload($files)
{ {
if (empty($files) !== false) { if (empty($files) !== false) {
return array("error"=>"请选择要上传的文件."); return array("error"=>"请选择要上传的文件.");
@ -59,7 +55,6 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
return array("error"=>"文件上传失败,请重新上传"); return array("error"=>"文件上传失败,请重新上传");
} }
$source = $this->source;
$file = $files; $file = $files;
$results = $this->getEventManager()->trigger('upload.pre', $this, compact('file')); $results = $this->getEventManager()->trigger('upload.pre', $this, compact('file'));
@ -70,45 +65,38 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
return $cache_data; return $cache_data;
} }
$msg = array(); $fileService = $this->serviceManager->get('File');
$file_name = $files['name']; //原文件名 $this->setRootDir($rootDir);
$file_size = $files['size']; //文件大小 $this->setChildDir($childDir);
$results = $this->makeUploadTarget(); if($dateDirModel !== false)
$this->makeDateDir($dateDirModel);
if(isset($results['error'])) $this->setFileName($fileName , $fileService->getFileTextExt($files['name']));
{
return $results;
}//文件夹问题
$new_file_basename = $this->gen_uuid();
$file_ext = $this->getFileTextExt($file_name);
$new_file_name = $new_file_basename . '.' . $file_ext;//新文件名
//移动文件 //移动文件
$file_path = $results['save_path'] . $new_file_name ; $file_path = $this->getUploadPath() . $this->getFileName();
if (move_uploaded_file($file['tmp_name'], $file_path) === false) { if (move_uploaded_file($file['tmp_name'], $file_path) === false) {
return array("error"=>"上传失败,请重试"); return array("error"=>"上传失败,请重试");
} }
$dbsave = $db_path = $results['dbsave']; $file_data = array();
$dbsave .= $new_file_name;//数据库最终存储的文件 $file_data['file_url'] = $this->getRelativePath() . $this->getFileName();
$file_url = $dbsave;//文件链接 $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);
$results = $this->getEventManager()->trigger('upload.makeThumb', $this, compact('conf','file_path','db_path','file_ext','new_file_basename')); $results = $this->getEventManager()->trigger('upload.after', $this, compact('file_data'));
$thumbnail = $results->last(); $cache_data = $results->last();
$msg['file_url'] = $file_url; if(is_array($cache_data))
$msg['file_size'] = $file_size; $file_data = array_merge($file_data , $cache_data);
$msg['db_path'] = $dbsave;
$msg['realname'] = $file_name;
$msg['file_ext'] = $file_ext;
$msg['file_type'] = $this->getFileMime($file_path);
$msg['thumb'] = $thumbnail;
return $msg; return $file_data;
}//文件上传 }//文件上传
/** /**
@ -116,6 +104,7 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
* 根路径需要自行创建 * 根路径需要自行创建
* 路径结尾必须加 "/" * 路径结尾必须加 "/"
* @param string $path * @param string $path
* @return bool
*/ */
public function setRootDir($path = "") public function setRootDir($path = "")
{ {
@ -126,17 +115,20 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
if(!preg_match("/[\/|\\\]+$/",$this->uploadPath)) if(!preg_match("/[\/|\\\]+$/",$this->uploadPath))
$this->uploadPath .= "/"; $this->uploadPath .= "/";
return true;
} }
/** /**
* 设置子路径,自动加在根路径之后 * 设置子路径,自动加在根路径之后
* 如果不存在程序将创建 * 如果不存在程序将创建
* @param $dirname * @param string $dirname
* @return bool | string
*/ */
public function setChildDir($dirname) public function setChildDir($dirname)
{ {
if(empty($dirname)) { if(empty($dirname)) {
return false; return true;
} }
$this->uploadPath .= $dirname; $this->uploadPath .= $dirname;
@ -145,8 +137,10 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
$this->uploadPath .= "/"; $this->uploadPath .= "/";
if(!file_exists($this->uploadPath)) if(!file_exists($this->uploadPath)) {
mkdir($this->uploadPath); if(!mkdir($this->uploadPath))
return "failed to create folder :".$this->uploadPath;
}
$this->relativePath = $dirname; $this->relativePath = $dirname;
} }
@ -155,32 +149,28 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac
* 创建并返回年月日的子目录路径 * 创建并返回年月日的子目录路径
* @return string * @return string
*/ */
public function makeDateDir() public function makeDateDir($model)
{ {
$y = date("Y"); $y = date("Y");
$m = date("m"); $m = date("m");
$d = date("d"); $d = date("d");
$current_path = $y . "/"; if($model == self::DATETIME_MODEL_YMD || $model == self::DATETIME_MODEL_YM || $model == self::DATETIME_MODEL_Y) {
$current_path = $y . "/";
$save_path = $this->uploadPath . $current_path; if (!file_exists($this->uploadPath . $current_path))
mkdir($this->uploadPath . $current_path);
if (!file_exists($save_path)) {
mkdir($save_path);
} }
$current_path .= $m . "/"; if($model == self::DATETIME_MODEL_YMD || $model == self::DATETIME_MODEL_YM){
$save_path .= $current_path; $current_path .= $m . "/";
if (!file_exists($this->uploadPath . $current_path))
if (!file_exists($save_path)) { mkdir($this->uploadPath . $current_path);
mkdir($save_path);
} }
$current_path .= $d ."/"; if($model == self::DATETIME_MODEL_YMD) {
$save_path .= $current_path; $current_path .= $d ."/";
if (!file_exists($this->uploadPath . $current_path))
if (!file_exists($save_path)) { mkdir($this->uploadPath . $current_path);
mkdir($save_path);
} }
$this->uploadPath .= $current_path; $this->uploadPath .= $current_path;

View File

@ -108,8 +108,11 @@ class Reference extends AbstractEventManager implements ServiceManagerAwareInter
//上传文献PDF //上传文献PDF
public function uploadReferencePdf($file,$autoread = false) public function uploadReferencePdf($file,$autoread = false)
{ {
$configService = $this->serviceManager->get('ConfigService');
$appConfig = $configService->get('application.ini');
$fileService = $this->serviceManager->get('File/Upload'); $fileService = $this->serviceManager->get('File/Upload');
$file_info = $fileService->upload($file,'literature/',true); $file_info = $fileService->upload($file,$appConfig['reference_save_path'],"","",$fileService::DATETIME_MODEL_Y);
if(isset($file_info['error']) && !empty($file_info['error'])) if(isset($file_info['error']) && !empty($file_info['error']))
{ {

View File

@ -16,7 +16,7 @@ class ServiceManager {
function __construct() function __construct()
{ {
$this->serviceManager = new Zend_ServiceManager(); $this->serviceManager = new Zend_ServiceManager;
$this->serviceManager->addAbstractFactory(new ServiceFactory); $this->serviceManager->addAbstractFactory(new ServiceFactory);
$configService = $this->serviceManager->get('ConfigService'); $configService = $this->serviceManager->get('ConfigService');