|
| 1 | +<?php |
| 2 | +namespace phpbu\Backup\Sync; |
| 3 | + |
| 4 | +use Dropbox as dbx; |
| 5 | +use phpbu\App\Result; |
| 6 | +use phpbu\Backup\Sync; |
| 7 | +use phpbu\Backup\Target; |
| 8 | + |
| 9 | +/** |
| 10 | + * Copycom |
| 11 | + * |
| 12 | + * @package phpbu |
| 13 | + * @subpackage Backup |
| 14 | + * @author Sebastian Feldmann <sebastian@phpbu.de> |
| 15 | + * @copyright Sebastian Feldmann <sebastian@phpbu.de> |
| 16 | + * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License |
| 17 | + * @link http://www.phpbu.de/ |
| 18 | + * @since Class available since Release 1.1.1 |
| 19 | + */ |
| 20 | +class Copycom implements Sync |
| 21 | +{ |
| 22 | + /** |
| 23 | + * API access key |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + protected $appKey; |
| 28 | + |
| 29 | + /** |
| 30 | + * API access token |
| 31 | + * |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + protected $appSecret; |
| 35 | + |
| 36 | + /** |
| 37 | + * API access key |
| 38 | + * |
| 39 | + * @var string |
| 40 | + */ |
| 41 | + protected $userKey; |
| 42 | + |
| 43 | + /** |
| 44 | + * API access token |
| 45 | + * |
| 46 | + * @var string |
| 47 | + */ |
| 48 | + protected $userSecret; |
| 49 | + |
| 50 | + /** |
| 51 | + * Remote path |
| 52 | + * |
| 53 | + * @var string |
| 54 | + */ |
| 55 | + protected $path; |
| 56 | + |
| 57 | + /** |
| 58 | + * (non-PHPdoc) |
| 59 | + * @see \phpbu\Backup\Sync::setup() |
| 60 | + */ |
| 61 | + public function setup(array $config) |
| 62 | + { |
| 63 | + if (!class_exists('\\Barracuda\\Copy\\API')) { |
| 64 | + throw new Exception('Copy api not loaded: use composer "barracuda/copy": "1.1.*" to install'); |
| 65 | + } |
| 66 | + if (!isset($config['app.key']) || '' == $config['app.key']) { |
| 67 | + throw new Exception('API access key is mandatory'); |
| 68 | + } |
| 69 | + if (!isset($config['app.secret']) || '' == $config['app.secret']) { |
| 70 | + throw new Exception('API access secret is mandatory'); |
| 71 | + } |
| 72 | + if (!isset($config['user.key']) || '' == $config['user.key']) { |
| 73 | + throw new Exception('User access key is mandatory'); |
| 74 | + } |
| 75 | + if (!isset($config['user.secret']) || '' == $config['user.secret']) { |
| 76 | + throw new Exception('User access secret is mandatory'); |
| 77 | + } |
| 78 | + if (!isset($config['path']) || '' == $config['path']) { |
| 79 | + throw new Exception('dropbox path is mandatory'); |
| 80 | + } |
| 81 | + $this->appKey = $config['app.key']; |
| 82 | + $this->appSecret = $config['app.secret']; |
| 83 | + $this->userKey = $config['user.key']; |
| 84 | + $this->userSecret = $config['user.secret']; |
| 85 | + $this->path = $config['path'] . ( substr($config['path'], -1) !== '/' ? '/' : '' ); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * (non-PHPdoc) |
| 90 | + * @see \phpbu\Backup\Sync::sync() |
| 91 | + */ |
| 92 | + public function sync(Target $target, Result $result) |
| 93 | + { |
| 94 | + $sourcePath = $target->getPathnameCompressed(); |
| 95 | + $targetPath = $this->path . $target->getFilenameCompressed(); |
| 96 | + |
| 97 | + $copy = new \Barracuda\Copy\API($this->appKey, $this->appSecret, $this->userKey, $this->userSecret); |
| 98 | + |
| 99 | + try { |
| 100 | + // open a file to upload |
| 101 | + $fh = fopen($sourcePath, 'rb'); |
| 102 | + // upload the file in 1MB chunks |
| 103 | + $parts = array(); |
| 104 | + while ($data = fread($fh, 1024 * 1024)) { |
| 105 | + $part = $copy->sendData($data); |
| 106 | + array_push($parts, $part); |
| 107 | + } |
| 108 | + fclose($fh); |
| 109 | + // finalize the file |
| 110 | + $copy->createFile($targetPath, $parts); |
| 111 | + } catch (\Exception $e) { |
| 112 | + throw new Exception($e->getMessage(), null, $e); |
| 113 | + } |
| 114 | + $result->debug('upload: done'); |
| 115 | + } |
| 116 | +} |
0 commit comments