123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635 |
- <?php
- namespace app\commands;
- use yii\console\Controller;
- use app\common\components\Unzip;
- use Smalot\PdfParser\Parser;
- use Yii;
- class TransController extends Controller
- {
- public $logFile;
- public $transferDir;
- public $transferUrl;
- public $ossConfig;
- public $transferConfig;
- public $attachmentConfig;
- public $imgConfig;
- public $oss;
- public function init()
- {
- parent::init();
- $this->logFile = Yii::getAlias('@runtime').DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.'cmdtransfer_'.date("Ymd").'.log';
- $this->transferDir = UPLOAD_PATH.'transfer'.DIRECTORY_SEPARATOR;
- $this->transferUrl = UPLOAD_URL.'transfer/';
- $ossConfigResult = Yii::$app->db->createCommand("select * from {{%config}} where name='oss'")->queryOne();
- $this->ossConfig = string2array($ossConfigResult['value']);
- $transferConfigResult = Yii::$app->db->createCommand("select * from {{%config}} where name='transfer'")->queryOne();
- $this->transferConfig = string2array($transferConfigResult['value']);
- $attachmentConfigResult = Yii::$app->db->createCommand("select * from {{%config}} where name='attachment'")->queryOne();
- $this->attachmentConfig = string2array($attachmentConfigResult['value']);
- $imgConfigResult = Yii::$app->db->createCommand("select * from {{%config}} where name='imageconfig'")->queryOne();
- $this->imgConfig = string2array($imgConfigResult['value']);
- if($this->ossConfig['OPEN_OSS'])
- {
- $this->oss = new \app\common\components\Oss();
- $initResult = $this->oss->init($this->ossConfig['OPEN_INTERNAL']);
- if($initResult['error'])
- {
- exit("OSS初始化出错");
- }
- }
- }
- public function actionIndex()
- {
- $doc = Yii::$app->db->createCommand("select * from {{%doc}} where (trans_status=0 or trans_status=99) and is_delete=0 order by id desc limit 1")->queryOne();
- if(!empty($doc))
- {
- if($doc['trans_status']==99&&TIMESTAMP-$doc['transfer_start_time']>=$this->transferConfig['timeout'])
- {
- Yii::$app->db->createCommand("update {{%doc}} set trans_status=88 where id=".$doc['id'])->execute();
- $this->_saveLog('timeout',$doc);
- }
- else
- {
- //只对待转换文档处理
- if($doc['trans_status']==0)$this->_dotrans($doc['id']);
- }
- }
- }
- //执行转换
- private function _dotrans($id)
- {
- set_time_limit(0);
- $status = 5;//默认进入待自审状态
- $audit_bak = [];//审核备注
- $doc = Yii::$app->db->createCommand("select * from (select * from {{%doc}} where id=$id) a left join {{%doc_data}} b on a.id=b.id")->queryOne();
- $this->_saveLog('start',$doc);
- //获取用户组权限
- if($doc['user_id'])
- {
- $user = Yii::$app->db->createCommand("select * from {{%user}} where user_id=".$doc['user_id']."")->queryOne();
- if(!empty($user))
- {
- $userGroupLevel = Yii::$app->db->createCommand("select * from {{%user_group_level}} where id=".$user['user_group_level'])->queryOne();
- if($userGroupLevel)
- {
- $levelSettings = string2array($userGroupLevel['settings']);
- //免自审
- if($levelSettings['doc_auto_selfaudit']==1)
- {
- $status = 0;
- }
- //免审核,直接发布
- if($levelSettings['doc_auto_audit']==1)
- {
- $status = 1;
- }
- }
- }
- }
- //合辑,资源免转换
- if(in_array($doc['doc_type'],[2,3,4]))
- {
- Yii::$app->db->createCommand("update {{%doc}} set trans_status=1,transfer_end_time=".time().",status=$status where id=$id")->execute();
- $this->_saveLog('end',$doc);
- }
- else
- {
- $downResult = $this->_locateFile(getFileUrl($doc['file']),$doc['ext']);
- if($downResult['error']==0)
- {
- $transferFolder = $downResult['data'][0];
- $transferFile = $downResult['data'][1];
- $relativeFolder = $downResult['data'][2];
- $relativeFile = $downResult['data'][3];
- Yii::$app->db->createCommand("update {{%doc}} set trans_status=99,transfer_start_time=".time()." where id=$id")->execute();
- //图片
- if(in_array($doc['ext'],array('jpg','jpeg','gif','png','bmp'))){
- $imgResult = $this->_doimg($transferFolder,$transferFile);
- $imgcode = $imgResult['data']['imgcode'];
- Yii::$app->db->createCommand("update {{%doc}} set trans_status=1,transfer_end_time=".time().",status=$status,page_num = 1,preview_num=1,thumb='".$doc['file']."' where id=".$id)->execute();
- Yii::$app->db->createCommand("update {{%doc_data}} set imgcode='".json_encode($imgcode)."' where id=".$id)->execute();
- //封面图信息
- if(!empty($doc['file']))
- {
- Yii::$app->db->createCommand("insert into {{%attachment}} set status=1,file_path='".$doc['file']."',hash='".md5($doc['file'])."',is_used=1,upload_time=".time().",file_name='".basename($doc['file'])."'")->execute();
- Yii::$app->db->createCommand("insert into {{%attachment_index}} set table_name='doc',field='thumb',data_id=".$id.",a_id=".Yii::$app->db->getLastInsertID()."")->execute();
- }
- $result = array('error'=>0,'msg'=>'处理成功');
- }
- else if(in_array($doc['ext'],array('zip','rar')))
- {
- $packfiles = $this->_uncompress($transferFile,$id);
- if(!empty($packfiles)&&is_array($packfiles))
- {
- Yii::$app->db->createCommand("update {{%doc_data}} set packfiles = '".addslashes(array2string($packfiles))."' where id=".$id)->execute();
- Yii::$app->db->createCommand("update {{%doc}} set trans_status=1,transfer_end_time=".time().",status=$status where id=".$id)->execute();
- $result = array('error'=>0,'msg'=>'处理成功');
- }
- else
- {
- $result = array('error'=>1,'msg'=>'解压失败');
- $audit_bak[] = "解压失败";
- }
- }
- else if(in_array($doc['ext'],array('mp3','mp4','flv','wmv')))
- {
- $mediaResult = $this->_domedia($transferFolder,$transferFile);
- if($mediaResult['error']==0)
- {
- Yii::$app->db->createCommand("update {{%doc}} set trans_status=1,transfer_end_time=".time().", status=$status, page_num = ".$mediaResult['data']['page_num'].", preview_num=".$mediaResult['data']['preview_num']." where id=".$id)->execute();
- Yii::$app->db->createCommand("update {{%doc_data}} set h5code='".json_encode($mediaResult['data']['h5code'])."' where id=".$id)->execute();
- $result = array('error'=>0,'msg'=>'处理成功');
- }
- else
- {
- $result = array('error'=>1,'msg'=>'音视频处理失败');
- $audit_bak[] = "音视频处理失败";
- }
- }
- else if(in_array(strtolower($doc['ext']),array('ppt','pptx','dps'))&&$this->transferConfig['trans_type']=='localkiivo'&&$this->transferConfig['pptimg'])//使用知沃云ppt转图片
- {
- $pptResult = $this->_doppt($transferFolder,$transferFile,getFileUrl($doc['file']));
- if($pptResult['error']==0)
- {
- $page_num = $pptResult['data']['page_num'];
- $imgcode = $pptResult['data']['imgcode'];
- $preview_num = $pptResult['data']['preview_num'];
- if(empty($doc['thumb'])){
- Yii::$app->db->createCommand("update {{%doc}} set page_num = $page_num,preview_num=$preview_num, thumb='".$imgcode[0]."', trans_status=1,transfer_end_time=".time().", status=$status where id=".$id)->execute();
- }
- else
- {
- Yii::$app->db->createCommand("update {{%doc}} set page_num = $page_num,preview_num=$preview_num, trans_status=1,transfer_end_time=".time().", status=$status where id=".$id)->execute();
- }
- Yii::$app->db->createCommand("update {{%doc_data}} set imgcode='".json_encode($imgcode)."' where id=".$id)->execute();
- //封面图信息
- if(!empty($imgcode[0])&&empty($doc['thumb']))
- {
- Yii::$app->db->createCommand("insert into {{%attachment}} set status=1,file_path='".$imgcode[0]."',hash='".md5($imgcode[0])."',is_used=1,upload_time=".time().",file_name='".basename($imgcode[0])."'")->execute();
- Yii::$app->db->createCommand("insert into {{%attachment_index}} set table_name='doc',field='thumb',data_id=".$id.",a_id=".Yii::$app->db->getLastInsertID()."")->execute();
- }
- $result = array('error'=>0,'msg'=>'处理成功');
- }
- else
- {
- $result = $pptResult;
- }
- }
- else
- {
- //只有在本地转换或本地+知沃云转换时候,需要转成PDF
- if($this->transferConfig['trans_type']=='local'||$this->transferConfig['trans_type']=='localkiivo')
- {
- if(in_array(strtolower($doc['ext']),array('pdf')))
- {
- $pdfFile = $transferFile;
- }
- else
- {
- $pdfFile = $this->_toPdf($transferFolder,$transferFile);
- }
- }
- else//云转换
- {
- $pdfFile = $transferFile;
- }
- if($pdfFile)
- {
- if($this->transferConfig['trans_type']=='local'||$this->transferConfig['trans_type']=='localkiivo')
- {
- //如果设定为转成图片
- if($this->transferConfig['dist_type']=='img')
- {
- $pdfPageInfo = $this->_pdfPageInfo($pdfFile);
- $page_num = $pdfPageInfo['data']['pageNum'];
- $h5code = [];
- $fullcontent = '';
- $content = '';
- //重新计算预览页数
- $transferPage = $pdfPageInfo['data']['transferPage'];
- $preview_num = $this->_getPreviewNum($page_num,$transferPage);
- $imgResult = $this->_pdf2img($pdfFile,$transferPage);
- }
- else if($this->transferConfig['dist_type']=='h5')
- {
- $pdf2htmlResult = $this->_pdf2html($pdfFile);
- if($pdf2htmlResult['error']==0)
- {
- $page_num = $pdf2htmlResult['data']['pageNum'];
- $h5code = $pdf2htmlResult['data']['h5code'];
- $preview_num = $pdf2htmlResult['data']['preview_num'];
- $fullcontent = $pdf2htmlResult['data']['fullcontent'];
- $content = beforeProcessHtml($pdf2htmlResult['data']['content']);
- $imgResult = $this->_pdf2img($pdfFile);
- }
- else
- {
- //转换html失败,尝试转图片
- $page_num = $pdf2htmlResult['data']['pageNum'];
- $h5code = [];
- $fullcontent = '';
- $content = '';
- //重新计算预览页数
- $transferPage = $pdf2htmlResult['data']['transferPage'];
- $preview_num = $this->_getPreviewNum($page_num,$transferPage);
- $imgResult = $this->_pdf2img($pdfFile,$transferPage);
- }
- }
- else if($this->transferConfig['dist_type']=='all')
- {
- $pdf2htmlResult = $this->_pdf2html($pdfFile);
- if($pdf2htmlResult['error']==0)
- {
- $page_num = $pdf2htmlResult['data']['pageNum'];
- $h5code = $pdf2htmlResult['data']['h5code'];
- $preview_num = $pdf2htmlResult['data']['preview_num'];
- $fullcontent = $pdf2htmlResult['data']['fullcontent'];
- $content = beforeProcessHtml($pdf2htmlResult['data']['content']);
- $transferPage = $pdf2htmlResult['data']['transferPage'];
- $imgResult = $this->_pdf2img($pdfFile,$transferPage);
- }
- else
- {
- //转换html失败,尝试转图片
- $page_num = $pdf2htmlResult['data']['pageNum'];
- $h5code = [];
- $fullcontent = '';
- $content = '';
- //重新计算预览页数
- $transferPage = $pdf2htmlResult['data']['transferPage'];
- $preview_num = $this->_getPreviewNum($page_num,$transferPage);
- $imgResult = $this->_pdf2img($pdfFile,$transferPage);
- }
- }
- }
- else
- {
- //云转换
- $imgResult = $this->_doRemoteTrans($pdfFile);
- $page_num = $imgResult['data']['pageNum'];
- $h5code = [];
- $fullcontent = '';
- $content = '';
- $transferPage = $imgResult['data']['transferPage'];
- $preview_num = $this->_getPreviewNum($page_num,$transferPage);
- }
- if($imgResult['error']==0)
- {
- //生成封面
- $imgcode = $imgResult['data']['imgcode'];
- //页数超限
- if($page_num>50)
- {
- $status = 6;
- $audit_bak[] = '页数超限(超过50页需重点审核)';
- }
- //pdf二审
- if($doc['ext']=='pdf'){
- $status = 7;
- $audit_bak[] = 'PDF需要二审';
- }
- if(!empty($fullcontent))
- {
- //敏感词检测
- $sesResult = sensitive($fullcontent);
- if(!empty($sesResult))
- {
- if($sesResult['log2'])
- {
- $status = 8;//非法资源
- $audit_bak[] = $sesResult['log2'];
- }
- if($sesResult['log1'])
- {
- $status = 8;//非法资源
- $audit_bak[] = $sesResult['log1'];
- }
- if($sesResult['log3'])
- {
- $status = 8;//非法资源
- $audit_bak[] = $sesResult['log3'];
- }
- }
- }
- if(empty($doc['thumb']))
- {
- Yii::$app->db->createCommand("update {{%doc}} set page_num = $page_num,preview_num=$preview_num, thumb='".$imgcode[0]."', trans_status=1,transfer_end_time=".time().", status=$status where id=".$id)->execute();
- }
- else
- {
- Yii::$app->db->createCommand("update {{%doc}} set page_num = $page_num,preview_num=$preview_num, trans_status=1,transfer_end_time=".time().", status=$status where id=".$id)->execute();
- }
- Yii::$app->db->createCommand("update {{%doc_data}} set h5code='".json_encode($h5code)."',imgcode='".json_encode($imgcode)."',content='".addslashes($content)."' where id=".$id)->execute();
- //封面图信息
- if(!empty($imgcode[0])&&empty($doc['thumb']))
- {
- Yii::$app->db->createCommand("insert into {{%attachment}} set status=1,file_path='".$imgcode[0]."',hash='".md5($imgcode[0])."',is_used=1,upload_time=".time().",file_name='".basename($imgcode[0])."'")->execute();
- Yii::$app->db->createCommand("insert into {{%attachment_index}} set table_name='doc',field='thumb',data_id=".$id.",a_id=".Yii::$app->db->getLastInsertID()."")->execute();
- }
- $result = array('error'=>0,'msg'=>'处理成功');
- }
- else
- {
- $result = $imgResult;
- }
- }
- else
- {
- $result = array('error'=>1,'msg'=>'文件转化成PDF失败');
- }
- }
- }
- else
- {
- $result = array('error'=>1,'msg'=>'文件下载到本地失败');
- }
- }
- //标记失败状态
- if($result['error']==1)
- {
- Yii::$app->db->createCommand("update {{%doc}} set trans_status=88 where id=$id")->execute();
- }
- else
- {
- //转换日志
- Yii::$app->db->createCommand("delete from {{%doc_auditlog}} where doc_id=$id")->execute();
- if(is_array($audit_bak))foreach($audit_bak as $abak)
- {
- Yii::$app->db->createCommand("insert into {{%doc_auditlog}} set doc_id=$id,log='".$abak."',create_time=".time()."")->execute();
- }
- $this->_saveLog('end',$doc);
- //免审核,直接上架
- if($levelSettings['doc_auto_audit']==1&&$status==1)
- {
- //此处写上架代码
- $this->_autoAudit($id);
- }
- }
- }
- //转化成pdf
- private function _toPdf($transferFolder,$transferFile)
- {
- $baseFile = basename($transferFile);
- $pdfFile = str_replace(".".fileext($baseFile),".pdf",$baseFile);
- $targetFile = $transferFolder.$pdfFile;
- //云转换
- if($this->transferConfig['trans_type']=='localkiivo'&&in_array(fileext($transferFile),['doc','docx','wps','ppt','pptx','dps']))
- {
- $fileUrl = $this->transferUrl.str_replace(DIRECTORY_SEPARATOR,'/',str_replace($this->transferDir,'',$transferFile));
- $data = ['file'=>$fileUrl,'appkey'=>$this->transferConfig['cloudpwd'],'action'=>'topdf'];
- $options['time_out'] = 300;
- $result = https_request($this->transferConfig['cloudurl'],$options,$data);
- $result = json_decode($result,true);
- $pdf = $result['data']['pdf'];
- if(!empty($pdf)&&file_put_contents($targetFile,file_get_contents($pdf))){
- return $targetFile;
- }
- else
- {
- return false;
- }
- }
- else
- {
- $command = "cd $transferFolder&&/opt/libreoffice7.1/program/soffice --headless --invisible --convert-to pdf $baseFile";
- if(!empty($command))
- {
- //file_put_contents($this->logFile,$command."\r\n",FILE_APPEND);
- passthru($command,$return);
- if(file_exists($targetFile)&&$return==0)
- {
- return $targetFile;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- }
- //将pdf转化成h5
- private function _pdf2html($pdf)
- {
- $workPath = $this->_initWorkPath($pdf);
- $pageNum = $this->_getPdfPageNum($pdf);
- $pdfcontent = '';
- if($pageNum>=1)
- {
- if($this->transferConfig['fullpage'])
- {
- $transferPage = $pageNum;
- }
- else
- {
- if($this->transferConfig['transferpage_type']=='page')
- {
- $transferPage = $this->transferConfig['transferpage'];
- }
- else
- {
- $transferPagePercent = (float)$this->transferConfig['transferpage']/100;
- $transferPage = ceil($transferPagePercent*$pageNum);
- }
- }
- if($this->transferConfig['transfermaxpage']>0&&$transferPage>$this->transferConfig['transfermaxpage'])
- {
- $transferPage = $this->transferConfig['transfermaxpage'];
- }
- if($transferPage>$pageNum)$transferPage = $pageNum;
- if($transferPage==0)$transferPage = 1;
- $toHtmlCmd = "pdf2htmlEX -l $transferPage --split-pages 1 --dest-dir ".$workPath." --css-filename pdf.css --embed-css 0 --fit-width 800 --optimize-text 1 --process-outline 0 --use-cropbox 0 --page-filename page%d.html ".$pdf;
- //file_put_contents($this->logFile,$toHtmlCmd."\r\n",FILE_APPEND);
- passthru($toHtmlCmd,$return);
- if($return==0)
- {
- @unlink($workPath.'base.min.css');
- @unlink($workPath.'fancy.min.css');
- $h5code = [];
- $allFileList = [];
- $allFileList[] = $workPath.'pdf.css';
- for($i=1;$i<=$transferPage;$i++)
- {
- $allFileList[] = $workPath.'page'.$i.'.html';
- //如果页数太多,只取10页内容
- if($pageNum>10&&$i<=10)
- {
- $pdfcontent .= strip_tags(file_get_contents($workPath.'page'.$i.'.html'));
- }
- }
- if(is_array($allFileList))foreach ($allFileList as $unitFile)
- {
- if($this->ossConfig['OPEN_OSS'])
- {
- $result = $this->oss->Upload($unitFile,str_replace($this->transferDir,'',$unitFile));
- if($result['error']==0)
- {
- if($this->attachmentConfig['absolute_url'])
- {
- $h5code[] = str_replace($this->ossConfig['OSS_INTERNAL_ENDPOINT'],$this->ossConfig['OSS_ENDPOINT'],$result['data']['url']);
- }
- else
- {
- $h5code[] = $result['data']['path'];
- }
- }
- else
- {
- return [
- 'error' => 1,
- 'msg' => $result['msg']
- ];
- }
- }
- else
- {
- $relativeFile = str_replace($this->transferDir,'',$unitFile);
- $targetFile = UPLOAD_PATH.$relativeFile;
- dir_create(dirname($targetFile));
- if(copy($unitFile,$targetFile))
- {
- if($this->attachmentConfig['absolute_url'])
- {
- $h5code[] = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
- }
- else
- {
- $h5code[] = str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
- }
- }
- else
- {
- return [
- 'error' => 1,
- 'msg' => '复制失败'
- ];
- }
- }
- }
- //获取文字内容
- if($this->transferConfig['gettext'])
- {
- //如果页数少,直接用插件读取内容
- if($pageNum<=10)
- {
- $parser = new Parser();
- $pdfParer = $parser->parseFile($pdf);
- $pdfcontent = $pdfParer->getText();
- }
- $fullcontent = $pdfcontent;
- if(!empty($pdfcontent)&&$this->transferConfig['textlength']>0)
- {
- $pdfcontent = str_cut($pdfcontent,0,$this->transferConfig['textlength'],'');
- }
- }
- else
- {
- $pdfcontent = '';
- }
- if($this->transferConfig['previewfullpage'])
- {
- $preview_num = $pageNum;
- }
- else
- {
- if($this->transferConfig['previewmaxpage_type']=='page')
- {
- $preview_num = $this->transferConfig['previewmaxpage'];
- }
- else
- {
- $previewPagePercent = (float)$this->transferConfig['previewmaxpage']/100;
- $preview_num = ceil($previewPagePercent*$pageNum);
- }
- }
- if($preview_num>$pageNum)$preview_num = $pageNum;
- if($preview_num==0)$preview_num = 1;
- if($this->transferConfig['transfermaxpage']>0&&$preview_num>$this->transferConfig['transfermaxpage']){
- $preview_num = $this->transferConfig['transfermaxpage'];
- }
- if($preview_num>$transferPage)$preview_num = $transferPage;
- return [
- 'error' => 0,
- 'msg' => '处理成功',
- 'data'=>['h5code'=>$h5code,'pageNum'=>$pageNum,'transferPage'=>$transferPage,'preview_num'=>$preview_num,'fullcontent'=>$fullcontent,'content'=>$pdfcontent]
- ];
- }
- else
- {
- return [
- 'error' => 1,
- 'msg' => '转化html失败'
- ];
- }
- }
- else
- {
- return [
- 'error' => 1,
- 'msg' => '文档页数异常'
- ];
- }
- }
- //将PDF转换成图片
- private function _pdf2img($pdf,$transferPage=0)
- {
- $result = [];
- if(!extension_loaded('imagick'))
- {
- $result = array('error'=>1,'msg'=>'imagick加载失败');
- }
- if(!file_exists($pdf))
- {
- $result = array('error'=>1,'msg'=>'pdf不存在');
- }
- if(!is_readable($pdf))
- {
- $result = array('error'=>1,'msg'=>'pdf不可读');
- }
- if(empty($result))
- {
- $workPath = $this->_initWorkPath($pdf);//dirname($pdf).DIRECTORY_SEPARATOR.md5(basename($pdf)).DIRECTORY_SEPARATOR;
- dir_create($workPath);
- $imgcode = [];
- $im = new \Imagick();
- $im->setResolution(300, 300);
- $im->setCompressionQuality(100);
- //$page = 0;//只读取首页
- //$im->readImage($pdf."[".$page."]");
- if($transferPage==0)
- {
- $page = 0;//只读取首页
- $im->readImage($pdf."[".$page."]");
- }
- else
- {
- $im->readImage($pdf);
- }
- foreach ($im as $key => $var)
- {
- if($transferPage>0&&($key+1)>$transferPage)continue;
- $var->setImageFormat('png');
- $unitFile = $workPath.$key.'.png';
- if(@$var->writeImage($unitFile) == true)
- {
- //打水印
- if($this->attachmentConfig['open_watermark']&&!empty($this->imgConfig['watermark']))
- {
- $watermark = $this->_locateWatermark();
- set_img_water($unitFile,$watermark,$this->attachmentConfig['watermark_quality'],$this->attachmentConfig['watermark_pos']);
- }
- if($this->ossConfig['OPEN_OSS'])
- {
- $result = $this->oss->Upload($unitFile,str_replace($this->transferDir,'',$unitFile));
- if($result['error']==0)
- {
- if($this->attachmentConfig['absolute_url'])
- {
- $imgcode[] = str_replace($this->ossConfig['OSS_INTERNAL_ENDPOINT'],$this->ossConfig['OSS_ENDPOINT'],$result['data']['url']);
- }
- else
- {
- $imgcode[] = $result['data']['path'];
- }
- }
- else
- {
- return [
- 'error' => 1,
- 'msg' => $result['msg']
- ];
- }
- }
- else
- {
- $targetFile = str_replace($this->transferDir,'',$unitFile);
- dir_create(dirname(UPLOAD_PATH.$targetFile));
- if(copy($unitFile,UPLOAD_PATH.$targetFile))
- {
- if($this->attachmentConfig['absolute_url'])
- {
- $imgcode[] = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$targetFile);
- }
- else
- {
- $imgcode[] = str_replace(DIRECTORY_SEPARATOR,'/',$targetFile);
- }
- }
- else
- {
- return [
- 'error' => 1,
- 'msg' => '复制图片失败'
- ];
- }
- }
- }
- else
- {
- return array('error'=>1,'msg'=>'PDF转图片失败');
- }
- }
- $im->destroy();
- return array('error'=>0,'msg'=>'PDF转图片成功','data'=>['imgcode'=>$imgcode]);
- }
- else
- {
- return $result;
- }
- }
- //获取预览页数
- private function _getPreviewNum($pageNum,$transferPage)
- {
- if($this->transferConfig['previewfullpage'])
- {
- $preview_num = $pageNum;
- }
- else
- {
- if($this->transferConfig['previewmaxpage_type']=='page')
- {
- $preview_num = $this->transferConfig['previewmaxpage'];
- }
- else
- {
- $previewPagePercent = (float)$this->transferConfig['previewmaxpage']/100;
- $preview_num = ceil($previewPagePercent*$pageNum);
- }
- }
- if($preview_num>$pageNum)$preview_num = $pageNum;
- if($preview_num==0)$preview_num = 1;
- if($this->transferConfig['transfermaxpage']>0&&$preview_num>$this->transferConfig['transfermaxpage']){
- $preview_num = $this->transferConfig['transfermaxpage'];
- }
- if($preview_num>$transferPage)$preview_num = $transferPage;
- return $preview_num;
- }
- private function _getPdfPageNum($file) {
- if(!file_exists($file))
- {
- return $this->_getPdfPageNum($file);
- }
- else
- {
- exec(sprintf('gs -q -dNODISPLAY -c "(%s) (r) file runpdfbegin pdfpagecount = quit"', $file), $res, $ret);
- if(0 == $ret) {
- return (int) $res[0];
- }
- return 0;
- }
- }
- //获取PDF总页数和转换页数
- private function _pdfPageInfo($pdf)
- {
- $pageNum = $this->_getPdfPageNum($pdf);
- if($pageNum>=1)
- {
- if($this->transferConfig['fullpage'])
- {
- $transferPage = $pageNum;
- }
- else
- {
- if($this->transferConfig['transferpage_type']=='page')
- {
- $transferPage = $this->transferConfig['transferpage'];
- }
- else
- {
- $transferPagePercent = (float)$this->transferConfig['transferpage']/100;
- $transferPage = ceil($transferPagePercent*$pageNum);
- }
- }
- if($this->transferConfig['transfermaxpage']>0&&$transferPage>$this->transferConfig['transfermaxpage'])
- {
- $transferPage = $this->transferConfig['transfermaxpage'];
- }
- if($transferPage>$pageNum)$transferPage = $pageNum;
- if($transferPage==0)$transferPage = 1;
- return [
- 'error' => 0,
- 'msg' => '获取pdf页数成功',
- 'data'=>['pageNum'=>$pageNum,'transferPage'=>$transferPage]
- ];
- }
- else
- {
- return [
- 'error' => 1,
- 'msg' => '文档页数异常'
- ];
- }
- }
- //PPT云转换
- private function _doppt($transferFolder,$transferFile)
- {
- $ext = fileext($transferFile);
- $workPath = $this->_initWorkPath($transferFile);
- $fileUrl = $this->transferUrl.str_replace(DIRECTORY_SEPARATOR,'/',str_replace($this->transferDir,'',$transferFile));
- $data = ['file'=>$fileUrl,'appkey'=>$this->transferConfig['cloudpwd'],'action'=>'ppt2img'];
- $options['time_out'] = 300;
- $pptresult = https_request($this->transferConfig['cloudurl'],$options,$data);
- $pptresult = json_decode($pptresult,true);
- if($pptresult['error']==0)
- {
- $images = $pptresult['data']['imgs'];
- $imgcode = [];
- if(is_array($images))foreach($images as $image)
- {
- $imgfile = $workPath.basename($image);
- if(file_put_contents($imgfile,file_get_contents($image)))
- {
- $unitFile = $imgfile;
- //打水印
- if($this->attachmentConfig['open_watermark']&&!empty($this->imgConfig['watermark']))
- {
- $watermark = $this->_locateWatermark();
- set_img_water($unitFile,$watermark,$this->attachmentConfig['watermark_quality'],$this->attachmentConfig['watermark_pos']);
- }
- if($this->ossConfig['OPEN_OSS'])
- {
- $result = $this->oss->Upload($unitFile,str_replace($this->transferDir,'',$unitFile));
- if($result['error']==0)
- {
- if($this->attachmentConfig['absolute_url'])
- {
- $imgcode[] = str_replace($this->ossConfig['OSS_INTERNAL_ENDPOINT'],$this->ossConfig['OSS_ENDPOINT'],$result['data']['url']);
- }
- else
- {
- $imgcode[] = $result['data']['path'];
- }
- }
- else
- {
- return [
- 'error' => 1,
- 'msg' => $result['msg']
- ];
- }
- }
- else
- {
- $relativeFile = str_replace($this->transferDir,'',$unitFile);
- $targetFile = UPLOAD_PATH.$relativeFile;
- dir_create(dirname($targetFile));
- if(copy($unitFile,$targetFile))
- {
- if($this->attachmentConfig['absolute_url'])
- {
- $imgcode[] = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
- }
- else
- {
- $imgcode[] = str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
- }
- }
- else
- {
- return [
- 'error' => 1,
- 'msg' => 'PPT单元页上传失败'
- ];
- }
- }
- }
- }
- $page_num = count($imgcode);
- if($this->transferConfig['previewfullpage'])
- {
- $preview_num = $page_num;
- }
- else
- {
- if($this->transferConfig['previewmaxpage_type']=='page')
- {
- $preview_num = $this->transferConfig['previewmaxpage'];
- }
- else
- {
- $previewPagePercent = (float)$this->transferConfig['previewmaxpage']/100;
- $preview_num = ceil($previewPagePercent*$page_num);
- }
- }
- //预览页数不应该迟超过转换页数上线
- if($this->transferConfig['transfermaxpage']>0&&$preview_num>$this->transferConfig['transfermaxpage']){
- $preview_num = $this->transferConfig['transfermaxpage'];
- }
- if($preview_num>$page_num)$preview_num = $page_num;
- if($preview_num==0)$preview_num = 1;
- return [
- 'error' => 0,
- 'msg' => '处理成功',
- 'data'=>['imgcode'=>$imgcode,'page_num'=>$page_num,'preview_num'=>$preview_num]
- ];
- }
- else
- {
- return array('error'=>1,'msg'=>'PPT远程处理失败');
- }
- }
- private function _domedia($transferFolder,$transferFile)
- {
- $ext = fileext($transferFile);
- $workPath = $this->_initWorkPath($transferFile);
- $videoInfo = $this->_getVideoInfo($transferFile);
- //预览和截断时间(秒)
- if($videoInfo['all_time']<=30)
- {
- $preview_num = 30;
- }
- else
- {
- if($this->transferConfig['previewfullmedia'])
- {
- $preview_num = $videoInfo['all_time'];
- }
- else
- {
- if($this->transferConfig['mediatime_type']=='seconds')
- {
- $preview_num = $this->transferConfig['mediatime'];
- }
- else
- {
- $previewMediaPercent = (float)$this->transferConfig['mediatime']/100;
- $preview_num = ceil($previewMediaPercent*$videoInfo['all_time']);
- }
- }
- }
- if($preview_num>$videoInfo['all_time'])$preview_num = $videoInfo['all_time'];
- $cuttedFile = $workPath.md5(basename($transferFile)).'_'.$preview_num.'.'.$ext;
- $cuttedDir = dirname($cuttedFile);
- $cutto = $this->changeTimeType($preview_num);
- dir_create($cuttedDir);
- $cutVideoCmd = "/usr/bin/ffmpeg -i $transferFile -vcodec copy -acodec copy -ss 00:00:00 -to $cutto $cuttedFile -y";
- passthru($cutVideoCmd,$return);
- if($return==0)
- {
- $unitFile = $cuttedFile;
- $h5code = [];
- if($this->ossConfig['OPEN_OSS'])
- {
- $result = $this->oss->Upload($unitFile,str_replace($this->transferDir,'',$unitFile));
- if($result['error']==0)
- {
- if($this->attachmentConfig['absolute_url'])
- {
- $h5code[] = str_replace($this->ossConfig['OSS_INTERNAL_ENDPOINT'],$this->ossConfig['OSS_ENDPOINT'],$result['data']['url']);
- }
- else
- {
- $h5code[] = $result['data']['path'];
- }
- }
- else
- {
- return [
- 'error' => 1,
- 'msg' => $result['msg']
- ];
- }
- }
- else
- {
- $relativeFile = str_replace($this->transferDir,'',$unitFile);
- $targetFile = UPLOAD_PATH.$relativeFile;
- dir_create(dirname($targetFile));
- if(copy($unitFile,$targetFile))
- {
- if($this->attachmentConfig['absolute_url'])
- {
- $h5code[] = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
- }
- else
- {
- $h5code[] = str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
- }
- }
- else
- {
- return [
- 'error' => 1,
- 'msg' => '音视频文件上传失败'
- ];
- }
- }
- return [
- 'error' => 0,
- 'msg' => '处理成功',
- 'data'=>['h5code'=>$h5code,'page_num'=>$videoInfo['all_time'],'preview_num'=>$preview_num]
- ];
- }
- else
- {
- return array('error'=>1,'msg'=>'音视频裁剪失败');
- }
- }
- //获取文档总页数和转换页数
- private function _doRemoteTrans($file)
- {
- $workPath = $this->_initWorkPath($file);//dirname($pdf).DIRECTORY_SEPARATOR.md5(basename($pdf)).DIRECTORY_SEPARATOR;
- dir_create($workPath);
- $imgcode = [];
- if($this->transferConfig['OSS_ACCESS_KEY'])
- {
- //腾讯云
- $object = str_replace($this->transferDir,'',$file);
- $protocol = str_replace("://","",SITE_PROTOCOL);
- $ossClient = new \Qcloud\Cos\Client(
- array(
- 'region' => $this->transferConfig['OSS_REGION'],
- 'schema' => $protocol, //协议头部,默认为http
- 'credentials'=> array(
- 'secretId' => $this->transferConfig['OSS_ACCESS_KEY'] ,
- 'secretKey' => $this->transferConfig['OSS_ACCESS_SECRET'])));
- $fileContent = fopen($file, "rb");
- if ($fileContent) {
- $ossresult = $ossClient->putObject(array(
- 'Bucket' => $this->transferConfig['OSS_BUCKET'],
- 'Key' => $object,
- 'Body' => $fileContent));
- //远程文件地址
- $remoteFileUrl = SITE_PROTOCOL.$ossresult['Location'];
- }
- $pageInfo = $this->_getRemoteFilePage($remoteFileUrl);
- if($remoteFileUrl)
- {
- for($i=1;$i<=$pageInfo['data']['transferPage'];$i++)
- {
- $tempUrl = $remoteFileUrl.'?ci-process=doc-preview&page='.$i;
- $requestData = https_request($tempUrl);
- //写图片到平台存储
- $unitFile = $workPath.$i.'.jpg';
- if(file_put_contents($unitFile,$requestData))
- {
- //打水印
- if($this->attachmentConfig['open_watermark']&&!empty($this->imgConfig['watermark']))
- {
- $watermark = $this->_locateWatermark();
- set_img_water($unitFile,$watermark,$this->attachmentConfig['watermark_quality'],$this->attachmentConfig['watermark_pos']);
- }
- if($this->ossConfig['OPEN_OSS'])
- {
- $result = $this->oss->Upload($unitFile,str_replace($this->transferDir,'',$unitFile));
- if($result['error']==0)
- {
- if($this->attachmentConfig['absolute_url'])
- {
- $imgcode[] = str_replace($this->ossConfig['OSS_INTERNAL_ENDPOINT'],$this->ossConfig['OSS_ENDPOINT'],$result['data']['url']);
- }
- else
- {
- $imgcode[] = $result['data']['path'];
- }
- }
- else
- {
- return [
- 'error' => 1,
- 'msg' => $result['msg']
- ];
- }
- }
- else
- {
- $targetFile = str_replace($this->transferDir,'',$unitFile);
- dir_create(dirname(UPLOAD_PATH.$targetFile));
- if(copy($unitFile,UPLOAD_PATH.$targetFile))
- {
- if($this->attachmentConfig['absolute_url'])
- {
- $imgcode[] = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$targetFile);
- }
- else
- {
- $imgcode[] = str_replace(DIRECTORY_SEPARATOR,'/',$targetFile);
- }
- }
- else
- {
- return [
- 'error' => 1,
- 'msg' => '复制图片失败'
- ];
- }
- }
- }
- }
- //删除转换容器中的文件
- $ossClient->deleteObject(array(
- 'Bucket' => $this->transferConfig['OSS_BUCKET'], //存储桶名称
- 'Key' => $object
- ));
- return [
- 'error' => 0,
- 'msg' => '远程转换成功',
- 'data'=>['pageNum'=>$pageInfo['data']['pageNum'],'transferPage'=>$pageInfo['data']['transferPage'],'imgcode'=>$imgcode]
- ];
- }
- else
- {
- return array('error'=>1,'msg'=>'上传到对象存储失败');
- }
- }
- else
- {
- return array('error'=>1,'msg'=>'远程转换配置错误');
- }
- }
- //获取远程文件总页数
- private function _getRemoteFilePage($remoteFileUrl)
- {
- $max_page = 1000;
- for($i=1;$i<=$max_page;$i++)
- {
- $tempUrl = $remoteFileUrl.'?ci-process=doc-preview&page='.$i;
- $requestData = https_request($tempUrl);
- if($this->iss_xml($requestData))
- {
- //格式化XML
- libxml_disable_entity_loader(true);
- $requestObj = simplexml_load_string($requestData, 'SimpleXMLElement', LIBXML_NOCDATA);
- if($requestObj->Code=='NoSuchPage')
- {
- $pageNum = ($i-1);
- if($this->transferConfig['fullpage'])
- {
- $transferPage = $pageNum;
- }
- else
- {
- if($this->transferConfig['transferpage_type']=='page')
- {
- $transferPage = $this->transferConfig['transferpage'];
- }
- else
- {
- $transferPagePercent = (float)$this->transferConfig['transferpage']/100;
- $transferPage = ceil($transferPagePercent*$pageNum);
- }
- }
- if($this->transferConfig['transfermaxpage']>0&&$transferPage>$this->transferConfig['transfermaxpage'])
- {
- $transferPage = $this->transferConfig['transfermaxpage'];
- }
- if($transferPage>$pageNum)$transferPage = $pageNum;
- if($transferPage==0)$transferPage = 1;
- return [
- 'error' => 0,
- 'msg' => '获取页数成功',
- 'data'=>['pageNum'=>$pageNum,'transferPage'=>$transferPage]
- ];
- }
- }
- }
- }
- //判断是否是XML
- function iss_xml($content){ //判断返回的内容是不是 xml 格式
- $xml_parser = xml_parser_create();
- $res = xml_parse($xml_parser, $content, true);
- xml_parser_free($xml_parser); return $res;
- }
- //获取文件信息
- private function _getVideoInfo($file) {
- $command = " ffmpeg -i $file 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//";
- ob_start();
- passthru($command);
- $info = ob_get_contents();
- ob_end_clean();
- $data = array();
- $duration = explode(":",$info);
- $duration_in_seconds = $duration[0]*3600 + $duration[1]*60+ round($duration[2]);//转化为秒
- $data['all_time'] = $duration_in_seconds;
- $data['size'] = filesize($file); //文件大小
- return $data;
- }
- function changeTimeType($seconds)
- {
- if ($seconds > 3600) {
- $hours = intval($seconds / 3600);
- $time = $hours . ":" . gmstrftime('%M:%S', $seconds);
- } else {
- $time = gmstrftime('%H:%M:%S', $seconds);
- }
- return $time;
- }
- //解压缩压缩包文件
- private function _uncompress($transferFile,$id)
- {
- $ext = fileext($transferFile);
- $workPath = $this->_initWorkPath($transferFile);
- if($ext=='zip')
- {
- $z = new Unzip();
- $z->unzip($transferFile,$workPath, true, true);
- }
- else if($ext=='rar')
- {
- $rar_file = rar_open($transferFile) or die("Failed to open Rar archive");
- $entries = rar_list($rar_file);
- foreach ($entries as $entry) {
- $entry->extract($workPath);
- }
- rar_close($rar_file);
- }
- $filelist = $this->scanDir($workPath,$this->transferDir);
- if($this->transferConfig['previewpack'])
- {
- $this->initPackFile($filelist,$id);
- }
- return $filelist;
- }
- //初始化压缩包待转换数据
- function initPackFile($arr,$id)
- {
- $packexts = $this->transferConfig['packexts']?explode("|",$this->transferConfig['packexts']):['ppt','pptx','doc','docx','xls','xlsx','pdf','jpeg','jpg','png','gif','bmp','txt'];
- Yii::$app->db->createCommand("delete from {{%doc_packfile}} where doc_id=$id")->execute();
- if(is_array($arr))foreach($arr as $key=>$val) {
- if(is_array($arr[$key])) {
- $this->initPackFile($arr[$key],$id);
- }
- else
- {
- if(!in_array(fileext($arr[$key]),$packexts))continue;
- $unitFile = $this->transferDir.$arr[$key];
- if($this->ossConfig['OPEN_OSS'])
- {
- $result = $this->oss->Upload($unitFile,str_replace($this->transferDir,'',$unitFile));
- if($result['error']==0)
- {
- if($this->attachmentConfig['absolute_url'])
- {
- $realPath = str_replace($this->ossConfig['OSS_INTERNAL_ENDPOINT'],$this->ossConfig['OSS_ENDPOINT'],$result['data']['url']);
- }
- else
- {
- $realPath = $result['data']['path'];
- }
- }
- }
- else
- {
- $relativeFile = str_replace($this->transferDir,'',$unitFile);
- $targetFile = UPLOAD_PATH.$relativeFile;
- $targetPath = dirname($targetFile);
- dir_create($targetPath);
- if(copy($unitFile,$targetFile))
- {
- if($this->attachmentConfig['absolute_url'])
- {
- $realPath = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
- }
- else
- {
- $realPath = str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
- }
- }
- }
- if(!empty($realPath))
- {
- $ext = fileext($arr[$key]);
- Yii::$app->db->createCommand("insert into {{%doc_packfile}} set doc_id=$id,name='".addslashes(basename($arr[$key],'.'.$ext))."',ext='".$ext."',path='".addslashes($arr[$key])."',path_md5='".md5($arr[$key])."',realpath='".addslashes($realPath)."'")->execute();
- }
- }
- }
- }
- //获取压缩包目录树(全路径)
- function scanDir($pathName,$transferDir) {
- //将结果保存在result变量中
- $result = array();
- $temp = array();
- //判断传入的变量是否是目录
- if(!is_dir($pathName) || !is_readable($pathName)) {
- return null;
- }
- $pathName = rtrim($pathName,'/');
- //取出目录中的文件和子目录名,使用scandir函数
- $allFiles = scandir($pathName);
- //遍历他们
- foreach($allFiles as $fileName) {
- //判断是否是.和..因为这两个东西神马也不是。。。
- if(in_array($fileName, array('.', '..'))) {
- continue;
- }
- //路径加文件名
- $fullName = $pathName.'/'.$fileName;
- //如果是目录的话就继续遍历这个目录
- if(is_dir($fullName)) {
- //将这个目录中的文件信息存入到数组中
- $result[$fullName] = $this->scanDir($fullName,$transferDir);
- }else {
- //如果是文件就先存入临时变量
- $temp[] = $fullName;
- }
- }
- //取出文件
- if($temp) {
- foreach($temp as $f) {
- $result[] = $f;
- }
- }
- $result = string2array(str_replace($transferDir,'',array2string($result)));
- return $result;
- }
- //处理图片
- private function _doimg($transferFolder,$transferFile)
- {
- $workPath = $this->_initWorkPath($transferFile);
- $newname = time();
- $unitFile = $workPath.$newname.'.'.fileext($transferFile);
- copy($transferFile,$unitFile);
- //$resize = new \app\common\components\ResizeImage($transferFile, 2000, 2000,$newname, 0,0,$workPath);
- //$unitFile = $resize->dstimg;
- //打水印
- if($this->attachmentConfig['open_watermark']&&!empty($this->imgConfig['watermark']))
- {
- $watermark = $this->_locateWatermark();
- set_img_water($unitFile,$watermark,$this->attachmentConfig['watermark_quality'],$this->attachmentConfig['watermark_pos']);
- }
- if($this->ossConfig['OPEN_OSS'])
- {
- $result = $this->oss->Upload($unitFile,str_replace($this->transferDir,'',$unitFile));
- if($result['error']==0)
- {
- if($this->attachmentConfig['absolute_url'])
- {
- $imgcode[] = str_replace($this->ossConfig['OSS_INTERNAL_ENDPOINT'],$this->ossConfig['OSS_ENDPOINT'],$result['data']['url']);
- }
- else
- {
- $imgcode[] = $result['data']['path'];
- }
- }
- else
- {
- return [
- 'error' => 1,
- 'msg' => $result['msg']
- ];
- }
- }
- else
- {
- $relativeFile = str_replace($this->transferDir,'',$unitFile);
- $targetFile = UPLOAD_PATH.$relativeFile;
- $targetPath = dirname(UPLOAD_PATH.$relativeFile);
- dir_create($targetPath);
- if(copy($unitFile,$targetFile))
- {
- if($this->attachmentConfig['absolute_url'])
- {
- $imgcode[] = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
- }
- else
- {
- $imgcode[] = str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
- }
- }
- else
- {
- return [
- 'error' => 1,
- 'msg' => '转换图片失败'
- ];
- }
- }
- return array('error'=>0,'msg'=>'图片转换成功','data'=>['imgcode'=>$imgcode]);
- }
- //本地化水印图片
- private function _locateWatermark()
- {
- $watermarkPath = UPLOAD_PATH.'watermark'.DIRECTORY_SEPARATOR;
- dir_create($watermarkPath);
- $watermark = $watermarkPath.basename($this->imgConfig['watermark']);
- //本地存储水印
- if(!file_exists($watermark))
- {
- file_put_contents($watermark,file_get_contents(getFileUrl($this->imgConfig['watermark'])));
- }
- return $watermark;
- }
- //根据临时转换文件路径,生成目标子目录
- private function _initWorkPath($transferFile)
- {
- $ext = fileext($transferFile);
- $basename = basename($transferFile,'.'.$ext);
- $workPath = dirname($transferFile).DIRECTORY_SEPARATOR.md5($basename).DIRECTORY_SEPARATOR;
- dir_create($workPath);
- return $workPath;
- }
- //下载文件到转码目录
- private function _locateFile($file,$ext)
- {
- if(!check_remote_file_exists($file))return array('error'=>1,'msg'=>'文件不存在');
- $result = parse_url($file);
- $result['path'] = str_replace('/upload/','',$result['path']);
- $relativeFile = ltrim(str_replace('/',DIRECTORY_SEPARATOR,$result['path']),DIRECTORY_SEPARATOR);
- $relativeFolder = dirname($relativeFile).DIRECTORY_SEPARATOR;
- $transferFile = $this->transferDir.$relativeFile;
- $transferFolder = dirname($transferFile).DIRECTORY_SEPARATOR;
- dir_create($transferFolder);
- if(file_exists($transferFile))
- {
- return array('error'=>0,'msg'=>'下载成功','data'=>[$transferFolder,$transferFile,$relativeFolder,$relativeFile]);
- }
- if($this->ossConfig['OPEN_OSS'])
- {
- $downresult = $this->oss->downLoad($transferFile,$relativeFile);
- if($downresult['error']==0)
- {
- return array('error'=>0,'msg'=>'下载成功','data'=>[$transferFolder,$transferFile,$relativeFolder,$relativeFile]);
- }
- else
- {
- return array('error'=>1,'msg'=>$downresult['msg']);
- }
- }
- else
- {
- if(is_net_file($file))
- {
- file_put_contents($transferFile,file_get_contents($file));
- }
- else
- {
- file_put_contents($transferFile,file_get_contents(UPLOAD_URL.ltrim($result['path'],'/')));
- }
- return array('error'=>0,'msg'=>'下载成功','data'=>[$transferFolder,$transferFile,$relativeFolder,$relativeFile]);
- }
- }
- //写日志
- private function _saveLog($title,$doc)
- {
- file_put_contents($this->logFile,$doc['title']."(".$title."-".get_date(time(),'Y-m-d H:i:s').")\r\n",FILE_APPEND);
- }
- //自动上架
- private function _autoAudit($id)
- {
- $doc = Yii::$app->db->createCommand("select * from {{%doc}} where id=".$id)->queryOne();
- $docData = Yii::$app->db->createCommand("select * from {{%doc_data}} where id=".$id)->queryOne();
- //把表从草稿表复制到正式表
- $docFields = [];
- if(is_array($doc))foreach($doc as $k=>$v)
- {
- if($k=='id'||$k=='status')continue;
- if($k=='description')
- {
- //文档摘要处理
- if((empty($doc['description'])||$doc['title']==$doc['description'])&&!empty($docData['content']))
- {
- $length = $this->transferConfig['desclength']>0?$this->transferConfig['desclength']:'350';
- $description = str_cut(removeHtml($docData['content'],0),0,$length);
- }
- else
- {
- $description = $doc['description'];
- }
- $docFields[] = "description ='".$description."'";
- }
- else
- {
- $docFields[] = "$k ='".$v."'";
- }
- }
- $docFields[] = "status=1";
- $result = Yii::$app->db->createCommand("insert into {{%doc_real}} set ".join(",",$docFields))->execute();
- $newId = getLastInsertId();
- if($result)
- {
- $docDataFields= [];
- if(is_array($docData))foreach($docData as $k=>$v)
- {
- if($k=='id'||$k=='hash')continue;
- if($k=='file'&&$doc['doc_type']==1)
- {
- $hash = randString(8);
- $docDataFields[] = "hash='".$hash."'";
- $docDataFields[] = "file='".sys_auth($docData['file'],'ENCODE',$hash)."'";
- }
- else
- {
- $docDataFields[] = "$k ='".$v."'";
- }
- }
- $docDataFields[] = "id='".$newId."'";
- if($doc['doc_type']==1&&!in_array($doc['ext'],['rar','zip','mp3','mp4']))$docDataFields[] = "content=''";
- $result1 = Yii::$app->db->createCommand("insert into {{%doc_real_data}} set ".join(",",$docDataFields))->execute();
- if($result1)
- {
- //更新附件关联信息,将草稿表的记录改成正式表
- $sql = "update {{%attachment_index}} set data_id=$newId,table_name='doc_real' where table_name='doc' and field='thumb' and data_id=$id";
- Yii::$app->db->createCommand($sql)->execute();
- $sql = "update {{%attachment_index}} set data_id=$newId,table_name='doc_real_data' where table_name='doc_data' and field='file' and data_id=$id";
- Yii::$app->db->createCommand($sql)->execute();
- //更新tag标签.将草稿表的记录改成正式表
- $sql = "update {{%tag_data}} set data_id=$newId,table_name='doc_real' where table_name='doc' and data_id=$id";
- Yii::$app->db->createCommand($sql)->execute();
- //更新审核记录表
- $sql = "update {{%doc_auditlog}} set doc_real_id=$newId where doc_id=$id";
- Yii::$app->db->createCommand($sql)->execute();
- //更新压缩包细节目录
- $sql = "update {{%doc_packfile}} set doc_real_id=$newId where doc_id=$id";
- Yii::$app->db->createCommand($sql)->execute();
- //更新合辑
- if($doc['doc_type']==2)
- {
- $sql = "update {{%doc_col}} set col_id=$newId where col_id=$id";
- Yii::$app->db->createCommand($sql)->execute();
- }
- $sql = "delete from {{%doc}} where id=$id";
- $dResult = Yii::$app->db->createCommand($sql)->execute();
- $sql = "delete from {{%doc_data}} where id=$id";
- $dResult1 = Yii::$app->db->createCommand($sql)->execute();
- //把草稿删除
- if(!$dResult||!$dResult1)
- {
- return ['error' => 1,'msg' => '操作失败'];
- }
- else
- {
- return ['error' => 0,'msg' => '操作成功'];
- }
- }
- else
- {
- return ['error' => 1,'msg' => '操作失败'];
- }
- }
- else
- {
- return ['error' => 1,'msg' => '操作失败'];
- }
- }
- }
|