|
1
|
|
|
<?php |
|
2
|
|
|
namespace phpbu\App\Backup\Sync; |
|
3
|
|
|
|
|
4
|
|
|
use Kunnu\Dropbox\DropboxApp as DropboxConfig; |
|
5
|
|
|
use Kunnu\Dropbox\Dropbox as DropboxApi; |
|
6
|
|
|
use Kunnu\Dropbox\DropboxFile; |
|
7
|
|
|
use phpbu\App\Backup\Collector; |
|
8
|
|
|
use phpbu\App\Backup\Path; |
|
9
|
|
|
use phpbu\App\Result; |
|
10
|
|
|
use phpbu\App\Backup\Target; |
|
11
|
|
|
use phpbu\App\Util; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Dropbox |
|
15
|
|
|
* |
|
16
|
|
|
* @package phpbu |
|
17
|
|
|
* @subpackage Backup |
|
18
|
|
|
* @author Sebastian Feldmann <[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
|
|
|
* @since Class available since Release 1.1.1 |
|
23
|
|
|
*/ |
|
24
|
|
|
class Dropbox implements Simulator |
|
25
|
|
|
{ |
|
26
|
|
|
use Clearable; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* API access token |
|
30
|
|
|
* |
|
31
|
|
|
* Goto https://www.dropbox.com/developers/apps |
|
32
|
|
|
* create your app |
|
33
|
|
|
* - Dropbox api app |
|
34
|
|
|
* - files and datastore |
|
35
|
|
|
* - yes |
|
36
|
|
|
* - provide some app name "my-dropbox-app" |
|
37
|
|
|
* - generate access token to authenticate connection to your dropbox |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $token; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Remote path |
|
45
|
|
|
* |
|
46
|
|
|
* @var Path |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $path; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Dropbox api client |
|
52
|
|
|
* |
|
53
|
|
|
* @var DropboxApi |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $client; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Unix timestamp of generating path from placeholder. |
|
59
|
|
|
* |
|
60
|
|
|
* @var int |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $time; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* (non-PHPDoc) |
|
66
|
|
|
* |
|
67
|
|
|
* @see \phpbu\App\Backup\Sync::setup() |
|
68
|
|
|
* @param array $config |
|
69
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
|
70
|
|
|
* @throws \phpbu\App\Exception |
|
71
|
|
|
*/ |
|
72
|
5 |
|
public function setup(array $config) |
|
73
|
|
|
{ |
|
74
|
5 |
|
if (!class_exists('\\Kunnu\\Dropbox\\Dropbox')) { |
|
75
|
|
|
throw new Exception('Dropbox sdk not loaded: use composer to install "kunalvarma05/dropbox-php-sdk"'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// check for mandatory options |
|
79
|
5 |
|
$this->validateConfig($config, ['token', 'path']); |
|
80
|
|
|
|
|
81
|
3 |
|
$this->token = $config['token']; |
|
82
|
|
|
// make sure the path contains leading and trailing slashes |
|
83
|
3 |
|
$this->path = new Path(Util\Path::withLeadingSlash($config['path']), $this->time); |
|
84
|
|
|
|
|
85
|
3 |
|
$this->setUpClearable($config); |
|
86
|
3 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Make sure all mandatory keys are present in given config. |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $config |
|
92
|
|
|
* @param array $keys |
|
93
|
|
|
* @throws Exception |
|
94
|
|
|
*/ |
|
95
|
5 |
|
protected function validateConfig(array $config, array $keys) |
|
96
|
|
|
{ |
|
97
|
5 |
|
foreach ($keys as $option) { |
|
98
|
5 |
|
if (!Util\Arr::isSetAndNotEmptyString($config, $option)) { |
|
99
|
5 |
|
throw new Exception($option . ' is mandatory'); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
3 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* (non-PHPDoc) |
|
106
|
|
|
* |
|
107
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
|
108
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
109
|
|
|
* @param \phpbu\App\Result $result |
|
110
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
|
111
|
|
|
*/ |
|
112
|
|
|
public function sync(Target $target, Result $result) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->time = time(); |
|
115
|
|
|
$sourcePath = $target->getPathname(); |
|
116
|
|
|
$dropboxPath = Util\Path::withTrailingSlash($this->path->getPath()) . $target->getFilename(); |
|
117
|
|
|
if (!$this->client) { |
|
118
|
|
|
$this->connect(); |
|
119
|
|
|
} |
|
120
|
|
|
try { |
|
121
|
|
|
$file = new DropboxFile($sourcePath); |
|
122
|
|
|
$meta = $this->client->upload($file, $dropboxPath, ['autorename' => true]); |
|
123
|
|
|
} catch (\Exception $e) { |
|
124
|
|
|
throw new Exception($e->getMessage(), null, $e); |
|
125
|
|
|
} |
|
126
|
|
|
// run remote cleanup |
|
127
|
|
|
$this->cleanup($target, $result); |
|
128
|
|
|
$result->debug('upload: done (' . $meta->getSize() . ')'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Simulate the sync execution. |
|
133
|
|
|
* |
|
134
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
135
|
|
|
* @param \phpbu\App\Result $result |
|
136
|
|
|
*/ |
|
137
|
2 |
|
public function simulate(Target $target, Result $result) |
|
138
|
|
|
{ |
|
139
|
2 |
|
$result->debug( |
|
140
|
2 |
|
'sync backup to dropbox' . PHP_EOL |
|
141
|
2 |
|
. ' token: ********' . PHP_EOL |
|
142
|
2 |
|
. ' location: ' . $this->path->getPath() . PHP_EOL |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
2 |
|
$this->simulateRemoteCleanup($target, $result); |
|
146
|
2 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Creates collector for Dropbox |
|
150
|
|
|
* |
|
151
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
152
|
|
|
* @return \phpbu\App\Backup\Collector |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function createCollector(Target $target): Collector |
|
155
|
|
|
{ |
|
156
|
|
|
return new \phpbu\App\Backup\Collector\Dropbox($target, $this->client, $this->path->getPathRaw(), $this->time); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Create Dropbox api client |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function connect() |
|
163
|
|
|
{ |
|
164
|
|
|
$config = new DropboxConfig("id", "secret", $this->token); |
|
165
|
|
|
$this->client = new DropboxApi($config); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|