Upload.php 6.8 KB

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