AttachmentModel.class.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace Api\Model;
  3. use Api\Model\BaseModel;
  4. /**
  5. *
  6. * @author star7th
  7. */
  8. class AttachmentModel extends BaseModel {
  9. Protected $autoCheckFields = false; //一定要关闭字段缓存,不然会报找不到表的错误
  10. //获取某个用户的当前已使用附件流量
  11. public function getUserFlow($uid){
  12. $month = Date("Y-m") ;
  13. $file_flow = D("FileFlow")->where(" uid = '%s' and date_month = '$month' " , array($uid))->find() ;
  14. if($file_flow){
  15. return intval($file_flow['used']) ;
  16. }else{
  17. D("FileFlow")->add(array(
  18. "uid" => $uid ,
  19. "used" => 0 ,
  20. "date_month" => $month ,
  21. ));
  22. return 0 ;
  23. }
  24. }
  25. //记录某个用户流量
  26. public function recordUserFlow($uid , $file_size){
  27. $month = Date("Y-m") ;
  28. $used = $this->getUserFlow($uid) ;
  29. return D("FileFlow")->where(" uid = '%s' and date_month = '$month' " , array($uid))->save(array(
  30. "used" => $used + intval($file_size)
  31. ));
  32. }
  33. public function deleteFile($file_id){
  34. $file = D("UploadFile")->where("file_id = '$file_id' ")->find();
  35. $real_url = $file['real_url'] ;
  36. $array = explode("/Public/Uploads/", $real_url) ;
  37. $file_path = "../Public/Uploads/".$array[1] ;
  38. if (file_exists($file_path)) {
  39. @unlink($file_path);
  40. }
  41. D("UploadFile")->where(" file_id = '$file_id' ")->delete();
  42. D("FilePage")->where(" file_id = '$file_id' ")->delete();
  43. return true ;
  44. }
  45. //上传文件,返回url
  46. public function upload($_files , $file_key , $uid , $item_id = 0 , $page_id = 0 ){
  47. $uploadFile = $_files[$file_key] ;
  48. if (strstr(strip_tags(strtolower($uploadFile['name'])), ".php") ) {
  49. return false;
  50. }
  51. $oss_open = D("Options")->get("oss_open" ) ;
  52. if ($oss_open) {
  53. $url = $this->uploadOss($uploadFile);
  54. if ($url) {
  55. $sign = md5($url.time().rand()) ;
  56. $insert = array(
  57. "sign" => $sign,
  58. "uid" => $uid,
  59. "item_id" => $item_id,
  60. "page_id" => $page_id,
  61. "display_name" => $uploadFile['name'],
  62. "file_type" => $uploadFile['type'],
  63. "file_size" => $uploadFile['size'],
  64. "real_url" => $url,
  65. "addtime" => time(),
  66. );
  67. $file_id = D("UploadFile")->add($insert);
  68. $insert = array(
  69. "file_id" => $file_id,
  70. "item_id" => $item_id,
  71. "page_id" => $page_id,
  72. "addtime" => time(),
  73. );
  74. $ret = D("FilePage")->add($insert);
  75. $url = get_domain().U("api/attachment/visitFile",array("sign" => $sign));
  76. return $url ;
  77. }
  78. }else{
  79. $upload = new \Think\Upload();// 实例化上传类
  80. $upload->maxSize = 1003145728 ;// 设置附件上传大小
  81. $upload->rootPath = './../Public/Uploads/';// 设置附件上传目录
  82. $upload->savePath = '';// 设置附件上传子目录
  83. $info = $upload->uploadOne($uploadFile) ;
  84. if(!$info) {// 上传错误提示错误信息
  85. var_dump($upload->getError());
  86. return;
  87. }else{// 上传成功 获取上传文件信息
  88. $url = get_domain().__ROOT__.substr($upload->rootPath,1).$info['savepath'].$info['savename'] ;
  89. $sign = md5($url.time().rand()) ;
  90. $insert = array(
  91. "sign" => $sign,
  92. "uid" => $uid,
  93. "item_id" => $item_id,
  94. "page_id" => $page_id,
  95. "display_name" => $uploadFile['name'],
  96. "file_type" => $uploadFile['type'],
  97. "file_size" => $uploadFile['size'],
  98. "real_url" => $url,
  99. "addtime" => time(),
  100. );
  101. $file_id = D("UploadFile")->add($insert);
  102. $insert = array(
  103. "file_id" => $file_id,
  104. "item_id" => $item_id,
  105. "page_id" => $page_id,
  106. "addtime" => time(),
  107. );
  108. $ret = D("FilePage")->add($insert);
  109. $url = get_domain().U("api/attachment/visitFile",array("sign" => $sign));
  110. return $url ;
  111. }
  112. }
  113. return false;
  114. }
  115. //上传到oss。参数$uploadFile是文件上传流,如$_FILES['file'] .也可以自己拼凑
  116. public function uploadOss($uploadFile){
  117. $oss_setting_json = D("Options")->get("oss_setting") ;
  118. $oss_setting = json_decode($oss_setting_json,1);
  119. if ($oss_setting && $oss_setting['oss_type'] && $oss_setting['oss_type'] == 'aliyun') {
  120. $config = array(
  121. "key" => $oss_setting['key'],
  122. "secret"=> $oss_setting['secret'],
  123. "endpoint"=> $oss_setting['endpoint'],
  124. "bucket"=> $oss_setting['bucket'],
  125. );
  126. // $oss = new_oss($config['key'] , $config['secret'] , $config['endpoint'] );
  127. include_once VENDOR_PATH .'Alioss/autoload.php';
  128. $oss = new \OSS\OssClient($config['key'] , $config['secret'] , $config['endpoint'] , false );
  129. $ext = strrchr($uploadFile['name'], '.'); //获取扩展名
  130. $oss_path = "showdoc_".time().rand().$ext;
  131. $res = $oss->uploadFile($config['bucket'],$oss_path,$uploadFile['tmp_name']);
  132. if ($res && $res['info'] && $res['info']['url']) {
  133. if ($oss_setting['domain']) {
  134. return $oss_setting['protocol'] . '://'.$oss_setting['domain']."/".$oss_path ;
  135. }else{
  136. return $res['info']['url'] ;
  137. }
  138. }
  139. }
  140. if ($oss_setting && $oss_setting['oss_type'] && $oss_setting['oss_type'] == 'qiniu') {
  141. $config = array(
  142. 'rootPath' => './',
  143. 'saveName' => array('uniqid', ''),
  144. 'driver' => 'Qiniu',
  145. 'driverConfig' => array(
  146. 'accessKey' => $oss_setting['key'],
  147. 'secrectKey' => $oss_setting['secret'],
  148. 'protocol'=>$oss_setting['protocol'],
  149. 'domain' => $oss_setting['domain'],
  150. 'bucket' => $oss_setting['bucket'],
  151. )
  152. );
  153. //上传到七牛
  154. $Upload = new \Think\Upload($config);
  155. $info = $Upload->uploadOne($uploadFile);
  156. if ($info && $info['url']) {
  157. return $info['url'] ;
  158. }
  159. }
  160. //var_dump($config);
  161. // 腾讯云
  162. if ($oss_setting && $oss_setting['oss_type'] && $oss_setting['oss_type'] == 'qcloud') {
  163. $cosClient = new \Qcloud\Cos\Client(array('region' => $oss_setting['region'],
  164. 'credentials'=> array(
  165. 'secretId' => $oss_setting['secretId'],
  166. 'secretKey' => $oss_setting['secretKey']
  167. )));
  168. $ext = strrchr($uploadFile['name'], '.'); //获取扩展名
  169. $oss_path = "showdoc_".time().rand().rand().$ext;
  170. $result = $cosClient->putObject(array(
  171. 'Bucket' => $oss_setting['bucket'],
  172. 'Key' => $oss_path ,
  173. 'Body' => fopen($uploadFile['tmp_name'], 'rb')));
  174. if ($result && $result['ObjectURL']) {
  175. if ($oss_setting['domain']) {
  176. return $oss_setting['protocol'] . '://'.$oss_setting['domain']."/".$oss_path ;
  177. }else{
  178. return $result['ObjectURL'] ;
  179. }
  180. }
  181. }
  182. return false ;
  183. }
  184. }