| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the PHP-FFmpeg-video-streaming package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Amin Yazdanpanah <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Streaming\Clouds; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Streaming\Exception\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Streaming\Exception\RuntimeException; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | class GoogleCloudStorage implements CloudInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     /** @var \Google\Cloud\Storage\StorageClient*/ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     private $storage; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * GoogleCloudStorage constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @param  array $config | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function __construct(array $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         if(!class_exists('\Google\Cloud\Storage\StorageClient')){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             throw new RuntimeException('Google\Cloud\Storage\StorageClient not found. make sure the package is installed: composer require google/cloud-storage'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             $this->storage = new \Google\Cloud\Storage\StorageClient($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 33 |  |  |             throw new InvalidArgumentException(sprintf("Invalid inputs:\n %s", $e->getMessage()), $e->getCode(), $e); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @param  string $dir | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * @param  array $options | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     public function uploadDirectory(string $dir, array $options): void | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $bucket = $this->getBucket($options); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $cloud_filename = dirname($options['filename'] ?? ''); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         unset($options['bucket'], $options['user_project'], $options['filename']); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             foreach (scandir($dir) as $file) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                 $path = $dir . DIRECTORY_SEPARATOR . $file; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 $name = $cloud_filename ? implode('/', [$cloud_filename, $file]) : $file; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                 $options = array_merge($options, ['name' => $name]); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                 if (is_file($path)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                     $bucket->upload(fopen($path, 'r'), $options); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         } catch (\Exception $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             throw new RuntimeException(sprintf("An error occurred while uploading files: %s", $e->getMessage()), $e->getCode(), $e); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @param  string $save_to | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @param  array $options | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function download(string $save_to, array $options): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $bucket = $this->getBucket($options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if(!isset($options['bucket'])){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             throw new InvalidArgumentException("You need to set the bucket name in the option array"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $name = $options['object_name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         unset($options['bucket'], $options['user_project'], $options['object_name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $bucket->object($name, $options)->downloadToFile($save_to); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  |             throw new RuntimeException(sprintf("An error occurred while downloading the file: %s", $e->getMessage()), $e->getCode(), $e); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * @param array $options | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * @return \Google\Cloud\Storage\Bucket | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     private function getBucket(array $options): \Google\Cloud\Storage\Bucket | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         if(!isset($options['bucket'])){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             throw new InvalidArgumentException("You need to set the bucket name in the option array"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         try{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             return $this->storage->bucket($options['bucket'], $options['user_project'] ?? false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         }catch (\Exception $e){ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  |             throw new RuntimeException(sprintf("An error occurred while opening the bucket: %s", $e->getMessage()), $e->getCode(), $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  | } |