UploadController.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace business\admin\controllers;
  3. use app\common\facades\Setting;
  4. use app\common\services\MiniFileLimitService;
  5. use app\common\services\upload\UploadService;
  6. use business\common\controllers\components\BaseController;
  7. class UploadController extends BaseController
  8. {
  9. public function uploadPic()
  10. {
  11. $attach = request()->attach;
  12. $ingress = request()->ingress;
  13. $file = request()->file('file');
  14. $local_upload = request()->local_upload == 1 ? true : false;
  15. $upload_type = request()->upload_type;
  16. if (!$file) {
  17. return $this->errorJson('请传入正确参数.');
  18. }
  19. if (!$file->isValid()) {
  20. return $this->errorJson('上传失败.');
  21. }
  22. if ($ingress && $upload_type == 'image' && $this->isMiniCheckImage()) {
  23. if ($file->getClientSize() > 1024 * 1024) {
  24. return $this->errorJson('小程序图片安全验证图片不能大于1M');
  25. }
  26. $check_result = (new MiniFileLimitService())->checkImg($file);
  27. if ($check_result['errcode'] == 87014) {
  28. return $this->errorJson('内容含有违法违规信息');
  29. }
  30. }
  31. $file_name = '';
  32. if (request()->need_name) {
  33. $file_name = $file->getClientOriginalName();
  34. }
  35. try {
  36. if ($local_upload) {
  37. $url_arr = (new UploadService())->upload($file, $upload_type, '', $file_name, false);
  38. } else {
  39. $url_arr = (new UploadService())->upload($file, $upload_type, '', $file_name);
  40. }
  41. }catch (\Exception $e){
  42. return $this->errorJson($e->getMessage());
  43. }
  44. return $this->successJson('上传成功', [
  45. 'img' => $url_arr['relative_path'],
  46. 'img_url' => $url_arr['absolute_path'],
  47. 'attach' => $attach,
  48. ]);
  49. }
  50. public function downloadFile()
  51. {
  52. @ini_set('memory_limit', -1);
  53. $url = request()->url;
  54. $temp = file_get_contents($url);
  55. if ($temp === false){
  56. http_response_code(404);
  57. die();
  58. }
  59. $name = basename($url);
  60. ob_clean();
  61. header('Content-Type:application/octet-stream');
  62. header('Content-Disposition:attachment; filename=' . $name);
  63. echo $temp;
  64. die();
  65. }
  66. protected function isMiniCheckImage()
  67. {
  68. if (!app('plugins')->isEnabled('min-app')) {
  69. return false;
  70. }
  71. $set = Setting::get('plugin.min_app');
  72. if ($set['switch'] != 1) {
  73. return false;
  74. }
  75. if ($set['image_check'] != 1) {
  76. return false;
  77. }
  78. return true;
  79. }
  80. }