From 08d0192a60092e455dbea1118a77b18b0d442f47 Mon Sep 17 00:00:00 2001 From: Jianxuan Li Date: Fri, 26 Dec 2014 21:45:05 +0800 Subject: [PATCH] add Westdc\File namespace,add uuid maker in Tools Service --- Westdc/File/File.php | 44 ++++ Westdc/File/Listener/DefaultFileListener.php | 40 ++++ Westdc/File/Upload.php | 233 +++++++++++++++++++ Westdc/Helpers/Theme.php | 20 +- Westdc/Helpers/Tools.php | 26 +++ Westdc/Reference/Reference.php | 7 +- 6 files changed, 359 insertions(+), 11 deletions(-) create mode 100644 Westdc/File/File.php create mode 100644 Westdc/File/Listener/DefaultFileListener.php create mode 100644 Westdc/File/Upload.php diff --git a/Westdc/File/File.php b/Westdc/File/File.php new file mode 100644 index 0000000..268cf7a --- /dev/null +++ b/Westdc/File/File.php @@ -0,0 +1,44 @@ +serviceManager = $serviceManager; + + return $this; + } + + //获取文件扩展名 + public function getFileTextExt($file_name) + { + $temp_arr = explode(".", $file_name); + $file_ext = array_pop($temp_arr); + $file_ext = trim($file_ext); + $file_ext = strtolower($file_ext); + return $file_ext; + } + + //获取文件Mime,通过finfo扩展 + public function getFileMime($file_name) + { + $finfo = finfo_open(FILEINFO_MIME_TYPE); + $filetype = finfo_file($finfo, $file_name) ; //文件mime类型 + finfo_close($finfo); + return $filetype; + } + +} \ No newline at end of file diff --git a/Westdc/File/Listener/DefaultFileListener.php b/Westdc/File/Listener/DefaultFileListener.php new file mode 100644 index 0000000..a93ba09 --- /dev/null +++ b/Westdc/File/Listener/DefaultFileListener.php @@ -0,0 +1,40 @@ +listeners[] = $events->attach('upload.pre', function($e){ + return true; + return ['error' => '文件格式不在可上传的范围内']; + }, 100); + + $this->listeners[] = $events->attach('upload.pre', function($e){ + return true; + return ['error' => '文件大小超出了限制']; + }, 80); + + } + + public function detach(EventManagerInterface $events) + { + foreach ($this->listeners as $index => $listener) { + if ($events->detach($listener)) { + unset($this->listeners[$index]); + } + } + } + +} diff --git a/Westdc/File/Upload.php b/Westdc/File/Upload.php new file mode 100644 index 0000000..4e7878a --- /dev/null +++ b/Westdc/File/Upload.php @@ -0,0 +1,233 @@ +serviceManager = $serviceManager; + + $this->init(); + + return $this; + } + + public function init() + { + $Listener = new DefaultFileListener; + $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) + { + if (empty($files) !== false) { + return array("error"=>"请选择要上传的文件."); + } + + if (is_uploaded_file($files['tmp_name']) === false) { + return array("error"=>"文件上传失败,请重新上传"); + } + + $source = $this->source; + $file = $files; + + $results = $this->getEventManager()->trigger('upload.pre', $this, compact('file')); + $cache_data = $results->last(); + + if($cache_data !== true) + { + return $cache_data; + } + + $msg = array(); + + $file_name = $files['name']; //原文件名 + $file_size = $files['size']; //文件大小 + + $results = $this->makeUploadTarget(); + + 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;//新文件名 + + //移动文件 + $file_path = $results['save_path'] . $new_file_name ; + + 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;//文件链接 + + $results = $this->getEventManager()->trigger('upload.makeThumb', $this, compact('conf','file_path','db_path','file_ext','new_file_basename')); + $thumbnail = $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; + + return $msg; + }//文件上传 + + /** + * 设置上传文件的根路径,这样操作会忽略config中的定义 + * 根路径需要自行创建 + * 路径结尾必须加 "/" + * @param string $path + */ + public function setRootDir($path = "") + { + if(empty($path)) + $this->uploadPath = $this->config->upload; + + $this->uploadPath = $path; + + if(!preg_match("/[\/|\\\]+$/",$this->uploadPath)) + $this->uploadPath .= "/"; + } + + /** + * 设置子路径,自动加在根路径之后 + * 如果不存在程序将创建 + * @param $dirname + */ + public function setChildDir($dirname) + { + if(empty($dirname)) { + return false; + } + + $this->uploadPath .= $dirname; + + if (!preg_match("/[\/|\\\]+$/", $this->uploadPath)) + $this->uploadPath .= "/"; + + + if(!file_exists($this->uploadPath)) + mkdir($this->uploadPath); + + $this->relativePath = $dirname; + } + + /** + * 创建并返回年月日的子目录路径 + * @return string + */ + public function makeDateDir() + { + $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); + } + + $current_path .= $m . "/"; + $save_path .= $current_path; + + if (!file_exists($save_path)) { + mkdir($save_path); + } + + $current_path .= $d ."/"; + $save_path .= $current_path; + + if (!file_exists($save_path)) { + mkdir($save_path); + } + + $this->uploadPath .= $current_path; + $this->relativePath .= $current_path; + return $current_path; + } + + /** + * @param $fileName + * @param $fileExt + */ + public function setFileName($fileName,$fileExt) + { + if(!empty($fileName)){ + $this->fileName = $fileName . "." .$fileExt; + return; + } + + $tools = $this->serviceManager->get('Tools'); + $uuid = $tools->uuid(); + + $this->fileName = $uuid . "." . $fileExt; + + } + + /** + * @return string + */ + public function getFileName() + { + return $this->fileName; + } + + /** + * @return string + */ + public function getUploadPath() + { + return $this->uploadPath; + } + + /** + * @return string + */ + public function getRelativePath() + { + return $this->relativePath; + } + +} \ No newline at end of file diff --git a/Westdc/Helpers/Theme.php b/Westdc/Helpers/Theme.php index 5993f38..5c06fa5 100644 --- a/Westdc/Helpers/Theme.php +++ b/Westdc/Helpers/Theme.php @@ -114,15 +114,17 @@ class Theme '/js/lib/jquery.masonry.min.js' ), ), - - 'uploadify' => array( - $this->ScriptKey => array( - '/js/lib/uploadify/jquery.uploadify.min.js' - ), - $this->CSSKey => array( - '/js/lib/uploadify/uploadify.css' - ), - ), + + 'jquery-fileupload' => array( + $this->ScriptKey => array( + '/static/lib/jquery-fileupload/vendor/jquery.ui.widget.js', + '/static/lib/jquery-fileupload/jquery.iframe-transport.js', + '/static/lib/jquery-fileupload/jquery.fileupload.js', + ), + $this->CSSKey => array( + '/static/lib/jquery-fileupload/css/jquery.fileupload.css', + ) + ), 'datepicker' => array( $this->ScriptKey => array( diff --git a/Westdc/Helpers/Tools.php b/Westdc/Helpers/Tools.php index 071479a..ee0658f 100644 --- a/Westdc/Helpers/Tools.php +++ b/Westdc/Helpers/Tools.php @@ -54,6 +54,32 @@ class Tools { return $response; } + + /** + * @return string + */ + public function uuid() + { + return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', + // 32 bits for "time_low" + mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), + + // 16 bits for "time_mid" + mt_rand( 0, 0xffff ), + + // 16 bits for "time_hi_and_version", + // four most significant bits holds version number 4 + mt_rand( 0, 0x0fff ) | 0x4000, + + // 16 bits, 8 bits for "clk_seq_hi_res", + // 8 bits for "clk_seq_low", + // two most significant bits holds zero and one for variant DCE1.1 + mt_rand( 0, 0x3fff ) | 0x8000, + + // 48 bits for "node" + mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) + ); + } } \ No newline at end of file diff --git a/Westdc/Reference/Reference.php b/Westdc/Reference/Reference.php index c19b6ae..24184db 100644 --- a/Westdc/Reference/Reference.php +++ b/Westdc/Reference/Reference.php @@ -108,13 +108,16 @@ class Reference extends AbstractEventManager implements ServiceManagerAwareInter //上传文献PDF public function uploadReferencePdf($file,$autoread = false) { - $files = new Files(); - $file_info = $files->upload($file,'literature/',true); + $fileService = $this->serviceManager->get('File/Upload'); + $file_info = $fileService->upload($file,'literature/',true); if(isset($file_info['error']) && !empty($file_info['error'])) { return array("error" => $file_info['error']); } + + var_dump($file_info); + exit(); $file_data = array( 'filename' => $file_info['file_url'],