repository = $repository; $this->validator = $validator; } public function index($course_video_id) { $this->repository->pushCriteria(AttachCriteria::class); $courseAttaches = $this->repository->where('course_video_id', $course_video_id)->paginate(request('per_page', self::PAGE_NUM)); $attaches = $this->repository->parserResult($courseAttaches); return Response::success($attaches); } public function store($course_video_id, Request $request) { $this->validate($request, $this->validator->getRules(ValidatorInterface::RULE_CREATE)); try { $data = $request->only(array_keys($this->validator->getRules(ValidatorInterface::RULE_CREATE))); $data['course_video_id'] = $course_video_id; $courseAttach = $this->repository->create($data); return Response::success($courseAttach); } catch (\Exception $e) { return $this->errorStore($e); } } public function update(Request $request, $id) { $this->validate($request, $this->validator->getRules(ValidatorInterface::RULE_UPDATE)); try { $attaches = $this->repository->update($request->only(array_keys($this->validator->getRules(ValidatorInterface::RULE_UPDATE))), $id); return Response::success($attaches); } catch (\Exception $e) { $this->errorStore($e); } } public function destroy($id) { try { $this->repository->delete($id); return Response::success(null, T('successfully delete.')); } catch (\Exception $exception) { return Response::fail(T('Delete failed.'), ResponseCodeEnum::SERVICE_UPDATE_DATA_ERROR); } } public function download($id) { $attach = Attach::query()->where('id', '=', (int)$id)->first(); if ($attach) { $attach->download_times++; $attach->save(); $resource = Resource::query()->find($attach->path); // return response()->download(storage_path('app/public/' . $resource->path), $resource->name, [ return response()->download(Storage::disk($resource->disk)->path($resource->path), $resource->name, [ 'Access-Control-Allow-Origin' => '*', 'Access-Control-Allow-Methods' => '*', 'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With', 'Access-Control-Expose-Headers' => 'Content-Disposition', 'Content-type' => 'application/octet-stream', 'Content-Disposition' => 'attachment; filename=' . $resource->name, ]); } return $this->errorFail(); } }