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\Backup\Sync as SyncInterface; |
10
|
|
|
use phpbu\App\Result; |
11
|
|
|
use phpbu\App\Backup\Target; |
12
|
|
|
use phpbu\App\Util; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Dropbox |
16
|
|
|
* |
17
|
|
|
* @package phpbu |
18
|
|
|
* @subpackage Backup |
19
|
|
|
* @author Sebastian Feldmann <[email protected]> |
20
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
21
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
22
|
|
|
* @link http://phpbu.de/ |
23
|
|
|
* @since Class available since Release 1.1.1 |
24
|
|
|
*/ |
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) |
116
|
|
|
{ |
117
|
2 |
|
$result->debug( |
118
|
2 |
|
'sync backup to dropbox' . PHP_EOL |
119
|
2 |
|
. ' token: ********' . PHP_EOL |
120
|
2 |
|
. ' location: ' . $this->path->getPath() . PHP_EOL |
121
|
|
|
); |
122
|
|
|
|
123
|
2 |
|
$this->simulateRemoteCleanup($target, $result); |
124
|
2 |
|
} |
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 |
133
|
|
|
{ |
134
|
|
|
return new \phpbu\App\Backup\Collector\Dropbox($target, $this->client, $this->path->getPathRaw(), $this->time); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Create Dropbox api client |
139
|
|
|
*/ |
140
|
|
|
protected function connect() |
141
|
|
|
{ |
142
|
|
|
$config = new DropboxConfig("id", "secret", $this->token); |
143
|
|
|
$this->client = new DropboxApi($config); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|