| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Http\Controllers\Tool\Ajax; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Models\Eloquent\Tool\ImageHosting; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Http\Controllers\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Http\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Validation\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use App\Models\ResponseModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Auth; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | class ImageHostingController extends Controller | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * Generate a new pastebin. | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @return Response | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function generate(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $user=Auth::user(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         if (!$user->hasPermission(26)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             return ResponseModel::err(2001); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $isValid=$request->file('image')->isValid(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         if ($isValid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $extension=$request->file('image')->extension(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             return ResponseModel::err(1005); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $allow_extension=['jpg', 'png', 'jpeg', 'gif', 'bmp']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         if ($isValid && in_array($extension, $allow_extension)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $path=$request->file('image')->store('/static/img/upload', 'NOJPublic'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $id=ImageHosting::create([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 'user_id' => $user->id, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 'relative_path' => "/$path" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             ])->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             return ResponseModel::success(200, null, [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 'relative_path' => "/$path", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 'path' => url($path), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 'id' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 'redirect_url' => route('tool.imagehosting.detail', ['id'=>$id]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } else { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |             return ResponseModel::err(1005); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |  |