123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace App\Http\Controllers\Api\Base;
- use App\Http\Controllers\Controller;
- use App\Repositories\Enums\ResponseCodeEnum;
- use App\Services\Base\ResourceService;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Storage;
- /**
- * 资源管理
- */
- class ResourceController extends Controller
- {
- protected $allowed_img_ext = 'system_upload_img_type';
- protected $allowed_file_ext = 'system_upload_file_type';
- /**
- * @var ResourceService
- */
- private $resourceService;
- /**
- * ResourceController constructor.
- *
- * @param ResourceService $resourceService
- */
- public function __construct(ResourceService $resourceService)
- {
- parent::__construct();
- $this->resourceService = $resourceService;
- }
- /**
- * 上传资源
- * @must
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
- * @throws \Illuminate\Validation\ValidationException
- */
- public function upload(Request $request)
- {
- $this->validate($request, [
- 'file' => 'required|file',
- 'dir' => 'required|alpha_dash',
- 'file_type' => 'required|in:img,file'
- ], [], [
- 'file' => '文件',
- 'dir' => '存放目录',
- 'file_type' => '文件类型',
- ]);
- $file = $request->file('file');
- if (!$file->isFile()) {
- return $this->response->fail('请上传一个文件', ResponseCodeEnum::CLIENT_VALIDATION_ERROR);
- }
- $file_type = $request->get('file_type', 'img');
- if ($file_type === 'img') {
- // 如果上传的不是图片将终止操作
- $config = str2arr(byCodeGetSetting($this->allowed_img_ext));
- if (!is_array($config)) {
- abort(ResponseCodeEnum::SYSTEM_CONFIG_ERROR, '系统图片配置错误');
- }
- if (!in_array(strtolower($file->getClientOriginalExtension()), $config)) {
- return $this->response->fail('图片类型不合法', ResponseCodeEnum::CLIENT_VALIDATION_ERROR);
- }
- } else {
- $config = str2arr(byCodeGetSetting($this->allowed_file_ext));
- if (!is_array($config)) {
- abort(ResponseCodeEnum::SYSTEM_CONFIG_ERROR, '系统文件配置错误');
- }
- if (!in_array(strtolower($file->getClientOriginalExtension()), $config)) {
- return $this->response->fail('文件类型不合法', ResponseCodeEnum::CLIENT_VALIDATION_ERROR);
- }
- }
- $model = $this->resourceService->handleUpload($request);
- if (!$model) {
- return $this->response->fail('上传文件失败', ResponseCodeEnum::SYSTEM_ERROR);
- }
- //判断图片还是视频,供前端判断
- $type = 0;
- $img_type = config('site.img_type');
- $video_type = config('site.video_type');
- $arr = explode('.', $model->path);
- if (strpos($img_type, $arr[1]) !== false) $type = 1;
- if (strpos($video_type, $arr[1]) !== false) $type = 2;
- return $this->response->success([
- 'id' => $model->id,
- 'path' => $model->path,
- 'url' => $model->url,
- 'type' => $type
- ]);
- }
- /**
- * 下载资源
- * @must
- * @param Request $request
- * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
- * @throws \Illuminate\Validation\ValidationException
- */
- public function download(Request $request)
- {
- $this->validate($request, ['id' => 'required|integer']);
- $resource = $this->resourceService->handleProfile($request->get('id'));
- return response()->download(Storage::disk($resource->disk)->url($resource->path));
- }
- }
|