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_id = intval($file_id) ;
  35. $file = D("UploadFile")->where("file_id = '$file_id' ")->find();
  36. $real_url = $file['real_url'] ;
  37. $array = explode("/Public/Uploads/", $real_url) ;
  38. $file_path = "../Public/Uploads/".$array[1] ;
  39. if (file_exists($file_path)) {
  40. @unlink($file_path);
  41. }
  42. D("UploadFile")->where(" file_id = '$file_id' ")->delete();
  43. D("FilePage")->where(" file_id = '$file_id' ")->delete();
  44. return true ;
  45. }
  46. //上传文件,返回url
  47. public function upload($_files , $file_key , $uid , $item_id = 0 , $page_id = 0 ){
  48. $uploadFile = $_files[$file_key] ;
  49. if (strstr(strip_tags(strtolower($uploadFile['name'])), ".php") ) {
  50. return false;
  51. }
  52. $oss_open = D("Options")->get("oss_open" ) ;
  53. if ($oss_open) {
  54. $url = $this->uploadOss($uploadFile);
  55. if ($url) {
  56. $sign = md5($url.time().rand()) ;
  57. $insert = array(
  58. "sign" => $sign,
  59. "uid" => $uid,
  60. "item_id" => $item_id,
  61. "page_id" => $page_id,
  62. "display_name" => $uploadFile['name'],
  63. "file_type" => $uploadFile['type'],
  64. "file_size" => $uploadFile['size'],
  65. "real_url" => $url,
  66. "addtime" => time(),
  67. );
  68. $file_id = D("UploadFile")->add($insert);
  69. $insert = array(
  70. "file_id" => $file_id,
  71. "item_id" => $item_id,
  72. "page_id" => $page_id,
  73. "addtime" => time(),
  74. );
  75. $ret = D("FilePage")->add($insert);
  76. $url = get_domain().U("api/attachment/visitFile",array("sign" => $sign));
  77. return $url ;
  78. }
  79. }else{
  80. $upload = new \Think\Upload();// 实例化上传类
  81. $upload->maxSize = 1003145728 ;// 设置附件上传大小
  82. $upload->rootPath = './../Public/Uploads/';// 设置附件上传目录
  83. $upload->savePath = '';// 设置附件上传子目录
  84. $info = $upload->uploadOne($uploadFile) ;
  85. if(!$info) {// 上传错误提示错误信息
  86. var_dump($upload->getError());
  87. return;
  88. }else{// 上传成功 获取上传文件信息
  89. $url = get_domain().__ROOT__.substr($upload->rootPath,1).$info['savepath'].$info['savename'] ;
  90. $sign = md5($url.time().rand()) ;
  91. $insert = array(
  92. "sign" => $sign,
  93. "uid" => $uid,
  94. "item_id" => $item_id,
  95. "page_id" => $page_id,
  96. "display_name" => $uploadFile['name'],
  97. "file_type" => $uploadFile['type'],
  98. "file_size" => $uploadFile['size'],
  99. "real_url" => $url,
  100. "addtime" => time(),
  101. );
  102. $file_id = D("UploadFile")->add($insert);
  103. $insert = array(
  104. "file_id" => $file_id,
  105. "item_id" => $item_id,
  106. "page_id" => $page_id,
  107. "addtime" => time(),
  108. );
  109. $ret = D("FilePage")->add($insert);
  110. $url = get_domain().U("api/attachment/visitFile",array("sign" => $sign));
  111. return $url ;
  112. }
  113. }
  114. return false;
  115. }
  116. //上传到oss。参数$uploadFile是文件上传流,如$_FILES['file'] .也可以自己拼凑
  117. public function uploadOss($uploadFile){
  118. $oss_setting_json = D("Options")->get("oss_setting") ;
  119. $oss_setting = json_decode($oss_setting_json,1);
  120. if ($oss_setting && $oss_setting['oss_type'] && $oss_setting['oss_type'] == 'aliyun') {
  121. $config = array(
  122. "key" => $oss_setting['key'],
  123. "secret"=> $oss_setting['secret'],
  124. "endpoint"=> $oss_setting['endpoint'],
  125. "bucket"=> $oss_setting['bucket'],
  126. );
  127. // $oss = new_oss($config['key'] , $config['secret'] , $config['endpoint'] );
  128. include_once VENDOR_PATH .'Alioss/autoload.php';
  129. $oss = new \OSS\OssClient($config['key'] , $config['secret'] , $config['endpoint'] , false );
  130. $ext = strrchr($uploadFile['name'], '.'); //获取扩展名
  131. $oss_path = "showdoc_".time().rand().$ext;
  132. $res = $oss->uploadFile($config['bucket'],$oss_path,$uploadFile['tmp_name']);
  133. if ($res && $res['info'] && $res['info']['url']) {
  134. if ($oss_setting['domain']) {
  135. return $oss_setting['protocol'] . '://'.$oss_setting['domain']."/".$oss_path ;
  136. }else{
  137. return $res['info']['url'] ;
  138. }
  139. }
  140. }
  141. if ($oss_setting && $oss_setting['oss_type'] && $oss_setting['oss_type'] == 'qiniu') {
  142. $config = array(
  143. 'rootPath' => './',
  144. 'saveName' => array('uniqid', ''),
  145. 'driver' => 'Qiniu',
  146. 'driverConfig' => array(
  147. 'accessKey' => $oss_setting['key'],
  148. 'secrectKey' => $oss_setting['secret'],
  149. 'protocol'=>$oss_setting['protocol'],
  150. 'domain' => $oss_setting['domain'],
  151. 'bucket' => $oss_setting['bucket'],
  152. )
  153. );
  154. //上传到七牛
  155. $Upload = new \Think\Upload($config);
  156. $info = $Upload->uploadOne($uploadFile);
  157. if ($info && $info['url']) {
  158. return $info['url'] ;
  159. }
  160. }
  161. //var_dump($config);
  162. // 腾讯云
  163. if ($oss_setting && $oss_setting['oss_type'] && $oss_setting['oss_type'] == 'qcloud') {
  164. $cosClient = new \Qcloud\Cos\Client(array('region' => $oss_setting['region'],
  165. 'credentials'=> array(
  166. 'secretId' => $oss_setting['secretId'],
  167. 'secretKey' => $oss_setting['secretKey']
  168. )));
  169. $ext = strrchr($uploadFile['name'], '.'); //获取扩展名
  170. $oss_path = "showdoc_".time().rand().rand().$ext;
  171. $result = $cosClient->putObject(array(
  172. 'Bucket' => $oss_setting['bucket'],
  173. 'Key' => $oss_path ,
  174. 'Body' => fopen($uploadFile['tmp_name'], 'rb')));
  175. if ($result && $result['ObjectURL']) {
  176. if ($oss_setting['domain']) {
  177. return $oss_setting['protocol'] . '://'.$oss_setting['domain']."/".$oss_path ;
  178. }else{
  179. return $result['ObjectURL'] ;
  180. }
  181. }
  182. }
  183. return false ;
  184. }
  185. }