| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Domains\FileLinks; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Facades\Auth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use App\FileLinks; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use App\Domains\FileLinks\SaveFileLinkFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use App\Http\Requests\NewFileLinkRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class CreateFileLink extends SaveFileLinkFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |     //  Create a brand new file link and determine if any files should be linked to it | 
            
                                                                        
                            
            
                                    
            
            
                | 17 | 8 |  |     public function create(NewFileLinkRequest $request) | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 | 8 |  |         $linkID = $this->createLink($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 8 |  |         if($request->session()->has('newLinkFile')) | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 2 |  |             $this->relocateFiles($linkID); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 8 |  |         return ['link' => $linkID, 'name' => Str::slug($request->name)]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     //  Build a new link data in the database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 8 |  |     protected function createLink($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 8 |  |         $link = FileLinks::create([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 8 |  |             'user_id'      => Auth::user()->user_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 8 |  |             'cust_id'      => $data->customerID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 8 |  |             'link_hash'    => $this->generateHash(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 8 |  |             'link_name'    => $data->name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 8 |  |             'expire'       => $data->expire, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 8 |  |             'allow_upload' => isset($data->allowUp) && $data->allowUp ? true : false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 8 |  |             'note'         => $data->instructions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 8 |  |         return $link->link_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     //  Generate a random hash to use as the link.  Verify it is not already in use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 8 |  |     protected function generateHash() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 8 |  |             $hash = strtolower(Str::random(15)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 8 |  |             $dup  = FileLinks::where('link_hash', $hash)->get()->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 8 |  |         } while($dup != 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 8 |  |         return $hash; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |  |