123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace App\Http\Controllers\Api\Course;
- use App\Http\Controllers\Controller;
- use App\Repositories\Criteria\Course\AttachCriteria;
- use App\Repositories\Enums\ResponseCodeEnum;
- use App\Repositories\Models\Course\Attach;
- use App\Repositories\Models\Base\Resource;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Str;
- use Jiannei\Response\Laravel\Support\Facades\Response;
- use Prettus\Validator\Contracts\ValidatorInterface;
- use App\Contracts\Repositories\Course\AttachRepository;
- use App\Repositories\Validators\Course\AttachValidator;
- /**
- * Class CourseAttachesController.
- *
- * @package namespace App\Http\Controllers;
- */
- class AttachController extends Controller
- {
- /**
- * @var AttachRepository
- */
- protected $repository;
- /**
- * @var AttachValidator
- */
- protected $validator;
- /**
- * CourseAttachesController constructor.
- *
- * @param AttachRepository $repository
- * @param AttachValidator $validator
- */
- public function __construct(AttachRepository $repository, AttachValidator $validator)
- {
- $this->repository = $repository;
- $this->validator = $validator;
- }
- public function index()
- {
- $this->repository->pushCriteria(AttachCriteria::class);
- $courseAttaches = $this->repository->paginate(request('per_page', self::PAGE_NUM));
- return Response::success($courseAttaches);
- }
- /**
- * 创建
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\Resource|void
- * @throws \Illuminate\Validation\ValidationException
- * Author: Mead
- */
- public function store(Request $request)
- {
- $this->validate($request, $this->validator->getRules(ValidatorInterface::RULE_CREATE));
- try {
- $data = $request->only(array_keys($this->validator->getRules(ValidatorInterface::RULE_CREATE)));
- $data['slug'] = Str::random();
- $courseAttach = $this->repository->create($data);
- return Response::success($courseAttach);
- } catch (\Exception $e) {
- return $this->errorStore($e);
- }
- }
- /**
- * 更新
- * @param Request $request
- * @param $id
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\Resource
- * @throws \Illuminate\Validation\ValidationException
- * Author: Mead
- */
- 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);
- }
- }
- /**
- * 删除
- * @param $id
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\Resource
- * Author: Mead
- */
- 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);
- }
- }
- /**
- * 文件下载
- * @param $id
- * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse
- * Author: Mead
- */
- public function download($slug)
- {
- $attach = Attach::query()->where('slug', '=', $slug)->first();
- if ($attach) {
- $attach->download_times++;
- $attach->save();
- $resource = Resource::query()->find($attach->path);
- if (!Storage::disk($resource->disk)->exists($resource->path)) {
- return Response::fail('找不到文件');
- }
- 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();
- }
- }
|