qcloudCosAction.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * 文件同步上传到腾讯云存储/阿里云oss,两个都配置只上传到腾讯云存储。
  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') && !getconfig('alioss_keysecret'))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. if(getconfig('qcloudCos_autoup')){
  46. $res = c('qcloudCos')->upload($path,'', $filepath);
  47. }else{
  48. if(getconfig('alioss_autoup')){
  49. $res = c('alioss')->uploadFile($filepath);
  50. if(!$res['success'])$res['message'] = $res['msg'];
  51. }
  52. }
  53. if($res['code']==0){
  54. $data = $res['data'];
  55. $bo = m('file')->update("`$fields`='".$res['url']."'", $frs['id']);
  56. if($bo)@unlink($path);//删除文件
  57. if(PHP_SAPI != 'cli')print_r($res);
  58. }else{
  59. return $res['code'].'.'.$res['message'];
  60. }
  61. }
  62. /**
  63. * 下载文件,预览用到
  64. * php task.php qcloudCos,down -fileid=1
  65. */
  66. public function downAction()
  67. {
  68. $fileid = (int)$this->getparams('fileid','0'); //文件ID
  69. if($fileid<=0)return 'error fileid';
  70. $fobj = m('file');
  71. $frs = $fobj->getone($fileid);
  72. if(!$frs)return 'filers not found';
  73. $filepathout = $frs['filepathout'];
  74. if(isempt($filepathout))return 'filepathout is empty';
  75. //$filepathout = str_replace('//');
  76. $filepath = $frs['filepath'];
  77. $fileext = $frs['fileext'];
  78. $dstPath = ROOT_PATH.'/'.$filepath;
  79. if(file_exists($dstPath)){
  80. return ''.$dstPath.' exists';
  81. }
  82. $filepath = ''.UPDIR.'/logs/costmp/'.date('YmdHis').'a'.$fileid.'.'.$fileext.'';//用临时文件
  83. $dstPath = ROOT_PATH.'/'.$filepath;
  84. $this->rock->createdir($filepath);
  85. $res = returnerror('error');
  86. $res['message'] = '';
  87. //腾讯云的存储
  88. if(contain($filepathout,'myqcloud.com')){
  89. $fsarr = explode('myqcloud.com', $filepathout);
  90. $srcPath = substr($fsarr[1],1);
  91. $res = c('qcloudCos')->download($srcPath, $dstPath);
  92. }
  93. //腾讯云的存储
  94. if(contain($filepathout,'aliyuncs.com')){
  95. $fsarr = explode('aliyuncs.com', $filepathout);
  96. $srcPath = substr($fsarr[1],1);
  97. $res = c('alioss')->download($srcPath, $dstPath);
  98. if(!$res['success'])$res['message'] = $res['msg'];
  99. }
  100. if($res['code']==0 && file_exists($dstPath)){
  101. if(!c('upfile')->issavefile($fileext)){
  102. $filebase64 = base64_encode(file_get_contents($dstPath));
  103. $filepath = str_replace('.'.$fileext.'','.uptemp', $filepath);
  104. $bo = $this->rock->createtxt($filepath, $filebase64);
  105. @unlink($dstPath);
  106. }
  107. $fobj->update("`filepath`='$filepath'", $fileid);
  108. }else{
  109. $msg = ''.$frs['filename'].',无法下载('.$res['code'].'):'.$res['message'].'';
  110. m('log')->addlogs('存储下载',$msg,2);
  111. }
  112. return $res['code'].'.'.$res['message'].'@'.$filepath.'@'.$srcPath;
  113. }
  114. }
粤ICP备19079148号