ResourceService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Services;
  11. use App\Contracts\Repositories\ResourceRepository;
  12. use App\Repositories\Eloquent\ResourceRepositoryEloquent;
  13. use App\Repositories\Enums\ResponseCodeEnum;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Support\Facades\Storage;
  16. use Illuminate\Support\Str;
  17. use Illuminate\Validation\ValidationException;
  18. use Jiannei\Enum\Laravel\Repositories\Enums\HttpStatusCodeEnum;
  19. use Jiannei\Response\Laravel\Support\Facades\Response;
  20. class ResourceService
  21. {
  22. public $allowed_img_ext = ["png", "jpg", "gif", 'jpeg'];
  23. public $allowed_file_ext = ["doc", 'docx', "xls", "pdf", 'xlsx', 'mp4', 'webvtt', 'vtt'];
  24. private $repository;
  25. /**
  26. * UserService constructor.
  27. *
  28. * @param ResourceRepositoryEloquent $repository
  29. */
  30. public function __construct(ResourceRepository $repository)
  31. {
  32. $this->repository = $repository;
  33. }
  34. public function handleUpload(Request $request)
  35. {
  36. $disk = 'public';
  37. // 获取上传的文件
  38. $file = $request->file('file');
  39. if (!$file->isFile()) return false;
  40. $type = $request->get('file_type', 'img');
  41. if ($type === 'img') {
  42. // 如果上传的不是图片将终止操作
  43. if (!in_array($file->getClientOriginalExtension(), $this->allowed_img_ext)) {
  44. return false;
  45. }
  46. } else {
  47. if (!in_array($file->getClientOriginalExtension(), $this->allowed_file_ext)) {
  48. return false;
  49. }
  50. }
  51. $type = $request->get('type', 'default');
  52. $dir = 'resource';
  53. switch ($type) {
  54. case 'course':
  55. $dir = 'course';
  56. break;
  57. case 'course_video':
  58. $dir = 'course_video';
  59. break;
  60. case 'course_video_subtitle_zh':
  61. $dir = 'course_video_subtitle_zh';
  62. break;
  63. case 'course_video_subtitle_en':
  64. $dir = 'course_video_subtitle_en';
  65. break;
  66. case 'attach':
  67. $dir = 'course_attach';
  68. break;
  69. case 'medical_record':
  70. $dir = 'medical_record';
  71. break;
  72. case 'user_headimg':
  73. $dir = 'user_headimg';
  74. break;
  75. case 'import_students':
  76. $dir = 'import_students';
  77. break;
  78. case 'import_drugs':
  79. $dir = 'import_drugs';
  80. break;
  81. case 'import_prescriptons':
  82. $dir = 'import_prescriptons';
  83. break;
  84. case 'banners':
  85. $dir = 'banners';
  86. break;
  87. }
  88. $newName = Str::random(32) . '.' . $file->getClientOriginalExtension();
  89. $path = Storage::disk($disk)->putFileAs($dir, $file, $newName);
  90. $url = Storage::disk($disk)->url($path);
  91. $resource = $this->repository->insertResource($path, $newName, $file->getClientOriginalName(), $url, $file->getSize());
  92. return $resource;
  93. }
  94. /**
  95. * 上传文件
  96. * @param Request $request
  97. * @param string $dir
  98. * @param array $allow_ext
  99. * @return
  100. * Author: Mead
  101. */
  102. public function upload(Request $request, $dir = '', $allow_ext = [])
  103. {
  104. $disk = 'public';
  105. // 获取上传的文件
  106. $file = $request->file('file');
  107. if (!$file->isFile()) return Response::fail('请上传文件类型', ResponseCodeEnum::CLIENT_VALIDATION_ERROR);;
  108. if (!in_array($file->getClientOriginalExtension(), $allow_ext)) {
  109. return Response::fail('上传文件的类型不对', ResponseCodeEnum::CLIENT_VALIDATION_ERROR);
  110. }
  111. $newName = Str::random(32) . '.' . $file->getClientOriginalExtension();
  112. $path = Storage::disk($disk)->putFileAs($dir, $file, $newName);
  113. return [
  114. 'path' => $path,
  115. 'name' => $newName,
  116. 'original_name' => $file->getClientOriginalName(),
  117. 'disk' => $disk,
  118. ];
  119. }
  120. }