1 | <?php |
||
25 | class Dropbox extends SyncInterface |
||
26 | { |
||
27 | use Clearable; |
||
28 | |||
29 | /** |
||
30 | * API access token |
||
31 | * |
||
32 | * Goto https://www.dropbox.com/developers/apps |
||
33 | * create your app |
||
34 | * - Dropbox api app |
||
35 | * - files and datastore |
||
36 | * - yes |
||
37 | * - provide some app name "my-dropbox-app" |
||
38 | * - generate access token to authenticate connection to your dropbox |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $token; |
||
43 | |||
44 | /** |
||
45 | * Remote path |
||
46 | * |
||
47 | * @var Path |
||
48 | */ |
||
49 | protected $path; |
||
50 | |||
51 | /** |
||
52 | * Dropbox api client |
||
53 | * |
||
54 | * @var DropboxApi |
||
55 | */ |
||
56 | protected $client; |
||
57 | |||
58 | /** |
||
59 | * (non-PHPDoc) |
||
60 | * |
||
61 | * @see \phpbu\App\Backup\Sync::setup() |
||
62 | * @param array $config |
||
63 | * @throws \phpbu\App\Backup\Sync\Exception |
||
64 | * @throws \phpbu\App\Exception |
||
65 | */ |
||
66 | 5 | public function setup(array $config) |
|
67 | { |
||
68 | 5 | if (!class_exists('\\Kunnu\\Dropbox\\Dropbox')) { |
|
69 | throw new Exception('Dropbox sdk not loaded: use composer to install "kunalvarma05/dropbox-php-sdk"'); |
||
70 | } |
||
71 | |||
72 | // check for mandatory options |
||
73 | 5 | $this->validateConfig($config, ['token', 'path']); |
|
74 | |||
75 | 3 | $this->token = $config['token']; |
|
76 | // make sure the path contains leading and trailing slashes |
||
77 | 3 | $this->path = new Path(Util\Path::withLeadingSlash($config['path']), $this->time); |
|
78 | |||
79 | 3 | $this->setUpClearable($config); |
|
80 | 3 | } |
|
81 | |||
82 | /** |
||
83 | * (non-PHPDoc) |
||
84 | * |
||
85 | * @see \phpbu\App\Backup\Sync::sync() |
||
86 | * @param \phpbu\App\Backup\Target $target |
||
87 | * @param \phpbu\App\Result $result |
||
88 | * @throws \phpbu\App\Backup\Sync\Exception |
||
89 | */ |
||
90 | public function sync(Target $target, Result $result) |
||
91 | { |
||
92 | $this->time = time(); |
||
93 | $sourcePath = $target->getPathname(); |
||
94 | $dropboxPath = Util\Path::withTrailingSlash($this->path->getPath()) . $target->getFilename(); |
||
95 | if (!$this->client) { |
||
96 | $this->connect(); |
||
97 | } |
||
98 | try { |
||
99 | $file = new DropboxFile($sourcePath); |
||
100 | $meta = $this->client->upload($file, $dropboxPath, ['autorename' => true]); |
||
101 | } catch (\Exception $e) { |
||
102 | throw new Exception($e->getMessage(), null, $e); |
||
103 | } |
||
104 | // run remote cleanup |
||
105 | $this->cleanup($target, $result); |
||
106 | $result->debug('upload: done (' . $meta->getSize() . ')'); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Simulate the sync execution. |
||
111 | * |
||
112 | * @param \phpbu\App\Backup\Target $target |
||
113 | * @param \phpbu\App\Result $result |
||
114 | */ |
||
115 | 2 | public function simulate(Target $target, Result $result) |
|
125 | |||
126 | /** |
||
127 | * Creates collector for Dropbox |
||
128 | * |
||
129 | * @param \phpbu\App\Backup\Target $target |
||
130 | * @return \phpbu\App\Backup\Collector |
||
131 | */ |
||
132 | protected function createCollector(Target $target): Collector |
||
136 | |||
137 | /** |
||
138 | * Create Dropbox api client |
||
139 | */ |
||
140 | protected function connect() |
||
145 | } |
||
146 |