UploadsController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Jobs\Attachment\VideoCosReviewSubmit;
  4. use App\Lib\Uploads\UploadHandler;
  5. use App\Models\WxAttachment;
  6. use App\Wen\Utils\FieldUtils;
  7. use App\Wen\Utils\FileUtils;
  8. use App\Wen\Utils\Settings;
  9. use App\Wen\Utils\UserUtils;
  10. use Illuminate\Http\Request;
  11. use QCloud\COSSTS\Sts;
  12. class UploadsController extends BaseController
  13. {
  14. public function cos_tmp_callback(Request $request){
  15. $uid = $request->uid;
  16. $key = _empty_default_($request->key, '');
  17. if(_empty_($key)){
  18. return $this->fail(200001);
  19. }
  20. if(strpos($key, $uid . '/') === false){
  21. return $this->fail(200004);
  22. }
  23. $res_data = _empty_default_($request->data, []);
  24. if(_empty_($res_data)){
  25. return $this->fail(200001);
  26. }
  27. if($res_data['statusCode'] != 200){
  28. return $this->fail(200004);
  29. }
  30. $ossType = Settings::get('ossType');
  31. $cosConfig = Settings::get('cosv5', []);
  32. $model = new WxAttachment();
  33. $model->user_id = $uid;
  34. $model->path = $key;
  35. $model->domain = $cosConfig['domain'];
  36. $model->store_type = $ossType;
  37. $types_str = implode('|', FieldUtils::getAttachmentTypes());
  38. preg_match('/\.('.$types_str.')$/', $key, $matchs);
  39. if($matchs && _array_key($matchs, 1, null)){
  40. $model->type = $matchs[1];
  41. }else{
  42. $model->type = '';
  43. }
  44. if(in_array($model->type, ['mp4', 'mp3'])){
  45. $model->cos_review = 4;
  46. }
  47. $model->state = 0;
  48. $r = $model->save();
  49. if($r){
  50. if($model->cos_review == 4){
  51. VideoCosReviewSubmit::dispatch($model->id);
  52. }
  53. return $this->success(['url'=>$cosConfig['domain'] . '/' . $key]);
  54. }else{
  55. return $this->fail(200002);
  56. }
  57. }
  58. public function cos_tmp_credentials(Request $request){
  59. $uid = $request->uid;
  60. $action = _empty_default_($request->action, 'put');
  61. $sts = new Sts();
  62. $cosConfig = Settings::get('cosv5', []);
  63. if(_empty_(_array_key($cosConfig, 'secretKey', ''))){
  64. return $this->fail(200008, [], 'cos配置错误');
  65. }
  66. $config = [];
  67. if($action == 'put'){
  68. $config = [
  69. 'url' => 'https://sts.tencentcloudapi.com/',
  70. 'domain' => 'sts.tencentcloudapi.com',
  71. 'secretId' => $cosConfig['secretId'], // 替换为您的 SecretId
  72. 'secretKey' => $cosConfig['secretKey'], // 替换为您的 SecretKey
  73. 'bucket' => $cosConfig['bucket'] . '-' . $cosConfig['appId'], // 替换为您的 bucket
  74. 'region' => $cosConfig['region'], // 替换为您的 bucket 所在地区
  75. 'durationSeconds' => 1800, // 密钥有效期,单位/秒,默认半小时
  76. 'allowPrefix' => [date('Y/m/d'). '/' . $uid . '/*'], // 临时密钥允许的前缀
  77. 'allowActions' => ['name/cos:PutObject', 'name/cos:PostObject'] // 临时密钥允许的操作列表
  78. ];
  79. }else if($action == 'post'){
  80. $config = [
  81. 'url' => 'https://sts.tencentcloudapi.com/',
  82. 'domain' => 'sts.tencentcloudapi.com',
  83. 'secretId' => $cosConfig['secretId'], // 替换为您的 SecretId
  84. 'secretKey' => $cosConfig['secretKey'], // 替换为您的 SecretKey
  85. 'bucket' => $cosConfig['bucket'] . '-' . $cosConfig['appId'], // 替换为您的 bucket
  86. 'region' => $cosConfig['region'], // 替换为您的 bucket 所在地区
  87. 'durationSeconds' => 1800, // 密钥有效期,单位/秒,默认半小时
  88. 'allowPrefix' => [date('Y/m/d'). '/' . $uid . '/*'], // 临时密钥允许的前缀
  89. 'allowActions' => ['name/cos:PostObject'] // 临时密钥允许的操作列表
  90. ];
  91. }
  92. if(_empty_($config)){
  93. return $this->fail(200004);
  94. }
  95. // 传入配置获取cos临时密钥
  96. $result = $sts->getTempKeys($config);
  97. return $this->success($result);
  98. }
  99. public function putFile(Request $request)
  100. {
  101. $uid = $request->uid;
  102. _limit_user('user:upload:file', $uid, 18);
  103. // if(!UserUtils::is_user_can_upload($uid)){
  104. // return $this->fail(200000);
  105. // }
  106. $file = $request->file('file');
  107. $scene = $request->header('scene',0);
  108. if(!in_array($file->extension(), FieldUtils::getAttachmentTypes())){
  109. if(!in_array($file->getClientOriginalExtension(), FieldUtils::getAttachmentTypes())){
  110. _logger_(__file__, __line__, $file->extension());
  111. _logger_(__file__, __line__, $file->getClientOriginalExtension());
  112. return $this->fail(200022);
  113. }
  114. }
  115. $path = UploadHandler::handle($file, $uid, 0, $scene);
  116. if($path){
  117. return $this->success($path['url']);
  118. }else{
  119. return $this->fail(200017);
  120. }
  121. }
  122. public function delFile(Request $request){
  123. $uid = $request->uid;
  124. $url = $request->url;
  125. if(_empty_($url)){
  126. return $this->fail(200001);
  127. }
  128. $attachment_part = FileUtils::get_attachment_part_from_url($url);
  129. if($attachment_part){
  130. $the_attachment = WxAttachment::where([
  131. ['domain', '=', $attachment_part['domain']],
  132. ['path', '=', $attachment_part['path']]
  133. ])->first();
  134. if($the_attachment){
  135. $author_id = $the_attachment->user_id;
  136. if($author_id == $uid || UserUtils::is_mini_admin($uid)){
  137. if(UploadHandler::del($the_attachment)){
  138. return $this->success();
  139. }else{
  140. _logger_(__file__, __line__, $the_attachment);
  141. return $this->fail(200006);
  142. }
  143. }else{
  144. return $this->fail(200000, [], '你没有上传权限');
  145. }
  146. }
  147. return $this->fail(200003);
  148. }
  149. return $this->fail(200004);
  150. }
  151. }