|
9 | 9 |
|
10 | 10 | class TinyImageController |
11 | 11 | { |
12 | | - public function upload(Request $request): JsonResponse |
13 | | - { |
14 | | - $validator = $this->validateRequest($request); |
15 | | - if ($validator->fails()) { |
16 | | - return response()->json(['error' => $validator->errors()->first()]); |
17 | | - } |
18 | | - |
19 | | - $imageFolder = config('nova-tinymce-editor.extra.upload_images.folder', 'images'); |
20 | | - reset($_FILES); |
21 | | - $temp = current($_FILES); |
22 | | - if (is_uploaded_file($temp['tmp_name'])) { |
23 | | - $file = Storage::disk(config('nova-tinymce-editor.extra.upload_images.disk', 'public'))->putFile($imageFolder, $temp['tmp_name']); |
24 | | - |
25 | | - return response()->json(['location' => Storage::disk(config('nova-tinymce-editor.extra.upload_images.disk', 'public'))->url($file)]); |
26 | | - } else { |
27 | | - return response()->json(['error' => 'Failed to move uploaded file.']); |
28 | | - } |
29 | 12 |
|
30 | | - } |
31 | | - |
32 | | - public function validateRequest(Request $request): \Illuminate\Validation\Validator |
| 13 | + public function __invoke(Request $request): JsonResponse |
33 | 14 | { |
34 | | - $maxSize = config('nova-tinymce-editor.extra.upload_images.maxSize', 2048); |
35 | | - $validator = Validator::make($request->all(), [ |
36 | | - 'file' => 'required|image|mimes:jpeg,png,jpg,gif|max:'.$maxSize, |
| 15 | + $request->validate([ |
| 16 | + 'file' => [ |
| 17 | + 'required', |
| 18 | + 'image', |
| 19 | + 'mimes:jpeg,png,jpg,gif', |
| 20 | + 'max:' . config('nova-tinymce-editor.extra.upload_images.maxSize', 2048), |
| 21 | + ], |
37 | 22 | ]); |
38 | | - |
39 | | - return $validator; |
| 23 | + $disk = config('nova-tinymce-editor.extra.upload_images.disk'); |
| 24 | + try{ |
| 25 | + $file = $request->file('file') |
| 26 | + ->storePublicly( |
| 27 | + config('nova-tinymce-editor.extra.upload_images.folder'), |
| 28 | + compact('disk') |
| 29 | + ); |
| 30 | + }catch (\Throwable $e){ |
| 31 | + report($e); |
| 32 | + return response()->json(['error' => 'Failed to move uploaded file.']); |
| 33 | + } |
| 34 | + return response()->json(['location' => Storage::disk($disk)->url($file)]); |
40 | 35 | } |
41 | 36 | } |
0 commit comments