* * 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, ]; } }