ResourceController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Http\Controllers\Api\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->resourceService = $resourceService;
  28. }
  29. /**
  30. * 上传资源
  31. * @must
  32. * @param Request $request
  33. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
  34. * @throws \Illuminate\Validation\ValidationException
  35. */
  36. public function upload(Request $request)
  37. {
  38. $this->validate($request, [
  39. 'file' => 'required|file',
  40. 'dir' => 'required|alpha_dash',
  41. 'file_type' => 'required|in:img,file'
  42. ], [], [
  43. 'file' => '文件',
  44. 'dir' => '存放目录',
  45. 'file_type' => '文件类型',
  46. ]);
  47. $file = $request->file('file');
  48. if (!$file->isFile()) {
  49. return $this->response->fail('请上传一个文件', ResponseCodeEnum::CLIENT_VALIDATION_ERROR);
  50. }
  51. $file_type = $request->get('file_type', 'img');
  52. if ($file_type === 'img') {
  53. // 如果上传的不是图片将终止操作
  54. $config = str2arr(byCodeGetSetting($this->allowed_img_ext));
  55. if (!is_array($config)) {
  56. abort(ResponseCodeEnum::SYSTEM_CONFIG_ERROR, '系统图片配置错误');
  57. }
  58. if (!in_array(strtolower($file->getClientOriginalExtension()), $config)) {
  59. return $this->response->fail('图片类型不合法', ResponseCodeEnum::CLIENT_VALIDATION_ERROR);
  60. }
  61. } else {
  62. $config = str2arr(byCodeGetSetting($this->allowed_file_ext));
  63. if (!is_array($config)) {
  64. abort(ResponseCodeEnum::SYSTEM_CONFIG_ERROR, '系统文件配置错误');
  65. }
  66. if (!in_array(strtolower($file->getClientOriginalExtension()), $config)) {
  67. return $this->response->fail('文件类型不合法', ResponseCodeEnum::CLIENT_VALIDATION_ERROR);
  68. }
  69. }
  70. $model = $this->resourceService->handleUpload($request);
  71. if (!$model) {
  72. return $this->response->fail('上传文件失败', ResponseCodeEnum::SYSTEM_ERROR);
  73. }
  74. //判断图片还是视频,供前端判断
  75. $type = 0;
  76. $img_type = config('site.img_type');
  77. $video_type = config('site.video_type');
  78. $arr = explode('.', $model->path);
  79. if (strpos($img_type, $arr[1]) !== false) $type = 1;
  80. if (strpos($video_type, $arr[1]) !== false) $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. }