|
|
@@ -0,0 +1,325 @@
|
|
|
+<?php
|
|
|
+namespace app\weixin\model;
|
|
|
+use cmf\lib\Upload;
|
|
|
+use think\Model;
|
|
|
+use think\Request;
|
|
|
+
|
|
|
+class Stroage extends Model
|
|
|
+{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传图片
|
|
|
+ * @param $formId 图片表单名称
|
|
|
+ * @param $scene 应用场景
|
|
|
+ * @return array|string
|
|
|
+ */
|
|
|
+ public static function uploadImg($file, $scene='default'){
|
|
|
+ $data = [];
|
|
|
+ $path = "upload/{$scene}";
|
|
|
+ if(!is_dir($path)){
|
|
|
+ mkdir($path, 0755, true);
|
|
|
+ }
|
|
|
+ $fileSize = config('files.imageSize');
|
|
|
+ $fileSize = $fileSize? $fileSize*1024*1024 : 10*1024*1024;
|
|
|
+ $info = $file->validate(['size'=>$fileSize,'ext'=>'jpg,jpeg,png,gif'])->move('./'.$path);
|
|
|
+ if($info){
|
|
|
+ $filename = str_replace('\\','/', $info->getSaveName());
|
|
|
+ $fileInfo = $info->getInfo();
|
|
|
+ $name = isset($fileInfo['name'])? $fileInfo['name'] : '';
|
|
|
+ $fileType = cmf_get_file_extension($name);
|
|
|
+ $data = [
|
|
|
+ 'name'=> $name,
|
|
|
+ 'file'=> "{$scene}/".$filename,
|
|
|
+ 'preview'=> cmf_get_image_preview_url("{$scene}/".$filename),
|
|
|
+ 'file_type'=> $fileType,
|
|
|
+ 'file_size'=> isset($fileInfo['size'])? getFileSize($fileInfo['size']) : 0,
|
|
|
+ 'md5_key'=> md5_file(cmf_get_image_preview_url("{$scene}/".$filename)),
|
|
|
+ 'total_page'=> 1,
|
|
|
+ ];
|
|
|
+ }else{
|
|
|
+ return $info;
|
|
|
+ }
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传base图片
|
|
|
+ * @param $baseList
|
|
|
+ * @param string $scene
|
|
|
+ * @return bool|int|string
|
|
|
+ */
|
|
|
+ public static function uploadBaseImg($baseList, $scene='default'){
|
|
|
+ $data = [];
|
|
|
+ $path = "{$scene}/".date('Ymd').'/';
|
|
|
+ foreach($baseList as $k => $item){
|
|
|
+ if(!is_dir('/public/'.$path)){
|
|
|
+ mkdir('/public/'.$path, 755, true);
|
|
|
+ }
|
|
|
+ $filename = $path.'P_'.md5(uniqid().time().$k).'.jpg';
|
|
|
+ $src = isset($item['src'])? $item['src'] : '';
|
|
|
+ if($src && !preg_match("/default//", $src) && file_put_contents('/public/'.$filename, $src)){
|
|
|
+ $data[] = [
|
|
|
+ 'name'=> isset($item['name'])? trim($item['name']) : '',
|
|
|
+ 'url'=> $filename,
|
|
|
+ 'nums'=> isset($item['nums'])? intval($item['nums']) : 1,
|
|
|
+ 'total_page'=> 1,
|
|
|
+ 'file_size'=> isset($item['file_size'])? intval($item['file_size']) : 0,
|
|
|
+ 'file_type'=> isset($item['file_type'])? ltrim($item['file_type'],'image/') : '',
|
|
|
+ 'type'=> isset($item['type'])? trim($item['type']) : '',
|
|
|
+ 'md5_key'=> md5_file(cmf_get_image_preview_url($filename)),
|
|
|
+ 'created_at'=> date('Y-m-d H:i:s')
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if($data){
|
|
|
+ return FileLogs::insertAll($data);
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传图片
|
|
|
+ * @param $formId 图片表单名称
|
|
|
+ * @param $scene 应用场景
|
|
|
+ * @return array|string
|
|
|
+ */
|
|
|
+ public static function uploadFile($file, $scene='default'){
|
|
|
+ $data = [];
|
|
|
+ $path = "upload/files/{$scene}";
|
|
|
+ if(!is_dir($path)){
|
|
|
+ mkdir($path, 0755, true);
|
|
|
+ }
|
|
|
+ $fileTypes = config('files.fileTypes');
|
|
|
+ $fileSize = config('files.fileSize');
|
|
|
+ $fileSize = $fileSize? $fileSize*1024*1024 : 30*1024*1024;
|
|
|
+ $fileTypes = $fileTypes? $fileTypes : ['doc','docx','xls','xlsx','ppt','pptx','pdf'];
|
|
|
+ $info = $file->validate(['size'=> $fileSize, 'ext'=> implode(',', $fileTypes)])->move('./'.$path);
|
|
|
+ if($info){
|
|
|
+ $filename = str_replace('\\','/', $info->getSaveName());
|
|
|
+ $fileInfo = $info->getInfo();
|
|
|
+ $name = isset($fileInfo['name'])? $fileInfo['name'] : '';
|
|
|
+ $fileType = cmf_get_file_extension($name);
|
|
|
+ $file = "files/{$scene}/".$filename;
|
|
|
+ if(Stroage::checkFileExists($file)){
|
|
|
+ $page = Stroage::getFilePage($file);
|
|
|
+ if(in_array($fileType,['xls','xlsx'])){
|
|
|
+ $file = str_replace('.'.$fileType,'.pdf', $file);
|
|
|
+ }
|
|
|
+ $data = [
|
|
|
+ 'name'=> $name,
|
|
|
+ 'file'=> $file,
|
|
|
+ 'preview'=> cmf_get_image_preview_url($file),
|
|
|
+ 'file_type'=> $fileType,
|
|
|
+ 'file_size'=> isset($fileInfo['size'])? getFileSize($fileInfo['size']) : 0,
|
|
|
+ 'md5_key'=> md5_file(cmf_get_image_preview_url($file)),
|
|
|
+ 'total_page'=> $page,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证上传文件是否存在
|
|
|
+ * @param $file
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public static function checkFileExists($file){
|
|
|
+ $file = preg_match("/^files/", $file)? UPLOAD_PATH.$file : $file;
|
|
|
+ return is_file($file);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 打包文件为ZIP
|
|
|
+ * @param $file 文件
|
|
|
+ * @param $fileType
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public static function makeZipFile($file, $fileType){
|
|
|
+ $filename = str_replace('.'.$fileType,'.zip', $file);
|
|
|
+ $zip = new \ZipArchive();
|
|
|
+ $file = UPLOAD_PATH.$file;
|
|
|
+ $zip->open(UPLOAD_PATH.$filename,\ZipArchive::CREATE); //打开压缩包
|
|
|
+ $zip->addFile($file, basename($file)); //向压缩包中添加文件
|
|
|
+ $zip->close(); //关闭压缩包
|
|
|
+ return $filename;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改文件后缀为zip压缩包
|
|
|
+ * @param $file 文件
|
|
|
+ * @param $fileType 文件类型
|
|
|
+ * @return bool|mixed
|
|
|
+ */
|
|
|
+ public static function moveFileToZip($file, $fileType=''){
|
|
|
+ $fileType = getFileType($file);
|
|
|
+ $file = preg_match("/^files/", $file)? UPLOAD_PATH.$file : $file;
|
|
|
+ $filename = str_replace('.'.$fileType,'.zip', $file);
|
|
|
+ if(copy($file, $filename)){
|
|
|
+ return str_replace('\\','/', $filename);;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解压读取ZIP内指定文件
|
|
|
+ * @param $file ZIP压缩文件
|
|
|
+ * @return bool|string
|
|
|
+ */
|
|
|
+ public static function getZipPageFile($file){
|
|
|
+ $zip = new \ZipArchive();
|
|
|
+ $file = preg_match("/^files/", $file)? UPLOAD_PATH.$file : $file;
|
|
|
+ // 中文文件名要使用ANSI编码的文件格式
|
|
|
+ if ($zip->open($file) === TRUE) {
|
|
|
+ //提取全部文件
|
|
|
+ $file = str_replace('.zip','', $file);
|
|
|
+ $zip->extractTo($file); //提取部分文件
|
|
|
+ $zip->close();
|
|
|
+ return str_replace('\\','/', $file.'/docProps/app.xml');
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除文件
|
|
|
+ * @param $file
|
|
|
+ */
|
|
|
+ public static function deleteFile($file){
|
|
|
+ $file = preg_match("/^files/", $file)? UPLOAD_PATH.$file : $file;
|
|
|
+ if(is_file($file)){
|
|
|
+ unlink($file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除ZIP压缩包以及其解压文件
|
|
|
+ * @param $file 压缩奥文件
|
|
|
+ */
|
|
|
+ public static function deleteZip($file){
|
|
|
+ if(is_file($file)){
|
|
|
+ Stroage::deleteFile($file);
|
|
|
+ }
|
|
|
+ $zipDir = str_replace('.zip','', $file);
|
|
|
+ if(is_dir($zipDir)){
|
|
|
+ deleteDir($zipDir);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文档页数
|
|
|
+ * @param $file 文件
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ public static function getFilePage($file){
|
|
|
+ set_time_limit(0);
|
|
|
+ $fileType = getFileType($file);
|
|
|
+
|
|
|
+ // PDF 类型文件
|
|
|
+ if(in_array($fileType, ['pdf'])){
|
|
|
+ return Stroage::getPdfFilePage($file);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 表格文件转PDF
|
|
|
+ if(in_array($fileType, ['xls','xlsx','doc','docx'])){
|
|
|
+ //if(in_array($fileType, ['xls','xlsx'])){
|
|
|
+ $file = Stroage::convertFile($file);
|
|
|
+ return Stroage::getPdfFilePage($file);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 其他文件
|
|
|
+ $fileTypes = config('files.fileTypes');
|
|
|
+ $fileTypes = $fileTypes? $fileTypes : ['doc','docx','ppt','pptx'];
|
|
|
+ //$fileTypes = $fileTypes? $fileTypes : ['ppt','pptx'];
|
|
|
+ if(!in_array($fileType, $fileTypes)){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ $zipFile = Stroage::moveFileToZip($file);
|
|
|
+ if(!is_file($zipFile)){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取页数文件内容
|
|
|
+ $pageFile = Stroage::getZipPageFile($zipFile);
|
|
|
+
|
|
|
+ //var_dump($pageFile);
|
|
|
+ if(!is_file($pageFile)){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ $content = file_get_contents($pageFile);
|
|
|
+ if(empty($content)){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ preg_match("/(<Pages>([1-9][0-9]*):?<\/Pages>)|(<Slides>([1-9][0-9]*):?<\/Slides>)|(<vt:i4>([1-9][0-9]*):?<\/vt:i4>)/", $content, $result);
|
|
|
+ $page = $result? intval(end($result)) : 0;
|
|
|
+
|
|
|
+ // 删除处理生成临时压缩包文件
|
|
|
+ $deleteTemp = config('print.deleteTemp');
|
|
|
+ if($deleteTemp){
|
|
|
+ Stroage::deleteZip($zipFile);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $page;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件转换
|
|
|
+ * @param $file 文件
|
|
|
+ * @param string $type 转换类型
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public static function convertFile($file, $type='pdf'){
|
|
|
+ $fileType = getFileType($file);
|
|
|
+ $pathFile = preg_match("/^files/", $file)? UPLOAD_PATH.$file : $file;
|
|
|
+ $path = dirname($pathFile).'/';
|
|
|
+ $filename = str_replace('.'.$fileType,'.pdf', $file);
|
|
|
+ switch($type){
|
|
|
+ case 'pdf':
|
|
|
+ $command = "libreoffice6.3 --invisible --convert-to pdf --outdir \"{$path}\" \"{$pathFile}\"";
|
|
|
+// $command = "libreoffice6.3 --invisible --convert-to pdf:writer_pdf_Export --outdir \"{$path}\" \"{$pathFile}\"";
|
|
|
+ exec($command);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ //echo $command;
|
|
|
+ return $filename;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取PDF文件页数
|
|
|
+ * @param $file 文件
|
|
|
+ * @return bool|int
|
|
|
+ */
|
|
|
+ public static function getPdfFilePage($file){
|
|
|
+ $file = preg_match("/^files/", $file)? UPLOAD_PATH.$file : $file;
|
|
|
+ if(!is_readable($file) || !file_exists($file)){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ $max=0;
|
|
|
+ $fp = @fopen($file,"r");
|
|
|
+ if(!$fp){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ while(!feof($fp)) {
|
|
|
+ $line = fgets($fp,255);
|
|
|
+ echo "++".$line."++\n\n";
|
|
|
+ if (preg_match('/\/Count [0-9]+/', $line, $matches)){
|
|
|
+ preg_match('/[0-9]+/',$matches[0], $matches2);
|
|
|
+ if ($max<$matches2[0]) $max=$matches2[0];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fclose($fp);
|
|
|
+
|
|
|
+ return $max;
|
|
|
+ }
|
|
|
+}
|