| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Jackal\Downloader\Downloader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Jackal\Downloader\Exception\DownloadException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Jackal\Downloader\Utils\DownloaderUtil; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\OptionsResolver\OptionsResolver; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | abstract class AbstractDownloader implements DownloaderInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     protected $tempFilePathName; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     protected $options = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * AbstractDownloader constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @param string $id | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @param array $config | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function __construct($id, array $config = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $options = new OptionsResolver(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $options->setDefaults(array_merge($config, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             'force' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             'video_id' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             'overwrite' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $options->setAllowedTypes('force', 'bool'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $options->setAllowedTypes('overwrite', 'bool'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $options->setAllowedTypes('video_id', ['string','integer']); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |         $this->options = $options->resolve($config); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     protected function forceDownload() : bool | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         return $this->options['force'] == true; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     protected function forceOverwriteFile() : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |         return $this->options['overwrite'] == true; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     public function getOptions(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         return $this->options; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     protected function getVideoId() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  |         return $this->options['video_id']; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     protected function getFormats() : array | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         if(!isset($this->options['format'])){ | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             return []; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         if(is_scalar($this->options['format'])){ | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             return [$this->options['format']]; | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         return $this->options['format']; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     protected function filterByFormats(array $possibleResults) : array{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if($this->getFormats() != []) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             foreach ($this->getFormats() as $selectedFormat) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 if (isset($possibleResults[$selectedFormat])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     return [$selectedFormat => $possibleResults[$selectedFormat]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             throw DownloadException::formatNotFound($this->getFormats(), $possibleResults); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  |         return $possibleResults; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * @param string $destinationFile | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      * @param callable|null $callback | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      * @throws DownloadException | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     public function download(string $destinationFile, callable $callback = null) : void | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         if (file_exists($destinationFile) and !$this->forceOverwriteFile()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |             throw DownloadException::destinationFileAlreadyExists($destinationFile); | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         if (!is_dir(dirname($destinationFile))) { | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |             if (!mkdir(dirname($destinationFile), 0777, true)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |                 throw DownloadException::cannotCreateDirectory(dirname($destinationFile)); | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         $this->tempFilePathName = $destinationFile . '.temp'; | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         if (file_exists($this->tempFilePathName) and !$this->forceDownload()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |             throw DownloadException::tempFileAlreadyExists($this->tempFilePathName); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |         DownloaderUtil::downloadURL($this->getURL(), $this->tempFilePathName, $callback); | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |         rename($this->tempFilePathName, $destinationFile); | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     public function __destruct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         if (is_file($this->tempFilePathName)) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 134 |  |  |             unlink($this->tempFilePathName); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 137 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 138 |  |  |  |