| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Bavix\Helpers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Bavix\Exceptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Bavix\Exceptions\NotFound; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Curl\Curl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class Stream | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @param string $from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @param string $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @throws Exceptions\PermissionDenied | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @throws NotFound\Path | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 3 |  |     public static function download($from, $to): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         /** | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |          * @var bool|resource $fromStream | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |          */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 3 |  |         $fromStream = File::open($from); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 3 |  |         if (!is_resource($fromStream)) | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 1 |  |             throw new NotFound\Path('Stream `' . $from . '` not found'); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 2 |  |         if (!File::real($to) && !File::touch($to)) | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 1 |  |             throw new Exceptions\PermissionDenied('File `' . $to . '`'); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 1 |  |         File::put($to, $fromStream); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 1 |  |         return File::close($fromStream); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param string|array $urlOrOptions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param array        $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @return Curl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @throws Exceptions\Invalid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @throws \ErrorException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public static function upload($urlOrOptions, array $data = []): Curl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $options = $urlOrOptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if (is_string($options)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $options = ['url' => $urlOrOptions]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if (!isset($options['url'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             throw new Exceptions\Invalid('Param option `URL` not found!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $url    = $options['url']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $method = $options['method'] ?? 'post'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $headers = $options['headers'] ?? []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $data = Arr::map($data, function ($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             if (\is_string($value) && Str::first($value) === '@') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 return curl_file_create(Str::withoutFirst($value)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $curl = new Curl(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if (!empty($headers)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             foreach ($headers as $key => $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 $headers[$key] = $key . ': ' . implode(',', (array)$value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $curl->setOpt(CURLOPT_HTTPHEADER, Arr::getValues($headers)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $curl->$method($url, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $response = JSON::decode($curl->response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         if (JSON::errorNone()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $curl->response = $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return $curl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  |