uptoxinhudocAction.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * 文件同步上传到文件平台
  4. * php task.php uptoxinhudoc,run -fileid=1
  5. */
  6. class uptoxinhudocClassAction extends runtAction
  7. {
  8. public function initAction()
  9. {
  10. exit('此文件已经不需要');
  11. }
  12. /**
  13. * 发送上传文件
  14. */
  15. public function runAction()
  16. {
  17. $fileid = (int)$this->getparams('fileid','0'); //文件ID
  18. if($fileid<=0)return 'error fileid';
  19. $frs = m('file')->getone($fileid);
  20. if(!$frs)return 'filers not found';
  21. $filepath = $frs['filepath'];
  22. if(substr($filepath, 0, 4)=='http')return 'filepath is httppath';
  23. $msg = $this->sendpath($filepath, $frs);
  24. if($msg)return $msg;
  25. return 'success';
  26. }
  27. private function sendpath($filepath, $frs)
  28. {
  29. $path = ROOT_PATH.'/'.$filepath;
  30. if(!file_exists($path))return 'filepath not exists';
  31. $lx = 'file';
  32. if(getconfig('xinhudoc_diskops'))$lx='path';
  33. $url = $this->upfileurl($lx);
  34. //文件远程上传的
  35. if($lx=='file'){
  36. $data = base64_encode(file_get_contents($path));
  37. $params = array(
  38. 'filename' => $frs['filename'],
  39. 'optname' => $frs['optname'],
  40. 'adddt' => $frs['adddt'],
  41. 'fileext' => $frs['fileext'],
  42. 'filesize' => $frs['filesize'],
  43. 'filepath' => $frs['filepath'],
  44. 'table' => ''.DB_BASE.'.file',
  45. 'mid' => $frs['id'],
  46. 'optid' => $frs['optid'],
  47. );
  48. $canstr = $this->jm->base64encode(json_encode($params));
  49. $url .= '&sendtype=file&paramsstr='.$canstr.'';
  50. $result = c('curl')->postcurl($url, $data);
  51. $msg = $this->chuliresult($result, $frs, 1);
  52. if(!$msg){
  53. $this->sendother($frs['thumbpath'], 'thumb', $frs,2);
  54. $this->sendother($frs['pdfpath'], 'pdf', $frs,3);
  55. }
  56. return $msg;
  57. }
  58. //同服务器用复制的
  59. $result = c('curl')->postcurl($url, array(
  60. 'rootpath' => ROOT_PATH,
  61. 'filepath' => $frs['filepath'],
  62. 'fileext' => $frs['fileext'],
  63. 'filename' => $frs['filename'],
  64. 'filesize' => $frs['filesize'],
  65. 'pdfpath' => $frs['pdfpath'],
  66. 'thumbpath' => $frs['thumbpath'],
  67. 'optname' => $frs['optname'],
  68. 'adddt' => $frs['adddt'],
  69. 'table' => ''.DB_BASE.'.file',
  70. 'mid' => $frs['id'],
  71. 'optid' => $frs['optid'],
  72. ));
  73. return $this->chuliresult($result, $frs, 0);
  74. }
  75. private function sendother($filepath, $type, $frs, $lx)
  76. {
  77. if(isempt($filepath))return;
  78. $path = ROOT_PATH.'/'.$filepath;
  79. if(!file_exists($path))return;
  80. $url = $this->upfileurl('file');
  81. $data = base64_encode(file_get_contents($path));
  82. $params = array(
  83. 'filepath' => $filepath,
  84. 'table' => ''.DB_BASE.'.file',
  85. 'mid' => $frs['id'],
  86. );
  87. $canstr = $this->jm->base64encode(json_encode($params));
  88. $url .= '&sendtype='.$type.'&paramsstr='.$canstr.'';
  89. $result = c('curl')->postcurl($url, $data);
  90. return $this->chuliresult($result, $frs, $lx);
  91. }
  92. private function chuliresult($result, $frs, $lx)
  93. {
  94. $barr = c('xinhudoc')->returnresult($result);
  95. if($barr['success']){
  96. $data = $barr['data'];
  97. $uarr = array();
  98. if($lx==0 || $lx==1){
  99. $filenum = $data['filenum'];
  100. $uarr['filenum'] = $filenum;
  101. }
  102. //缩略图必须保存对应路径
  103. if($lx==0 || $lx==2){
  104. $thumbpath = arrvalue($data, 'thumbpath');
  105. if(!isempt($thumbpath)){
  106. $uarr['thumbplat'] = '{PLATURL}'.$thumbpath;
  107. }
  108. }
  109. //自动删除文件
  110. if(getconfig('autoup_localdbool')){
  111. if($lx==0 || $lx==1)$this->delfilelocal($frs['filepath']);
  112. if($lx==0 || $lx==2)$this->delfilelocal($frs['thumbpath']);
  113. if($lx==0 || $lx==3)$this->delfilelocal($frs['pdfpath']);
  114. }
  115. if($uarr)m('file')->update($uarr, $frs['id']); //更新
  116. }else{
  117. return $barr['msg'];
  118. }
  119. return '';
  120. }
  121. private function delfilelocal($path)
  122. {
  123. if(isempt($path))return;
  124. $path = ROOT_PATH.'/'.$path;
  125. if(file_exists($path))@unlink($path);
  126. }
  127. private function upfileurl($lx)
  128. {
  129. $url = c('xinhudoc')->geturlstr('upfile', $lx, array(
  130. 'updir' => getconfig('xinhudoc_upmkdir')
  131. ));
  132. return $url;
  133. }
  134. /**
  135. * 从管理平台上删除文件
  136. */
  137. public function delAction()
  138. {
  139. $filenum = $this->getparams('filenum'); //文件ID
  140. if(isempt($filenum))return 'filenum is empty';
  141. $doc = c('xinhudoc');
  142. $url = $doc->geturlstr('upfile', 'del', array(
  143. 'filenum' => $filenum
  144. ));
  145. $result = c('curl')->getcurl($url);
  146. $barr = $doc->returnresult($result);
  147. if($barr['success']){
  148. return $barr['data'];
  149. }else{
  150. return $barr['msg'];
  151. }
  152. }
  153. }
粤ICP备19079148号