123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php namespace App\Http\Controllers\Admin;
- /**
- * 百度编辑器接口
- *
- *
- * @author 刘相欣
- *
- */
- class Ueditor extends Auth
- {
- protected $config;
- protected $type;
- protected $_basepath = 'uploads/ueditor/';
- /**
- * 将内容存到Storage中,返回转存后的文件路径
- *
- * @param String $action 方法
- * @param String $ext 后缀
- * @param String $content 内容
- */
- private function save_storage_content($action,$ext,$content){
- // 后缀或者内容为空
- if ( !$ext || !$content ) return ['error'=>'上传内容或者文件格式有误'];
- // 获得用户路径
- $user_path = $this->_get_user_path($action);
- // 如果不存在目录
- if( !is_dir($user_path) ) mkdir($user_path,0755,true);
- // 文件名称
- $filename = $user_path.uniqid().'.'. $ext;
- // 写入数据
- $result = @file_put_contents($filename,$content);
- // 失败提示
- if( !$result ) return ['error'=>'上传失败'];
- // 返回路径
- return request()->root().'/'.$filename;
- }
- /**
- * 上传
- */
- public function upload()
- {
- // 接收参数
- $this->type = request('edit_type','');
- $action = request('action');
- // 获取配置
- $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("./static/ueditor/config.json")), true);
- // 赋值通用
- $this->config = $CONFIG;
- // 判断方法,调用对应方式
- switch ($action) {
- case 'config':
- $result = response()->json($CONFIG);
- break;
- /* 上传图片 */
- case 'uploadimage':
- $result = $this->_ueditor_upload(['size' => 1024*1024*100,'ext'=>['jpg', 'gif', 'png', 'jpeg']],'images');
- break;
- /* 上传涂鸦 */
- case 'uploadscrawl':
- $result = $this->_ueditor_upload_scrawl('scrawl');
- break;
- /* 上传视频 */
- case 'uploadvideo':
- $result = $this->_ueditor_upload(['size' => 1024*1024*100 ,'ext'=>['mp4', 'avi', 'wmv','rm','rmvb','mkv']],'video');
- break;
- /* 上传文件 */
- case 'uploadfile':
- $result = $this->_ueditor_upload(['ext'=>['txt','pdf','doc','docx','xls','xlsx','zip','rar','ppt','pptx']],'file');
- break;
- /* 列出图片 */
- case 'listimage':
- $result = $this->_ueditor_list('images');
- break;
- /* 列出文件 */
- case 'listfile':
- $result = $this->_ueditor_list('file');
- break;
- /* 抓取远程文件 */
- case 'catchimage':
- $result = $this->_ueditor_upload_catch();
- break;
- default:
- $result = response()->json(['state'=>'请求地址错误']);
- break;
- }
- // 返回结果
- return $result;
- }
- /**
- * 编辑列表
- *
- * @param String $action 方法
- *
- *
- * */
- private function _ueditor_list($action)
- {
- // 配置
- $config = $this->config;
- // 判断文件后缀
- $ext_allow = $config['imageAllowFiles'];
- // 默认图片列表长度
- $listSize = $this->config['imageManagerListSize'];
- /* 判断类型 */
- if( $action == 'file' ) {
- // 如果是文件
- $listSize = $this->config['fileManagerListSize'];
- // 判断文件后缀
- $ext_allow = $config['fileAllowFiles'];
- }
- // 允许的文件对象
- foreach ($ext_allow as $key => $value) {
- // 去除点
- $ext_allow[$key] = ltrim($value,'.');
- }
- // 合并字符串
- $ext_allow = implode(',',$ext_allow);
- // 列表长度
- $size = request('size',$listSize);
- // 开始
- $start = request('start',0);
- // 获得用户路径
- $user_path = $this->_get_user_path($action);
- // 取得文件
- $files = (array) glob($user_path.'*.{'.$ext_allow.'}', GLOB_BRACE);
- // 循环处理
- foreach ($files as $key => $value) {
- // 组装路径
- $files[$key] = request()->root().'/'.$value;
- }
- // 总数量
- $total = count($files);
- // 列表数据
- $list = array_slice($files,$start,$size);
- // 返回数据
- return response()->json(['state'=>'SUCCESS','list'=>$list,'start'=>$start,'total'=>$total]);
- }
- /**
- * 用户目录
- *
- * */
- private function _get_user_path($action){
- // 当前登录用户
- if( admin('uid') ) return $this->_basepath.$action.'/admin/'.admin('uid').'/'.date('Ymd').'/';
- }
- /**
- * 上传
- *
- */
- private function _ueditor_upload($config=[],$action='') {
- // 如果没有文件
- if( !$action ) return response()->json(['state'=>'上传方法有误','url'=>'','title'=>'','original'=>'']);
- // 如果没有文件
- if( !request()->hasFile('upfile') ) return response()->json(['state'=>'未接收到文件','url'=>'','title'=>'','original'=>'']);
- // 本地上传
- $file = request()->file('upfile');
- // 如果没有文件
- if( !$file->isValid() ) return response()->json(['state'=>'上传错误','url'=>'','title'=>'','original'=>'']);
- // 配置合并配置
- if( !empty($config) ) $config = array_merge($this->config,$config);
- // 获取文件后缀名
- $ext = $file->extension();
- // 获取文件大小
- $size = $file->getSize();
- // 验证文件大小
- if( isset($config['size']) && $size >= $config['size'] ) return response()->json(['state'=>'文件大小超过限制大小','url'=>'','title'=>'','original'=>'']);
- // 验证文件格式
- if( isset($config['ext']) && !in_array($ext,$config['ext'] ) ) return response()->json(['state'=>'请上传允许的格式','url'=>'','title'=>'','original'=>'']);
- // 获取文件名
- $filename = ltrim($file->getClientOriginalName(),'/');
- // 文件名
- $filename = html_entity_decode($filename, ENT_QUOTES, 'UTF-8');
- // 获得用户路径
- $user_path = $this->_get_user_path($action);
- // 保存文件
- $info = $file->move($user_path,$filename);
- // 如果有错误
- if( !$info ) return ['error'=>$file->getError()];
- // 移动失败
- if( isset($objectPath['error']) ) return response()->json(['state'=>$objectPath['error'],'url'=>'','title'=>'','original'=>'']);
- // 数据
- $url = request()->root().'/'.$user_path.$filename;
- // 状态成功
- return response()->json(['state' => 'SUCCESS','url'=>$url,'title'=>$filename,'original'=>$filename]);
- }
- /**
- * 涂鸦
- *
- */
- private function _ueditor_upload_scrawl($action='scrawl'){
- // 接收数据
- $data = request($this->config['scrawlFieldName'],'');
- // 未接收到涂鸦数据
- if ( empty ($data) ) return response()->json(['state'=>'涂鸦内容不能为空','url'=>'','title'=>'','original'=>'']);
- // base64转码接收到的涂鸦数据
- $img = base64_decode($data);
- // 大小
- $size = strlen($img);
- // 验证文件大小
- if( isset($this->config['scrawlMaxSize']) && $size >= $this->config['scrawlMaxSize'] ) return response()->json(['state'=>'文件大小超过限制大小','url'=>'','title'=>'','original'=>'']);
- // 调用本地存储方法
- $savepath = $this->save_storage_content($action,'png',$img);
- // 存储失败
- if ( isset($savepath['error']) ) return response()->json(['state'=>$savepath['error'],'url'=>'','title'=>'','original'=>'']);
- // 状态成功
- return response()->json(['state' => 'SUCCESS','url'=>$savepath,'title'=>'','original'=>'']);
- }
- //抓取远程文件
- private function _ueditor_upload_catch(){
- // 暂不支持
- return response()->json(['state' => '暂不支持抓取远程文件','list' => null]);
- }
- }
|