qcloudCosAction.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * 文件同步上传到腾讯云存储
  4. *
  5. */
  6. class qcloudCosClassAction extends runtAction
  7. {
  8. /**
  9. * 发送上传文件
  10. * php task.php qcloudCos,run -fileid=1
  11. * http://你地址/task.php?m=qcloudCos|runt&a=run&fileid=文件id
  12. */
  13. public function runAction()
  14. {
  15. if(!getconfig('qcloudCos_SecretKey'))return '未配置腾讯云存储';
  16. $fileid = (int)$this->getparams('fileid','0'); //文件ID
  17. if($fileid<=0)return 'error fileid';
  18. $frs = m('file')->getone($fileid);
  19. if(!$frs)return 'filers not found';
  20. $filepath = $frs['filepath'];
  21. if(substr($filepath, 0, 4)=='http')return 'filepath is httppath';
  22. $nfilepath = '';
  23. if(substr($filepath,-6)=='uptemp'){
  24. $aupath = ROOT_PATH.'/'.$filepath;
  25. $nfilepath = str_replace('.uptemp','.'.$frs['fileext'].'', $filepath);
  26. $content = file_get_contents($aupath);
  27. $this->rock->createtxt($nfilepath, base64_decode($content));
  28. unlink($aupath);
  29. $filepath = $nfilepath;
  30. }
  31. $msg = $this->sendpath($filepath, $frs, 'filepathout');
  32. if($nfilepath && file_exists($nfilepath))unlink($nfilepath);
  33. if($msg)return $msg;
  34. $thumbpath = $frs['thumbpath'];
  35. if(!isempt($thumbpath)){
  36. $msg = $this->sendpath($thumbpath, $frs, 'thumbplat');
  37. if($msg)return $msg;
  38. }
  39. return 'success';
  40. }
  41. private function sendpath($filepath, $frs, $fields)
  42. {
  43. $path = ROOT_PATH.'/'.$filepath;
  44. if(!file_exists($path))return 'filepath['.$fields.'] not exists';
  45. $res = c('qcloudCos')->upload($path,'', $filepath);
  46. if($res['code']==0){
  47. $data = $res['data'];
  48. $bo = m('file')->update("`$fields`='".$res['url']."'", $frs['id']);
  49. if($bo)@unlink($path);//删除文件
  50. if(PHP_SAPI != 'cli')print_r($res);
  51. }else{
  52. return $res['code'].'.'.$res['message'];
  53. }
  54. }
  55. /**
  56. * 下载文件,预览用到
  57. * php task.php qcloudCos,down -fileid=1
  58. */
  59. public function downAction()
  60. {
  61. $fileid = (int)$this->getparams('fileid','0'); //文件ID
  62. if($fileid<=0)return 'error fileid';
  63. $fobj = m('file');
  64. $frs = $fobj->getone($fileid);
  65. if(!$frs)return 'filers not found';
  66. $filepathout = $frs['filepathout'];
  67. if(isempt($filepathout))return 'filepathout is empty';
  68. //$filepathout = str_replace('//');
  69. $filepath = $frs['filepath'];
  70. $fileext = $frs['fileext'];
  71. $dstPath = ROOT_PATH.'/'.$filepath;
  72. if(file_exists($dstPath)){
  73. return ''.$dstPath.' exists';
  74. }
  75. $filepath = ''.UPDIR.'/logs/costmp/'.date('YmdHis').'a'.$fileid.'.'.$fileext.'';//用临时文件
  76. $dstPath = ROOT_PATH.'/'.$filepath;
  77. $this->rock->createdir($filepath);
  78. $fsarr = explode('myqcloud.com', $filepathout);
  79. $srcPath = substr($fsarr[1],1);
  80. $res = c('qcloudCos')->download($srcPath, $dstPath);
  81. if($res['code']==0 && file_exists($dstPath)){
  82. if(!c('upfile')->issavefile($fileext)){
  83. $filebase64 = base64_encode(file_get_contents($dstPath));
  84. $filepath = str_replace('.'.$fileext.'','.uptemp', $filepath);
  85. $bo = $this->rock->createtxt($filepath, $filebase64);
  86. @unlink($dstPath);
  87. }
  88. $fobj->update("`filepath`='$filepath'", $fileid);
  89. }else{
  90. $msg = ''.$frs['filename'].',无法下载('.$res['code'].'):'.$res['message'].'';
  91. m('log')->addlogs('腾讯云存储下载',$msg,2);
  92. }
  93. return $res['code'].'.'.$res['message'].'@'.$filepath.'@'.$srcPath;
  94. }
  95. }