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\Result; |
8
|
|
|
use phpbu\App\Backup\Target; |
9
|
|
|
use phpbu\App\Util\Arr; |
10
|
|
|
use phpbu\App\Util\Str; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Dropbox |
14
|
|
|
* |
15
|
|
|
* @package phpbu |
16
|
|
|
* @subpackage Backup |
17
|
|
|
* @author Sebastian Feldmann <[email protected]> |
18
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
19
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
20
|
|
|
* @link http://phpbu.de/ |
21
|
|
|
* @since Class available since Release 1.1.1 |
22
|
|
|
*/ |
23
|
|
|
class Dropbox implements Simulator |
24
|
|
|
{ |
25
|
|
|
use Clearable; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* API access token |
29
|
|
|
* |
30
|
|
|
* Goto https://www.dropbox.com/developers/apps |
31
|
|
|
* create your app |
32
|
|
|
* - Dropbox api app |
33
|
|
|
* - files and datastore |
34
|
|
|
* - yes |
35
|
|
|
* - provide some app name "my-dropbox-app" |
36
|
|
|
* - generate access token to authenticate connection to your dropbox |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $token; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Remote path |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $path; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Dropbox api client |
51
|
|
|
* |
52
|
|
|
* @var DropboxApi |
53
|
|
|
*/ |
54
|
|
|
protected $client; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* (non-PHPDoc) |
58
|
|
|
* |
59
|
|
|
* @see \phpbu\App\Backup\Sync::setup() |
60
|
|
|
* @param array $config |
61
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
62
|
|
|
* @throws \phpbu\App\Exception |
63
|
|
|
*/ |
64
|
5 |
|
public function setup(array $config) |
65
|
|
|
{ |
66
|
5 |
|
if (!class_exists('\\Kunnu\\Dropbox\\Dropbox')) { |
67
|
|
|
throw new Exception('Dropbox sdk not loaded: use composer to install "kunalvarma05/dropbox-php-sdk"'); |
68
|
|
|
} |
69
|
5 |
|
if (!Arr::isSetAndNotEmptyString($config, 'token')) { |
70
|
1 |
|
throw new Exception('API access token is mandatory'); |
71
|
|
|
} |
72
|
4 |
|
if (!Arr::isSetAndNotEmptyString($config, 'path')) { |
73
|
1 |
|
throw new Exception('dropbox path is mandatory'); |
74
|
|
|
} |
75
|
|
|
// make sure the path contains leading and trailing slashes |
76
|
3 |
|
$this->path = Str::withLeadingSlash(Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path']))); |
77
|
3 |
|
$this->token = $config['token']; |
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
|
|
|
$sourcePath = $target->getPathname(); |
93
|
|
|
$dropboxPath = $this->path . $target->getFilename(); |
94
|
|
|
if (!$this->client) { |
95
|
|
|
$this->connect(); |
96
|
|
|
} |
97
|
|
|
try { |
98
|
|
|
$file = new DropboxFile($sourcePath); |
99
|
|
|
$meta = $this->client->upload($file, $dropboxPath, ['autorename' => true]); |
100
|
|
|
} catch (\Exception $e) { |
101
|
|
|
throw new Exception($e->getMessage(), null, $e); |
102
|
|
|
} |
103
|
|
|
// run remote cleanup |
104
|
|
|
$this->cleanup($target, $result); |
105
|
|
|
$result->debug('upload: done (' . $meta->getSize() . ')'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Simulate the sync execution. |
110
|
|
|
* |
111
|
|
|
* @param \phpbu\App\Backup\Target $target |
112
|
|
|
* @param \phpbu\App\Result $result |
113
|
|
|
*/ |
114
|
2 |
|
public function simulate(Target $target, Result $result) |
115
|
|
|
{ |
116
|
2 |
|
$result->debug( |
117
|
2 |
|
'sync backup to dropbox' . PHP_EOL |
118
|
2 |
|
. ' token: ********' . PHP_EOL |
119
|
2 |
|
. ' location: ' . $this->path . PHP_EOL |
120
|
|
|
); |
121
|
|
|
|
122
|
2 |
|
$this->simulateRemoteCleanup($target, $result); |
123
|
2 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Execute the remote clean up if needed |
127
|
|
|
* |
128
|
|
|
* @param \phpbu\App\Backup\Target $target |
129
|
|
|
* @param \phpbu\App\Result $result |
130
|
|
|
*/ |
131
|
|
View Code Duplication |
public function cleanup(Target $target, Result $result) |
132
|
|
|
{ |
133
|
|
|
if (!$this->cleaner) { |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$collector = new \phpbu\App\Backup\Collector\Dropbox($target, $this->client, $this->path); |
138
|
|
|
$this->cleaner->cleanup($target, $collector, $result); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Create Dropbox api client |
143
|
|
|
*/ |
144
|
|
|
protected function connect() |
145
|
|
|
{ |
146
|
|
|
$config = new DropboxConfig("id", "secret", $this->token); |
147
|
|
|
$this->client = new DropboxApi($config); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|