123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- /*
- * This file is part of the Jiannei/lumen-api-starter.
- *
- * (c) Jiannei <longjian.huang@foxmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace App\Services;
- use App\Contracts\Repositories\ResourceRepository;
- use App\Repositories\Eloquent\ResourceRepositoryEloquent;
- use App\Repositories\Enums\ResponseCodeEnum;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Str;
- use Illuminate\Validation\ValidationException;
- use Jiannei\Enum\Laravel\Repositories\Enums\HttpStatusCodeEnum;
- use Jiannei\Response\Laravel\Support\Facades\Response;
- class ResourceService
- {
- public $allowed_img_ext = ["png", "jpg", "gif", 'jpeg'];
- public $allowed_file_ext = ["doc", 'docx', "xls", "pdf", 'xlsx', 'mp4', 'webvtt', 'vtt'];
- private $repository;
- /**
- * UserService constructor.
- *
- * @param ResourceRepositoryEloquent $repository
- */
- public function __construct(ResourceRepository $repository)
- {
- $this->repository = $repository;
- }
- public function handleUpload(Request $request)
- {
- $disk = 'public';
- // 获取上传的文件
- $file = $request->file('file');
- if (!$file->isFile()) return false;
- $type = $request->get('file_type', 'img');
- if ($type === 'img') {
- // 如果上传的不是图片将终止操作
- if (!in_array($file->getClientOriginalExtension(), $this->allowed_img_ext)) {
- return false;
- }
- } else {
- if (!in_array($file->getClientOriginalExtension(), $this->allowed_file_ext)) {
- return false;
- }
- }
- $type = $request->get('type', 'default');
- $dir = 'resource';
- switch ($type) {
- case 'course':
- $dir = 'course';
- break;
- case 'course_video':
- $dir = 'course_video';
- break;
- case 'course_video_subtitle_zh':
- $dir = 'course_video_subtitle_zh';
- break;
- case 'course_video_subtitle_en':
- $dir = 'course_video_subtitle_en';
- break;
- case 'attach':
- $dir = 'course_attach';
- break;
- case 'medical_record':
- $dir = 'medical_record';
- break;
- case 'user_headimg':
- $dir = 'user_headimg';
- break;
- case 'import_students':
- $dir = 'import_students';
- break;
- case 'import_drugs':
- $dir = 'import_drugs';
- break;
- case 'import_prescriptons':
- $dir = 'import_prescriptons';
- break;
- case 'banners':
- $dir = 'banners';
- break;
- }
- $newName = Str::random(32) . '.' . $file->getClientOriginalExtension();
- $path = Storage::disk($disk)->putFileAs($dir, $file, $newName);
- $url = Storage::disk($disk)->url($path);
- $resource = $this->repository->insertResource($path, $newName, $file->getClientOriginalName(), $url, $file->getSize());
- return $resource;
- }
- /**
- * 上传文件
- * @param Request $request
- * @param string $dir
- * @param array $allow_ext
- * @return
- * Author: Mead
- */
- public function upload(Request $request, $dir = '', $allow_ext = [])
- {
- $disk = 'public';
- // 获取上传的文件
- $file = $request->file('file');
- if (!$file->isFile()) return Response::fail('请上传文件类型', ResponseCodeEnum::CLIENT_VALIDATION_ERROR);;
- if (!in_array($file->getClientOriginalExtension(), $allow_ext)) {
- return Response::fail('上传文件的类型不对', ResponseCodeEnum::CLIENT_VALIDATION_ERROR);
- }
- $newName = Str::random(32) . '.' . $file->getClientOriginalExtension();
- $path = Storage::disk($disk)->putFileAs($dir, $file, $newName);
- return [
- 'path' => $path,
- 'name' => $newName,
- 'original_name' => $file->getClientOriginalName(),
- 'disk' => $disk,
- ];
- }
- }
|