AssetController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: kane <chengjin005@163.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\user\controller;
  12. use cmf\controller\AdminBaseController;
  13. use cmf\lib\Upload;
  14. use think\facade\View;
  15. use Aws\S3\S3Client;
  16. /**
  17. * 附件上传控制器
  18. * Class Asset
  19. * @package app\asset\controller
  20. */
  21. class AssetController extends AdminBaseController
  22. {
  23. public function initialize()
  24. {
  25. $adminId = cmf_get_current_admin_id();
  26. $userId = cmf_get_current_user_id();
  27. if (empty($adminId) && empty($userId)) {
  28. $this->error("非法上传!");
  29. }
  30. connectionRedis();
  31. }
  32. /**
  33. * webuploader 上传
  34. */
  35. public function webuploaderBF()
  36. {
  37. if ($this->request->isPost()) {
  38. $uploader = new Upload();
  39. $result = $uploader->upload();
  40. if ($result === false) {
  41. $this->error($uploader->getError());
  42. } else {
  43. $this->success("上传成功!", '', $result);
  44. }
  45. } else {
  46. $uploadSetting = cmf_get_upload_setting();
  47. $arrFileTypes = [
  48. 'image' => ['title' => 'Image files', 'extensions' => $uploadSetting['file_types']['image']['extensions']],
  49. 'video' => ['title' => 'Video files', 'extensions' => $uploadSetting['file_types']['video']['extensions']],
  50. 'audio' => ['title' => 'Audio files', 'extensions' => $uploadSetting['file_types']['audio']['extensions']],
  51. 'file' => ['title' => 'Custom files', 'extensions' => $uploadSetting['file_types']['file']['extensions']]
  52. ];
  53. $arrData = $this->request->param();
  54. if (empty($arrData["filetype"])) {
  55. $arrData["filetype"] = "image";
  56. }
  57. $fileType = $arrData["filetype"];
  58. if (array_key_exists($arrData["filetype"], $arrFileTypes)) {
  59. $extensions = $uploadSetting['file_types'][$arrData["filetype"]]['extensions'];
  60. $fileTypeUploadMaxFileSize = $uploadSetting['file_types'][$fileType]['upload_max_filesize'];
  61. } else {
  62. $this->error('上传文件类型配置错误!');
  63. }
  64. View::share('filetype', $arrData["filetype"]);
  65. View::share('extensions', $extensions);
  66. View::share('upload_max_filesize', $fileTypeUploadMaxFileSize * 1024);
  67. View::share('upload_max_filesize_mb', intval($fileTypeUploadMaxFileSize / 1024));
  68. $maxFiles = intval($uploadSetting['max_files']);
  69. $maxFiles = empty($maxFiles) ? 20 : $maxFiles;
  70. $chunkSize = intval($uploadSetting['chunk_size']);
  71. $chunkSize = empty($chunkSize) ? 512 : $chunkSize;
  72. View::share('max_files', $arrData["multi"] ? $maxFiles : 1);
  73. View::share('chunk_size', $chunkSize); //// 单位KB
  74. View::share('multi', $arrData["multi"]);
  75. View::share('app', $arrData["app"]);
  76. $content = hook_one('fetch_upload_view');
  77. $tabs = ['local', 'url', 'cloud'];
  78. $tab = !empty($arrData['tab']) && in_array($arrData['tab'], $tabs) ? $arrData['tab'] : 'local';
  79. if (!empty($content)) {
  80. $this->assign('has_cloud_storage', true);
  81. }
  82. if (!empty($content) && $tab == 'cloud') {
  83. return $content;
  84. }
  85. $tab = $tab == 'cloud' ? 'local' : $tab;
  86. $this->assign('tab', $tab);
  87. return $this->fetch(":webuploader");
  88. }
  89. }
  90. /**
  91. * webuploader 上传
  92. */
  93. public function webuploader(){
  94. if ($this->request->isPost()) {
  95. /*$allow_wj="jpg,JPG,png,PNG,gif,GIF,Gif,jpeg,JPEG";
  96. $allow=explode(",",$allow_wj);
  97. if (!get_file_suffix($_FILES['file']['name'],$allow)){
  98. $this->error("请上传正确格式的图片");
  99. }*/
  100. $files["file"]=$_FILES["file"];
  101. $type='img';
  102. $rs=adminUploadFiles($files,$type);
  103. if($rs['code']!=0){
  104. $this->error($rs['msg']);
  105. }
  106. $this->success("上传成功!", '', $rs);
  107. } else {
  108. $uploadSetting = cmf_get_upload_setting();
  109. $arrFileTypes = [
  110. 'image' => ['title' => 'Image files', 'extensions' => $uploadSetting['file_types']['image']['extensions']],
  111. 'video' => ['title' => 'Video files', 'extensions' => $uploadSetting['file_types']['video']['extensions']],
  112. 'audio' => ['title' => 'Audio files', 'extensions' => $uploadSetting['file_types']['audio']['extensions']],
  113. 'file' => ['title' => 'Custom files', 'extensions' => $uploadSetting['file_types']['file']['extensions']]
  114. ];
  115. $arrData = $this->request->param();
  116. if (empty($arrData["filetype"])) {
  117. $arrData["filetype"] = "image";
  118. }
  119. $fileType = $arrData["filetype"];
  120. if (array_key_exists($arrData["filetype"], $arrFileTypes)) {
  121. $extensions = $uploadSetting['file_types'][$arrData["filetype"]]['extensions'];
  122. $fileTypeUploadMaxFileSize = $uploadSetting['file_types'][$fileType]['upload_max_filesize'];
  123. } else {
  124. $this->error('上传文件类型配置错误!');
  125. }
  126. View::share('filetype', $arrData["filetype"]);
  127. View::share('extensions', $extensions);
  128. View::share('upload_max_filesize', $fileTypeUploadMaxFileSize * 1024);
  129. View::share('upload_max_filesize_mb', intval($fileTypeUploadMaxFileSize / 1024));
  130. $maxFiles = intval($uploadSetting['max_files']);
  131. $maxFiles = empty($maxFiles) ? 20 : $maxFiles;
  132. $chunkSize = intval($uploadSetting['chunk_size']);
  133. $chunkSize = empty($chunkSize) ? 512 : $chunkSize;
  134. View::share('max_files', $arrData["multi"] ? $maxFiles : 1);
  135. View::share('chunk_size', $chunkSize); //// 单位KB
  136. View::share('multi', $arrData["multi"]);
  137. View::share('app', $arrData["app"]);
  138. $content = hook_one('fetch_upload_view');
  139. $tabs = ['local', 'url', 'cloud'];
  140. $tab = !empty($arrData['tab']) && in_array($arrData['tab'], $tabs) ? $arrData['tab'] : 'local';
  141. if (!empty($content)) {
  142. $this->assign('has_cloud_storage', true);
  143. }
  144. if (!empty($content) && $tab == 'cloud') {
  145. return $content;
  146. }
  147. $tab = $tab == 'cloud' ? 'local' : $tab;
  148. $this->assign('tab', $tab);
  149. return $this->fetch(":webuploader");
  150. }
  151. }
  152. }