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 Cleanable; |
|
|
|
|
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 \phpbu\App\Backup\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
|
8 |
|
public function setup(array $config) |
73
|
|
|
{ |
74
|
8 |
|
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
|
8 |
|
$this->validateConfig($config, ['token', 'path']); |
80
|
|
|
|
81
|
6 |
|
$this->time = time(); |
82
|
6 |
|
$this->token = $config['token']; |
83
|
|
|
// make sure the path contains a leading slash |
84
|
6 |
|
$this->path = new Path(Util\Path::withLeadingSlash($config['path']), $this->time); |
85
|
|
|
|
86
|
6 |
|
$this->setUpCleanable($config); |
87
|
6 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Make sure all mandatory keys are present in given config. |
91
|
|
|
* |
92
|
|
|
* @param array $config |
93
|
|
|
* @param string[] $keys |
94
|
|
|
* @throws Exception |
95
|
|
|
*/ |
96
|
8 |
|
protected function validateConfig(array $config, array $keys) |
97
|
|
|
{ |
98
|
8 |
|
foreach ($keys as $option) { |
99
|
8 |
|
if (!Util\Arr::isSetAndNotEmptyString($config, $option)) { |
100
|
8 |
|
throw new Exception($option . ' is mandatory'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
6 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* (non-PHPDoc) |
107
|
|
|
* |
108
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
109
|
|
|
* @param \phpbu\App\Backup\Target $target |
110
|
|
|
* @param \phpbu\App\Result $result |
111
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
112
|
|
|
*/ |
113
|
3 |
|
public function sync(Target $target, Result $result) |
114
|
|
|
{ |
115
|
3 |
|
$sourcePath = $target->getPathname(); |
116
|
3 |
|
$dropboxPath = $this->path->getPath() . '/' . $target->getFilename(); |
117
|
3 |
|
$client = $this->createClient(); |
118
|
|
|
|
119
|
|
|
try { |
120
|
3 |
|
$file = new DropboxFile($sourcePath); |
121
|
3 |
|
$meta = $client->upload($file, $dropboxPath, ['autorename' => true]); |
122
|
2 |
|
$result->debug('upload: done (' . $meta->getSize() . ')'); |
123
|
|
|
|
124
|
|
|
// run remote cleanup |
125
|
2 |
|
$this->cleanup($target, $result); |
126
|
1 |
|
} catch (\Exception $e) { |
127
|
1 |
|
throw new Exception($e->getMessage(), null, $e); |
128
|
|
|
} |
129
|
2 |
|
} |
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
|
2 |
|
$this->isSimulation = true; |
145
|
2 |
|
$this->simulateRemoteCleanup($target, $result); |
146
|
2 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Creates the Dropbox collector. |
150
|
|
|
* |
151
|
|
|
* @param \phpbu\App\Backup\Target $target |
152
|
|
|
* @return \phpbu\App\Backup\Collector |
153
|
|
|
*/ |
154
|
1 |
|
protected function createCollector(Target $target) : Collector |
155
|
|
|
{ |
156
|
1 |
|
$collector = new Collector\Dropbox($target, $this->path, $this->createClient()); |
157
|
1 |
|
$collector->setSimulation($this->isSimulation); |
158
|
|
|
|
159
|
1 |
|
return $collector; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Create a dropbox api client. |
164
|
|
|
* |
165
|
|
|
* @return \Kunnu\Dropbox\Dropbox |
166
|
|
|
*/ |
167
|
|
|
protected function createClient() : DropboxApi |
168
|
|
|
{ |
169
|
|
|
if (!$this->client) { |
170
|
|
|
$config = new DropboxConfig("id", "secret", $this->token); |
171
|
|
|
$this->client = new DropboxApi($config); |
172
|
|
|
} |
173
|
|
|
return $this->client; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|