ResourceController.php 4.3 KB

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