From d21de7cc1171c3fb8d61213a5393bfae882ab4c0 Mon Sep 17 00:00:00 2001 From: Jianxuan Li Date: Sun, 28 Dec 2014 00:03:41 +0800 Subject: [PATCH] add new service File/Upload,add reference upload test function,rename file upload listener --- ...ener.php => DefaultFileUploadListener.php} | 18 ++- Westdc/File/Upload.php | 110 ++++++++---------- Westdc/Reference/Reference.php | 5 +- Westdc/Service/ServiceManager.php | 2 +- 4 files changed, 71 insertions(+), 64 deletions(-) rename Westdc/File/Listener/{DefaultFileListener.php => DefaultFileUploadListener.php} (64%) diff --git a/Westdc/File/Listener/DefaultFileListener.php b/Westdc/File/Listener/DefaultFileUploadListener.php similarity index 64% rename from Westdc/File/Listener/DefaultFileListener.php rename to Westdc/File/Listener/DefaultFileUploadListener.php index a93ba09..c6cc3b6 100644 --- a/Westdc/File/Listener/DefaultFileListener.php +++ b/Westdc/File/Listener/DefaultFileUploadListener.php @@ -4,14 +4,18 @@ namespace Westdc\File\Listener; use Zend\EventManager\EventCollection; use Zend\EventManager\ListenerAggregateInterface; use Zend\EventManager\EventManagerInterface; +use Westdc\Service\ServiceManager; -class DefaultFileListener implements ListenerAggregateInterface +class DefaultFileUploadListener implements ListenerAggregateInterface { protected $listeners = array(); + protected $serviceManager; + function __construct() { - + $this->serviceManager = new ServiceManager(); + $this->serviceManager = $this->serviceManager->getServiceManager(); } public function attach(EventManagerInterface $events) @@ -26,6 +30,16 @@ class DefaultFileListener implements ListenerAggregateInterface return ['error' => '文件大小超出了限制']; }, 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) diff --git a/Westdc/File/Upload.php b/Westdc/File/Upload.php index 4e7878a..bd5ee6a 100644 --- a/Westdc/File/Upload.php +++ b/Westdc/File/Upload.php @@ -11,7 +11,7 @@ namespace Westdc\File; use Zend\ServiceManager\ServiceManager; use Zend\ServiceManager\ServiceManagerAwareInterface; use Westdc\EventModel\AbstractEventManager; -use Westdc\File\Listener\DefaultFileListener; +use Westdc\File\Listener\DefaultFileUploadListener; class Upload extends AbstractEventManager implements ServiceManagerAwareInterface{ @@ -22,6 +22,10 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac private $fileName = ""; 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) { $this->serviceManager = $serviceManager; @@ -33,23 +37,15 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac public function init() { - $Listener = new DefaultFileListener; + $Listener = new DefaultFileUploadListener; $this->getEventManager()->attachAggregate($Listener); $configService = $this->serviceManager->get('ConfigService'); $this->config = $configService->get('file.php'); } - /** - * upload - * - * 文件上传 - * - * @param Array $files e.g. $_FILES['Filedata'] - * - * @return Array $msg e.g. if($msg['error']) - */ - public function upload($files) + + public function upload($files,$rootDir = "",$childDir = "",$fileName = "",$dateDirModel = self::DATETIME_MODEL_YMD) { if (empty($files) !== false) { return array("error"=>"请选择要上传的文件."); @@ -59,7 +55,6 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac return array("error"=>"文件上传失败,请重新上传"); } - $source = $this->source; $file = $files; $results = $this->getEventManager()->trigger('upload.pre', $this, compact('file')); @@ -70,45 +65,38 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac return $cache_data; } - $msg = array(); + $fileService = $this->serviceManager->get('File'); - $file_name = $files['name']; //原文件名 - $file_size = $files['size']; //文件大小 + $this->setRootDir($rootDir); + $this->setChildDir($childDir); - $results = $this->makeUploadTarget(); + if($dateDirModel !== false) + $this->makeDateDir($dateDirModel); - if(isset($results['error'])) - { - return $results; - }//文件夹问题 - - $new_file_basename = $this->gen_uuid(); - $file_ext = $this->getFileTextExt($file_name); - $new_file_name = $new_file_basename . '.' . $file_ext;//新文件名 + $this->setFileName($fileName , $fileService->getFileTextExt($files['name'])); //移动文件 - $file_path = $results['save_path'] . $new_file_name ; + $file_path = $this->getUploadPath() . $this->getFileName(); if (move_uploaded_file($file['tmp_name'], $file_path) === false) { return array("error"=>"上传失败,请重试"); } - $dbsave = $db_path = $results['dbsave']; - $dbsave .= $new_file_name;//数据库最终存储的文件 - $file_url = $dbsave;//文件链接 + $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); - $results = $this->getEventManager()->trigger('upload.makeThumb', $this, compact('conf','file_path','db_path','file_ext','new_file_basename')); - $thumbnail = $results->last(); + $results = $this->getEventManager()->trigger('upload.after', $this, compact('file_data')); + $cache_data = $results->last(); - $msg['file_url'] = $file_url; - $msg['file_size'] = $file_size; - $msg['db_path'] = $dbsave; - $msg['realname'] = $file_name; - $msg['file_ext'] = $file_ext; - $msg['file_type'] = $this->getFileMime($file_path); - $msg['thumb'] = $thumbnail; + if(is_array($cache_data)) + $file_data = array_merge($file_data , $cache_data); - return $msg; + return $file_data; }//文件上传 /** @@ -116,6 +104,7 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac * 根路径需要自行创建 * 路径结尾必须加 "/" * @param string $path + * @return bool */ public function setRootDir($path = "") { @@ -126,17 +115,20 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac if(!preg_match("/[\/|\\\]+$/",$this->uploadPath)) $this->uploadPath .= "/"; + + return true; } /** * 设置子路径,自动加在根路径之后 * 如果不存在程序将创建 - * @param $dirname + * @param string $dirname + * @return bool | string */ public function setChildDir($dirname) { if(empty($dirname)) { - return false; + return true; } $this->uploadPath .= $dirname; @@ -145,8 +137,10 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac $this->uploadPath .= "/"; - if(!file_exists($this->uploadPath)) - mkdir($this->uploadPath); + if(!file_exists($this->uploadPath)) { + if(!mkdir($this->uploadPath)) + return "failed to create folder :".$this->uploadPath; + } $this->relativePath = $dirname; } @@ -155,32 +149,28 @@ class Upload extends AbstractEventManager implements ServiceManagerAwareInterfac * 创建并返回年月日的子目录路径 * @return string */ - public function makeDateDir() + public function makeDateDir($model) { $y = date("Y"); $m = date("m"); $d = date("d"); - $current_path = $y . "/"; - - $save_path = $this->uploadPath . $current_path; - - if (!file_exists($save_path)) { - mkdir($save_path); + if($model == self::DATETIME_MODEL_YMD || $model == self::DATETIME_MODEL_YM || $model == self::DATETIME_MODEL_Y) { + $current_path = $y . "/"; + if (!file_exists($this->uploadPath . $current_path)) + mkdir($this->uploadPath . $current_path); } - $current_path .= $m . "/"; - $save_path .= $current_path; - - if (!file_exists($save_path)) { - mkdir($save_path); + if($model == self::DATETIME_MODEL_YMD || $model == self::DATETIME_MODEL_YM){ + $current_path .= $m . "/"; + if (!file_exists($this->uploadPath . $current_path)) + mkdir($this->uploadPath . $current_path); } - $current_path .= $d ."/"; - $save_path .= $current_path; - - if (!file_exists($save_path)) { - mkdir($save_path); + if($model == self::DATETIME_MODEL_YMD) { + $current_path .= $d ."/"; + if (!file_exists($this->uploadPath . $current_path)) + mkdir($this->uploadPath . $current_path); } $this->uploadPath .= $current_path; diff --git a/Westdc/Reference/Reference.php b/Westdc/Reference/Reference.php index 24184db..a576692 100644 --- a/Westdc/Reference/Reference.php +++ b/Westdc/Reference/Reference.php @@ -108,8 +108,11 @@ class Reference extends AbstractEventManager implements ServiceManagerAwareInter //上传文献PDF public function uploadReferencePdf($file,$autoread = false) { + $configService = $this->serviceManager->get('ConfigService'); + $appConfig = $configService->get('application.ini'); + $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'])) { diff --git a/Westdc/Service/ServiceManager.php b/Westdc/Service/ServiceManager.php index 558c090..e323740 100644 --- a/Westdc/Service/ServiceManager.php +++ b/Westdc/Service/ServiceManager.php @@ -16,7 +16,7 @@ class ServiceManager { function __construct() { - $this->serviceManager = new Zend_ServiceManager(); + $this->serviceManager = new Zend_ServiceManager; $this->serviceManager->addAbstractFactory(new ServiceFactory); $configService = $this->serviceManager->get('ConfigService');