LaravelRUS /
SleepingOwlAdmin
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace SleepingOwl\Admin\Http\Controllers; |
||||
| 4 | |||||
| 5 | use Illuminate\Http\JsonResponse; |
||||
| 6 | use Illuminate\Http\Request; |
||||
| 7 | use Illuminate\Http\UploadedFile; |
||||
| 8 | use Illuminate\Routing\Controller; |
||||
| 9 | use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
||||
| 10 | use SleepingOwl\Admin\Form\Element\File; |
||||
| 11 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||||
| 12 | use Validator; |
||||
| 13 | |||||
| 14 | class UploadController extends Controller |
||||
| 15 | { |
||||
| 16 | /** |
||||
| 17 | * @param Request $request |
||||
| 18 | * @param ModelConfigurationInterface $model |
||||
| 19 | * @param string $field |
||||
| 20 | * @param int|null $id |
||||
| 21 | * |
||||
| 22 | * @return JsonResponse |
||||
| 23 | */ |
||||
| 24 | public function fromField(Request $request, ModelConfigurationInterface $model, $field, $id = null) |
||||
| 25 | { |
||||
| 26 | if (! is_null($id)) { |
||||
| 27 | $item = $model->getRepository()->find($id); |
||||
| 28 | if (is_null($item) || ! $model->isEditable($item)) { |
||||
| 29 | return new JsonResponse([ |
||||
| 30 | 'message' => trans('lang.message.access_denied'), |
||||
| 31 | ], 403); |
||||
| 32 | } |
||||
| 33 | |||||
| 34 | $form = $model->fireEdit($id); |
||||
| 35 | } else { |
||||
| 36 | if (! $model->isCreatable()) { |
||||
| 37 | return new JsonResponse([ |
||||
| 38 | 'message' => trans('lang.message.access_denied'), |
||||
| 39 | ], 403); |
||||
| 40 | } |
||||
| 41 | |||||
| 42 | $form = $model->fireCreate(); |
||||
| 43 | } |
||||
| 44 | |||||
| 45 | /** @var File $element */ |
||||
| 46 | if (is_null($element = $form->getElement($field))) { |
||||
| 47 | throw new NotFoundHttpException("Field [{$field}] not found"); |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | $rules = $element->getUploadValidationRules(); |
||||
| 51 | $messages = $element->getUploadValidationMessages(); |
||||
| 52 | $labels = $element->getUploadValidationLabels(); |
||||
| 53 | |||||
| 54 | /** @var \Illuminate\Contracts\Validation\Validator $validator */ |
||||
| 55 | $validator = Validator::make($request->all(), $rules, $messages, $labels); |
||||
| 56 | |||||
| 57 | $element->customValidation($validator); |
||||
| 58 | |||||
| 59 | if ($validator->fails()) { |
||||
| 60 | return new JsonResponse([ |
||||
| 61 | 'message' => trans('lang.message.validation_error'), |
||||
| 62 | 'errors' => $validator->errors()->get('file'), |
||||
| 63 | ], 400); |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | $file = $request->file('file'); |
||||
| 67 | |||||
| 68 | $filename = $element->getUploadFileName($file); |
||||
| 69 | $path = $element->getUploadPath($file); |
||||
| 70 | $settings = $element->getUploadSettings(); |
||||
| 71 | |||||
| 72 | $result = $element->saveFile($file, $path, $filename, $settings); |
||||
| 73 | |||||
| 74 | /* When driver not file */ |
||||
| 75 | return new JsonResponse($result); |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | /** |
||||
| 79 | * @param Request $request |
||||
| 80 | * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|\Symfony\Component\HttpFoundation\Response |
||||
| 81 | */ |
||||
| 82 | public function ckEditorStore(Request $request) |
||||
| 83 | { |
||||
| 84 | //dropZone && CKEDITOR fileBrowser && CKEDITOR drag&drop |
||||
| 85 | /** @var UploadedFile $file */ |
||||
| 86 | $file = $request->image ? $request->image : $request->file; |
||||
| 87 | $file = $file ? $file : $request->upload; |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 88 | if (is_array($file)) { |
||||
|
0 ignored issues
–
show
|
|||||
| 89 | $file = $file[0]; |
||||
| 90 | } |
||||
| 91 | |||||
| 92 | $result = []; |
||||
| 93 | |||||
| 94 | $extensions = collect(['jpe', 'jpeg', 'jpg', 'png', 'bmp', 'ico', 'gif']); |
||||
| 95 | |||||
| 96 | if ($extensions->search($file->getClientOriginalExtension())) { |
||||
| 97 | $uploadFileName = md5(time().$file->getClientOriginalName()).'.'.$file->getClientOriginalExtension(); |
||||
| 98 | |||||
| 99 | $file->move(public_path(config('sleeping_owl.imagesUploadDirectory')), $uploadFileName); |
||||
| 100 | |||||
| 101 | $result['url'] = asset( |
||||
| 102 | config('sleeping_owl.imagesUploadDirectory').'/'.$uploadFileName |
||||
| 103 | ); |
||||
| 104 | $result['uploaded'] = 1; |
||||
| 105 | $result['fileName'] = $uploadFileName; |
||||
| 106 | |||||
| 107 | if ($request->CKEditorFuncNum && $request->CKEditor && $request->langCode) { |
||||
| 108 | return app('sleeping_owl.template') |
||||
| 109 | ->view('helper.ckeditor.ckeditor_upload_file', compact('result')); |
||||
|
0 ignored issues
–
show
The method
view() does not exist on Illuminate\Contracts\Foundation\Application.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 110 | } |
||||
| 111 | |||||
| 112 | if ($result) { |
||||
|
0 ignored issues
–
show
The expression
$result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using Loading history...
|
|||||
| 113 | return response($result); |
||||
| 114 | } |
||||
| 115 | } |
||||
| 116 | |||||
| 117 | return response('Something wrong', 500); |
||||
| 118 | } |
||||
| 119 | } |
||||
| 120 |