Upload.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * @filename Upload.php
  4. * @createdAt 2020/1/25
  5. * @project https://github.com/yanwenwu/catch-admin
  6. * @document http://doc.catchadmin.com
  7. * @author JaguarJack <njphper@gmail.com>
  8. * @copyright By CatchAdmin
  9. * @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt
  10. */
  11. namespace catchAdmin\system\controller;
  12. use catchAdmin\system\model\Attachments;
  13. use catcher\base\CatchController;
  14. use catcher\base\CatchRequest;
  15. use catcher\CatchResponse;
  16. use catcher\CatchUpload;
  17. use catcher\exceptions\FailedException;
  18. use think\facade\Db;
  19. use catcher\facade\FileSystem;
  20. use web2oss\WebUploadToOss;
  21. use catchAdmin\system\model\Config;
  22. class Upload extends CatchController
  23. {
  24. protected $attachment;
  25. public function __construct(Attachments $attachment)
  26. {
  27. $this->attachment = $attachment;
  28. }
  29. /**
  30. * image upload
  31. *
  32. * @time 2020年01月25日
  33. * @param CatchRequest $request
  34. * @param CatchUpload $upload
  35. * @return \think\response\Json
  36. */
  37. public function image(CatchRequest $request, CatchUpload $upload): \think\response\Json
  38. {
  39. $images = $request->file();
  40. if (!$images) {
  41. throw new FailedException('请选择图片上传');
  42. }
  43. return CatchResponse::success($upload->checkImages($images)->multiUpload($images['image']));
  44. }
  45. /**
  46. * file upload
  47. *
  48. * @time 2020年01月25日
  49. * @param CatchRequest $request
  50. * @param CatchUpload $upload
  51. * @return \think\response\Json
  52. */
  53. public function file(CatchRequest $request, CatchUpload $upload): \think\response\Json
  54. {
  55. $files = $request->file();
  56. return CatchResponse::success($upload->checkFiles($files)->multiUpload($files['file']));
  57. }
  58. /**
  59. * file upload 固件升级文件上传特殊处理
  60. *
  61. * @time 2020年01月25日
  62. * @param CatchRequest $request
  63. * @param RlCatchUpload $upload
  64. * @return \think\response\Json
  65. */
  66. public function file_v2(CatchRequest $request, CatchUpload $upload): \think\response\Json
  67. {
  68. $files = $request->file();
  69. // $content=file_get_contents($files['file']->getPathname());
  70. $fh = fopen($files['file']->getPathname(), "rb");
  71. $content = bin2hex(fread($fh, 16));
  72. fclose($fh);
  73. if(!$content){
  74. return CatchResponse::fail('文件读取失败');
  75. }
  76. $str=str_replace(' ','',$content);
  77. $ver=substr($str , 8 , 4);
  78. $uid=substr($str , 16 , 8);
  79. $rom_ver=substr($str , 28 , 4);
  80. if(!$ver || !$uid || !$rom_ver){
  81. return CatchResponse::fail('文件内容格式不正确');
  82. }
  83. $firm_ver=$rom_ver.$ver;
  84. $basename=$files['file']->getOriginalName();
  85. // $path='firmware' . DIRECTORY_SEPARATOR .$basename;
  86. // if($upload->checkFilesExists($path)){
  87. // return CatchResponse::fail('文件已存在');
  88. // }
  89. if(Db::table("firmware_upgrade")->where('file_name', $basename)->where('deleted_at', 0)->count()){
  90. return CatchResponse::fail('文件已存在');
  91. }
  92. $path_parts = pathinfo($basename);
  93. $filename=$path_parts['filename'];
  94. $res=$upload->checkFiles($files)->multiUpload($files['file'],function()use ($filename){
  95. return 'firmware'. DIRECTORY_SEPARATOR .$filename;
  96. });
  97. if(!$res){
  98. return CatchResponse::fail('文件上传失败');
  99. }
  100. $data=array('file_url'=>$res,'ver'=>$firm_ver,'uid'=>$uid);
  101. return CatchResponse::success($data);
  102. }
  103. /**
  104. * file upload 固件升级文件上传特殊处理
  105. *
  106. * @time 2020年01月25日
  107. * @param CatchRequest $request
  108. * @param RlCatchUpload $upload
  109. * @return \think\response\Json
  110. */
  111. public function file_excel(CatchRequest $request, CatchUpload $upload): \think\response\Json
  112. {
  113. $files = $request->file();
  114. $basename=$files['file']->getOriginalName();
  115. $path_parts = pathinfo($basename);
  116. $filename=$path_parts['filename'];
  117. $res=$upload->checkFiles($files)->multiUpload($files['file'],function()use ($filename){
  118. return 'student_excel'. DIRECTORY_SEPARATOR .$filename;
  119. });
  120. if(!$res){
  121. return CatchResponse::fail('文件上传失败');
  122. }
  123. $data=array('excel_url'=>$res,'msg'=>'上传成功');
  124. return CatchResponse::success($data);
  125. }
  126. /**
  127. * file upload
  128. *
  129. * @time 2020年01月25日
  130. * @param CatchRequest $request
  131. * @param CatchUpload $upload
  132. * @return \think\response\Json
  133. */
  134. public function unlinkFile(CatchRequest $request): \think\response\Json
  135. {
  136. $params = $request->post();
  137. $file_name=basename($params['file_url']);
  138. //if ($attachment->driver == 'local') {
  139. $localPath = config('filesystem.disks.local.root') . DIRECTORY_SEPARATOR;
  140. $path = $localPath . 'firmware/'.$file_name;
  141. if (FileSystem::exists($path)) {
  142. Filesystem::delete($path);
  143. }
  144. // } else {
  145. // Filesystem::delete($attachment->path);
  146. // }
  147. return CatchResponse::success();
  148. }
  149. /**
  150. * 获取阿里云OSS策略
  151. */
  152. public function getAliOssPolicy()
  153. {
  154. $get = request()->get();
  155. $max_size = isset($get['maxSize']) ? intval($get['maxSize']) : 10;
  156. // 从配置表中获取配置信息
  157. try {
  158. $configs = (new Config())->getConfig('upload');
  159. } catch(\Exception $e) {
  160. return CatchResponse::fail('获取配置数据失败');
  161. }
  162. if (!isset($configs['oss'])) {
  163. return CatchResponse::fail('获取OSS配置数据失败');
  164. }
  165. $oss = $configs['oss'];
  166. $bucket = isset($oss['bucket']) ? $oss['bucket'] : '';
  167. $end_point = isset($oss['end_point']) ? $oss['end_point'] : '';
  168. $oss_host = 'https://'. $bucket .'.'.$end_point; // 格式为 bucketname.endpoint
  169. $config = array(
  170. 'id' => isset($oss['access_key']) ? $oss['access_key'] : '',
  171. 'key' => isset($oss['secret_key']) ? $oss['secret_key'] : '',
  172. 'host' => $oss_host,
  173. 'callbackUrl' => env('APP_DOMAIN') . '/upload/osscallback', // $_SERVER['HTTP_HOST'].'/osscallback',// 上传完成回调
  174. 'fileSize' => $max_size * 1024 * 1024, // bit
  175. 'dir' => 'wxt/'.date('Y-m-d').'/',
  176. 'expire' => 60, // 设置该policy超时时间(单位:秒). 即这个policy过了这个有效时间,将不能访问。
  177. );
  178. $response = WebUploadToOss::getPolicy($config);
  179. if (!$response['success']) {
  180. return CatchResponse::fail($response['message']);
  181. }
  182. return CatchResponse::success($response['data']);
  183. }
  184. /**
  185. * 阿里云OSS回调
  186. */
  187. public function aliOssCallback()
  188. {
  189. $res = WebUploadToOss::callback();
  190. if (!$res['success']) {
  191. return CatchResponse::fail($res['message']);
  192. }
  193. return CatchResponse::success($res['data']);
  194. }
  195. }