| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Mostafaznv\Larupload\Concerns\Storage\Attachment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Facades\Storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Mostafaznv\Larupload\Enums\LaruploadFileType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Mostafaznv\Larupload\Larupload; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Mostafaznv\Larupload\Actions\FixExceptionNamesAction; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | trait StyleAttachment | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |      * Handle styles | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      * resize, crop and generate styles from original file | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @param string $id | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @param Model|string $class | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * @param bool $standalone | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     protected function handleStyles(string $id, Model|string $model, bool $standalone = false): void | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         switch ($this->type) { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |             case LaruploadFileType::IMAGE: | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |                 foreach ($this->imageStyles as $name => $style) { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |                     $path = $this->getBasePath($id, $name); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |                     $saveTo = $path . '/' . FixExceptionNamesAction::make($this->output['name'], $name)->run(); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |                     Storage::disk($this->disk)->makeDirectory($path); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |                     $this->img($this->file)->resize($saveTo, $style); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             case LaruploadFileType::VIDEO: | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |                 if ($this->ffmpegQueue) { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |                     if ($this->driverIsNotLocal()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |                         $this->uploadOriginalFile($id, $this->localDisk); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |                     if ($model instanceof Model) { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |                         $this->initializeFFMpegQueue( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |                             $model->id, $model->getMorphClass(), $standalone | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |                         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |                     else { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |                         $this->initializeFFMpegQueue( | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |                             $id, $model, $standalone | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                 else { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                     $this->handleVideoStyles($id); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             case LaruploadFileType::AUDIO: | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                 if ($this->ffmpegQueue) { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                     if ($this->driverIsNotLocal()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                         $this->uploadOriginalFile($id, $this->localDisk); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                     if ($model instanceof Model) { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                         $this->initializeFFMpegQueue( | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                             $model->id, $model->getMorphClass(), $standalone | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                     else { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |                         $this->initializeFFMpegQueue( | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |                             $id, $model, $standalone | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |                         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |                 else { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                     $this->handleAudioStyles($id); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * Handle styles for videos | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * @param $id | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     protected function handleVideoStyles($id): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         foreach ($this->videoStyles as $name => $style) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $path = $this->getBasePath($id, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             Storage::disk($this->disk)->makeDirectory($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             $saveTo = "$path/{$this->output['name']}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $this->ffmpeg()->manipulate($style, $saveTo); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         if (count($this->streams)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $fileName = pathinfo($this->output['name'], PATHINFO_FILENAME) . '.m3u8'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $path = $this->getBasePath($id, Larupload::STREAM_FOLDER); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             Storage::disk($this->disk)->makeDirectory($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  |             $this->ffmpeg()->stream($this->streams, $path, $fileName); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * Handle styles for audios | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      * @param $id | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     protected function handleAudioStyles($id): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         foreach ($this->audioStyles as $name => $style) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             $path = $this->getBasePath($id, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             Storage::disk($this->disk)->makeDirectory($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $saveTo = "$path/{$this->output['name']}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  |             $this->ffmpeg()->audio($style, $saveTo); | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |      * Prepare style path | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |      * this function will use to prepare full path of given style to generate url/download response | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * @param string $style | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      * @return string|null | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     protected function prepareStylePath(string $style): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $staticStyles = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             Larupload::ORIGINAL_FOLDER, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             Larupload::COVER_FOLDER, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             Larupload::STREAM_FOLDER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         if (isset($this->id) and (in_array($style, $staticStyles) or array_key_exists($style, $this->imageStyles) or array_key_exists($style, $this->videoStyles) or array_key_exists($style, $this->audioStyles))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             $name = $style == Larupload::COVER_FOLDER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 ? $this->output['cover'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 : $this->output['name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             $type = $this->output['type'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                 ? LaruploadFileType::from($this->output['type']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                 : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             if ($name and $style == Larupload::STREAM_FOLDER) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                 if ($type === LaruploadFileType::VIDEO) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                     $name = pathinfo($name, PATHINFO_FILENAME) . '.m3u8'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                     $path = $this->getBasePath($this->id, $style); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                     return "$path/$name"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                 return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             else if ($name and $this->styleHasFile($style)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                 $name = FixExceptionNamesAction::make($name, $style, $this->getStyle($style))->run(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                 $path = $this->getBasePath($this->id, $style); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                 return "$path/$name"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 169 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |  |