ResourceController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Http\Controllers\Admin\Base;
  3. use App\Http\Controllers\Controller;
  4. use App\Repositories\Enums\ResponseCodeEnum;
  5. use App\Repositories\Models\Base\Resource;
  6. use App\Services\Base\ResourceService;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Storage;
  9. use function EasyWeChat\Kernel\Support\str_random;
  10. /**
  11. * 资源管理
  12. */
  13. class ResourceController extends Controller
  14. {
  15. protected $allowed_img_ext = 'system_upload_img_type';
  16. protected $allowed_file_ext = 'system_upload_file_type';
  17. /**
  18. * @var ResourceService
  19. */
  20. private $resourceService;
  21. /**
  22. * ResourceController constructor.
  23. *
  24. * @param ResourceService $resourceService
  25. */
  26. public function __construct(ResourceService $resourceService)
  27. {
  28. parent::__construct();
  29. $this->middleware('checkUserPermission');
  30. $this->resourceService = $resourceService;
  31. }
  32. /**
  33. * 上传资源
  34. * @must
  35. * @param Request $request
  36. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
  37. * @throws \Illuminate\Validation\ValidationException
  38. */
  39. public function upload(Request $request)
  40. {
  41. $this->validate($request, [
  42. 'file' => 'required|file',
  43. 'dir' => 'required|alpha_dash',
  44. 'file_type' => 'required|in:img,file'
  45. ], [], [
  46. 'file' => '文件',
  47. 'dir' => '存放目录',
  48. 'file_type' => '文件类型',
  49. ]);
  50. $file = $request->file('file');
  51. if (!$file->isFile()) {
  52. return $this->response->fail('请上传一个文件', ResponseCodeEnum::CLIENT_VALIDATION_ERROR);
  53. }
  54. $file_type = $request->get('file_type', 'img');
  55. if ($file_type === 'img') {
  56. // 如果上传的不是图片将终止操作
  57. $config = str2arr(byCodeGetSetting($this->allowed_img_ext));
  58. if (!is_array($config)) {
  59. abort(ResponseCodeEnum::SYSTEM_CONFIG_ERROR, '系统图片配置错误');
  60. }
  61. if (!in_array(strtolower($file->getClientOriginalExtension()), $config)) {
  62. return $this->response->fail('图片类型不合法', ResponseCodeEnum::CLIENT_VALIDATION_ERROR);
  63. }
  64. } else {
  65. $config = str2arr(byCodeGetSetting($this->allowed_file_ext));
  66. if (!is_array($config)) {
  67. abort(ResponseCodeEnum::SYSTEM_CONFIG_ERROR, '系统文件配置错误');
  68. }
  69. if (!in_array(strtolower($file->getClientOriginalExtension()), $config)) {
  70. return $this->response->fail('文件类型不合法', ResponseCodeEnum::CLIENT_VALIDATION_ERROR);
  71. }
  72. }
  73. $model = $this->resourceService->handleUpload($request, config('filesystems.default'));
  74. if (!$model) {
  75. return $this->response->fail('上传文件失败', ResponseCodeEnum::SYSTEM_ERROR);
  76. }
  77. $type = 0;
  78. $img_type = config('site.img_type');
  79. $video_type = config('site.video_type');
  80. $arr = explode('.', $model->url);
  81. if (strpos($img_type, $arr[1])) $type = 1;
  82. if (strpos($video_type, $arr[1])) $type = 2;
  83. return $this->response->success([
  84. 'id' => $model->id,
  85. 'path' => $model->path,
  86. 'url' => $model->url,
  87. 'type' => $type,
  88. ]);
  89. }
  90. /**
  91. * 下载资源
  92. * @must
  93. * @param Request $request
  94. * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
  95. * @throws \Illuminate\Validation\ValidationException
  96. */
  97. public function download(Request $request)
  98. {
  99. $this->validate($request, ['id' => 'required|integer']);
  100. $resource = $this->resourceService->handleProfile($request->get('id'));
  101. return response()->download(Storage::disk($resource->disk)->url($resource->path));
  102. }
  103. /**
  104. * base64图片
  105. * @must
  106. * @param Request $request
  107. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
  108. * @throws \Illuminate\Validation\ValidationException
  109. */
  110. public function base64ToImg(Request $request)
  111. {
  112. $data = $this->validateData($request, [
  113. 'base64' => 'required|string',
  114. 'type' => 'required|string',
  115. ]);
  116. $resource = $this->resourceService->handleBase64ToImg($data, config('filesystems.default'));
  117. return $this->response->success($resource);
  118. }
  119. }