|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace phpbu\App\Backup\Sync; |
| 4 | + |
| 5 | +use Google\Cloud\Storage\StorageClient; |
| 6 | +use phpbu\App\Backup\Collector; |
| 7 | +use phpbu\App\Backup\Path; |
| 8 | +use phpbu\App\Backup\Target; |
| 9 | +use phpbu\App\Configuration; |
| 10 | +use phpbu\App\Result; |
| 11 | +use phpbu\App\Util; |
| 12 | + |
| 13 | +/** |
| 14 | + * Google Drive |
| 15 | + * |
| 16 | + * @package phpbu |
| 17 | + * @subpackage Backup |
| 18 | + * @author David Dattee <[email protected]> |
| 19 | + * @copyright Sebastian Feldmann <[email protected]> |
| 20 | + * @license https://opensource.org/licenses/MIT The MIT License (MIT) |
| 21 | + * @link http://phpbu.de/ |
| 22 | + */ |
| 23 | +class GoogleCloudStorage implements Simulator |
| 24 | +{ |
| 25 | + use Cleanable; |
| 26 | + |
| 27 | + /** |
| 28 | + * Google Gloud Storage client. |
| 29 | + * |
| 30 | + * @var StorageClient |
| 31 | + */ |
| 32 | + private $client; |
| 33 | + |
| 34 | + /** |
| 35 | + * Google json secret file. |
| 36 | + * |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + private $secret; |
| 40 | + |
| 41 | + /** |
| 42 | + * Bucket to upload to |
| 43 | + * |
| 44 | + * @var string |
| 45 | + */ |
| 46 | + private $bucket; |
| 47 | + |
| 48 | + /** |
| 49 | + * Path to upload to in the bucket |
| 50 | + * |
| 51 | + * @var string |
| 52 | + */ |
| 53 | + private $parent; |
| 54 | + |
| 55 | + /** |
| 56 | + * (non-PHPDoc) |
| 57 | + * |
| 58 | + * @param array $options |
| 59 | + * @throws \phpbu\App\Exception |
| 60 | + * @see \phpbu\App\Backup\Sync::setup() |
| 61 | + */ |
| 62 | + public function setup(array $options) |
| 63 | + { |
| 64 | + if (!class_exists('\\Google\\Cloud\\Storage\\StorageClient')) { |
| 65 | + throw new Exception('google cloud api client not loaded: use composer to install "google/cloud-storage"'); |
| 66 | + } |
| 67 | + if (!Util\Arr::isSetAndNotEmptyString($options, 'secret')) { |
| 68 | + throw new Exception('google secret json file is mandatory'); |
| 69 | + } |
| 70 | + if (!Util\Arr::isSetAndNotEmptyString($options, 'bucket')) { |
| 71 | + throw new Exception('bucket to upload to is mandatory'); |
| 72 | + } |
| 73 | + $this->parent = Util\Arr::getValue($options, 'path', ''); |
| 74 | + |
| 75 | + $this->setupAuthFiles($options); |
| 76 | + $this->setUpCleanable($options); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Make sure google authentication files exist and determine absolute path to them. |
| 81 | + * |
| 82 | + * @param array $config |
| 83 | + * @throws \phpbu\App\Backup\Sync\Exception |
| 84 | + */ |
| 85 | + private function setupAuthFiles(array $config) |
| 86 | + { |
| 87 | + $secret = Util\Path::toAbsolutePath($config['secret'], Configuration::getWorkingDirectory()); |
| 88 | + if (!file_exists($secret)) { |
| 89 | + throw new Exception(sprintf('google secret json file not found at %s', $secret)); |
| 90 | + } |
| 91 | + |
| 92 | + $this->secret = $secret; |
| 93 | + $this->bucket = $config['bucket']; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Execute the Sync |
| 98 | + * |
| 99 | + * @param \phpbu\App\Backup\Target $target |
| 100 | + * @param \phpbu\App\Result $result |
| 101 | + * @throws \phpbu\App\Backup\Sync\Exception |
| 102 | + * @see \phpbu\App\Backup\Sync::sync() |
| 103 | + */ |
| 104 | + public function sync(Target $target, Result $result) |
| 105 | + { |
| 106 | + try { |
| 107 | + $this->client = $this->createGoogleCloudClient(); |
| 108 | + $bucket = $this->client->bucket($this->bucket); |
| 109 | + $hash = $this->calculateHash($target->getPathname()); |
| 110 | + |
| 111 | + $sentObject = $bucket->upload( |
| 112 | + fopen($target->getPathname(), 'rb'), |
| 113 | + [ |
| 114 | + 'name' => ($this->parent ? $this->parent . '/' : '') . $target->getFilename(), |
| 115 | + 'metadata' => [ |
| 116 | + 'crc32c' => $hash, |
| 117 | + ], |
| 118 | + ], |
| 119 | + ); |
| 120 | + |
| 121 | + $result->debug(sprintf('upload: done: %s', $sentObject->name())); |
| 122 | + $this->cleanup($target, $result); |
| 123 | + } catch (\Exception $e) { |
| 124 | + throw new Exception($e->getMessage(), 0, $e); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private function calculateHash(string $filePath): string |
| 129 | + { |
| 130 | + return base64_encode(hex2bin(hash_file('crc32c', $filePath))); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Simulate the sync execution. |
| 135 | + * |
| 136 | + * @param \phpbu\App\Backup\Target $target |
| 137 | + * @param \phpbu\App\Result $result |
| 138 | + */ |
| 139 | + public function simulate(Target $target, Result $result) |
| 140 | + { |
| 141 | + $result->debug('sync backup to google cloud storage' . PHP_EOL); |
| 142 | + |
| 143 | + $this->isSimulation = true; |
| 144 | + $this->simulateRemoteCleanup($target, $result); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Setup google api client and google drive service. |
| 149 | + */ |
| 150 | + protected function createGoogleCloudClient(): StorageClient |
| 151 | + { |
| 152 | + if (!$this->client) { |
| 153 | + $this->client = new StorageClient(['keyFilePath' => $this->secret]); |
| 154 | + } |
| 155 | + |
| 156 | + return $this->client; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Creates collector for remote cleanup. |
| 161 | + * |
| 162 | + * @param \phpbu\App\Backup\Target $target |
| 163 | + * @return \phpbu\App\Backup\Collector |
| 164 | + */ |
| 165 | + protected function createCollector(Target $target): Collector |
| 166 | + { |
| 167 | + return new Collector\GoogleCloudStorage( |
| 168 | + $target, |
| 169 | + $this->createGoogleCloudClient()->bucket($this->bucket), |
| 170 | + new Path($this->parent) |
| 171 | + ); |
| 172 | + } |
| 173 | +} |
0 commit comments